Skip to content

Commit b0c666f

Browse files
committed
test: configurable timeout for element search
Signed-off-by: Saw-jan <saw.jan.grg3e@gmail.com>
1 parent 4bab3d2 commit b0c666f

5 files changed

Lines changed: 56 additions & 48 deletions

File tree

test/gui/helpers/AppHelper.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import pyautogui
22
import psutil
33
import threading
4+
import requests
45
from appium.webdriver import Remote, WebElement
56
from appium.options.common.base import AppiumOptions
67
from appium.webdriver.common.appiumby import AppiumBy as By
78
from selenium.common.exceptions import WebDriverException, NoSuchElementException
9+
from selenium.webdriver.common.timeouts import Timeouts
810

911
from helpers.ConfigHelper import get_config, get_app_env
1012
from helpers.ElementHelper import get_element_center_xy
1113
from helpers.keys.keys_map import get_key
14+
import helpers.api.http_helper as request
1215

1316

1417
def native_click(self, **kwargs):
@@ -35,23 +38,32 @@ def native_send_keys(self, key):
3538
pyautogui.press(get_key(key))
3639

3740

38-
def find_element(self, by, selector):
41+
def find_element(self, by, selector, timeout=None):
3942
"""
4043
Returns a visible element.
4144
Throws if no elements are found or if multiple visible elements are found.
4245
"""
43-
elements = self.find_elements(by, selector)
44-
elements_count = len(elements)
45-
if elements_count > 1:
46-
visible_elements = [el for el in elements if el.is_displayed()]
47-
if len(visible_elements) == 1:
48-
return visible_elements.pop()
49-
raise WebDriverException(
50-
f'Found {elements_count} elements using "{by}={selector}"'
51-
)
52-
if elements_count == 0:
53-
raise NoSuchElementException(f'No element found for "{by}={selector}"')
54-
return elements[0]
46+
47+
if timeout is not None:
48+
set_implicit_wait(timeout)
49+
50+
try:
51+
elements = self.find_elements(by, selector)
52+
elements_count = len(elements)
53+
if elements_count > 1:
54+
visible_elements = [el for el in elements if el.is_displayed()]
55+
if len(visible_elements) == 1:
56+
return visible_elements.pop()
57+
raise WebDriverException(
58+
f'Found {elements_count} elements using "{by}={selector}"'
59+
)
60+
if elements_count == 0:
61+
raise NoSuchElementException(f'No element found for "{by}={selector}"')
62+
return elements[0]
63+
finally:
64+
# reset implicit wait to default value
65+
if timeout is not None:
66+
set_implicit_wait(get_config('min_timeout'))
5567

5668

5769
def pause(self):
@@ -84,8 +96,11 @@ def create_app_session():
8496
f'{get_config("app_path")} -s {command_args} --logdebug',
8597
)
8698
options.set_capability('appium:environ', get_app_env())
87-
app_driver = Remote(command_executor='http://localhost:4723', options=options)
88-
app_driver.implicitly_wait = 10
99+
options.set_capability('timeouts', { 'implicit': get_config('min_timeout') * 1000 })
100+
app_driver = Remote(command_executor=get_config('webdriver_url'), options=options)
101+
# NOTE: these methods to set implicit wait time are not working:
102+
# app_driver.implicitly_wait(5)
103+
# app_driver.implicitly_wait = 5
89104

90105

91106
def close_and_kill_app():
@@ -117,3 +132,19 @@ def get_window_location():
117132
.location
118133
)
119134
return window['x'], window['y']
135+
136+
137+
def set_implicit_wait(timeout):
138+
print("set: ", timeout)
139+
"""
140+
Set the implicit wait time for the current session.
141+
"""
142+
session_id = app().session_id
143+
body = {'ms': timeout * 1000}
144+
response = requests.request(
145+
'POST',
146+
f'{get_config("webdriver_url")}/session/{session_id}/timeouts/implicit_wait',
147+
json=body,
148+
timeout=get_config('max_timeout'),
149+
)
150+
request.assert_http_status(response, 200, 'Failed to set implicit timeout')

test/gui/helpers/ConfigHelper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def get_app_env():
8787
'files_for_upload': os.path.join(CURRENT_DIR.parent, 'files-for-upload'),
8888
# actual file path where the client stores the crash log.
8989
'crash_log_file': os.path.join(gettempdir(), CRASH_LOG_FILE),
90+
'webdriver_url': 'http://localhost:4723',
9091
}
9192

9293
# mutable configs

test/gui/helpers/SyncHelper.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -399,21 +399,6 @@ def wait_for_resource_to_have_sync_error(resource, resource_type):
399399
wait_for_resource_to_have_sync_status(resource, resource_type, SYNC_STATUS['ERROR'])
400400

401401

402-
# performing actions immediately after completing the sync from the server does not work
403-
# The test should wait for a while before performing the action
404-
# issue: https://github.com/owncloud/client/issues/8832
405-
def wait_for_client_to_be_ready():
406-
global WAITED_AFTER_SYNC
407-
if not WAITED_AFTER_SYNC:
408-
time.sleep(get_config('min_timeout'))
409-
WAITED_AFTER_SYNC = True
410-
411-
412-
def clear_waited_after_sync():
413-
global WAITED_AFTER_SYNC
414-
WAITED_AFTER_SYNC = False
415-
416-
417402
def perform_file_explorer_vfs_action(resource_path, action):
418403
if action == 'Free up space':
419404
make_online_only(resource_path)

test/gui/pageObjects/SyncConnection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def is_sync_in_progress(sync_folder):
114114
sync_folder=sync_folder,
115115
sync_path=get_config('currentUserSyncPath'),
116116
),
117+
timeout=get_config("lowest_timeout"),
117118
)
118119
return True
119120
except NoSuchElementException:

test/gui/steps/file_context.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from helpers.SetupClientHelper import get_resource_path, get_temp_resource_path
1111
from helpers.SyncHelper import (
12-
wait_for_client_to_be_ready,
1312
listen_sync_status_for_item,
1413
)
1514
from helpers.Utils import wait_for
@@ -72,14 +71,12 @@ def write_file(resource, content):
7271
f.write(content)
7372

7473

75-
def wait_and_write_file(path, content):
76-
wait_for_client_to_be_ready()
74+
def write_file_to_sync_path(path, content):
7775
listen_sync_status_for_item(get_resource_path(path), 'FILE')
7876
write_file(path, content)
7977

8078

81-
def wait_and_try_to_write_file(resource, content):
82-
wait_for_client_to_be_ready()
79+
def try_to_write_file(resource, content):
8380
listen_sync_status_for_item(get_resource_path(resource), 'FILE')
8481
try:
8582
write_file(resource, content)
@@ -120,7 +117,6 @@ def copy_resource(resource_type, source, destination, from_files_for_upload=Fals
120117
if source == destination and destination != '/':
121118
destination = add_copy_suffix(source, resource_type)
122119

123-
wait_for_client_to_be_ready()
124120
listen_sync_status_for_item(destination, resource_type)
125121
if resource_type == 'folder':
126122
return shutil.copytree(source, destination)
@@ -134,13 +130,11 @@ def move_resource(username, resource_type, source, destination, is_temp_folder=F
134130
destination = ''
135131
destination = get_resource_path(destination, username)
136132

137-
wait_for_client_to_be_ready()
138133
listen_sync_status_for_item(destination, resource_type)
139134
shutil.move(source, destination)
140135

141136

142137
def deleteResource(resource, resource_type):
143-
wait_for_client_to_be_ready()
144138
listen_sync_status_for_item(resource, resource_type)
145139
resource_path = sanitize_path(get_resource_path(resource))
146140
if resource_type == 'file':
@@ -154,12 +148,11 @@ def deleteResource(resource, resource_type):
154148
)
155149
def step(context, username, filename):
156150
file = get_resource_path(filename, username)
157-
wait_and_write_file(convert_path_separators_for_os(file), context.text)
151+
write_file_to_sync_path(convert_path_separators_for_os(file), context.text)
158152

159153

160154
@When('user "{username}" creates a folder "{foldername}" inside the sync folder')
161155
def step(context, username, foldername):
162-
wait_for_client_to_be_ready()
163156
create_folder(foldername, username)
164157

165158

@@ -190,7 +183,6 @@ def step(context, resource_type, resource_name):
190183
@When('the user renames a file "{source}" to "{destination}"')
191184
@When('the user renames a folder "{source}" to "{destination}"')
192185
def step(context, source, destination):
193-
wait_for_client_to_be_ready()
194186
rename_file_folder(source, destination)
195187

196188

@@ -245,7 +237,7 @@ def step(context, resource_type, resource):
245237
@Given('the user has changed the content of local file "{filename}" to:')
246238
def step(context, filename):
247239
file_content = context.text
248-
wait_and_write_file(get_resource_path(filename), file_content)
240+
write_file_to_sync_path(get_resource_path(filename), file_content)
249241

250242

251243
@Then(
@@ -273,19 +265,19 @@ def step(context, filename):
273265
@When('the user overwrites the file "{resource}" with content "{content}"')
274266
def step(context, resource, content):
275267
resource = get_resource_path(resource)
276-
wait_and_write_file(resource, content)
268+
write_file_to_sync_path(resource, content)
277269

278270

279271
@When('the user tries to overwrite the file "|any|" with content "|any|"')
280272
def step(context, resource, content):
281273
resource = get_resource_path(resource)
282-
wait_and_try_to_write_file(resource, content)
274+
try_to_write_file(resource, content)
283275

284276

285277
@When('user "|any|" tries to overwrite the file "|any|" with content "|any|"')
286278
def step(context, user, resource, content):
287279
resource = get_resource_path(resource, user)
288-
wait_and_try_to_write_file(resource, content)
280+
try_to_write_file(resource, content)
289281

290282

291283
@When('the user deletes the {resource_type:ResourceType} "{resource_name}"')
@@ -297,7 +289,7 @@ def step(context, resource_type, resource_name):
297289
def step(context, username):
298290
for row in context.table:
299291
file = get_resource_path(row[0], username)
300-
wait_and_write_file(file, '')
292+
write_file_to_sync_path(file, '')
301293

302294

303295
@Given('the user has created a folder "{folder_name}" in temp folder')
@@ -414,7 +406,6 @@ def step(context, username, zip_file_name):
414406

415407
@When('user "|any|" copies file "|any|" to temp folder')
416408
def step(context, username, source):
417-
wait_for_client_to_be_ready()
418409
source_dir = get_resource_path(source, username)
419410
destination_dir = get_temp_resource_path(source)
420411
shutil.copy2(source_dir, destination_dir)
@@ -447,7 +438,6 @@ def step(context, resource_name, destination):
447438

448439
@When('the user deletes the following files')
449440
def step(context):
450-
wait_for_client_to_be_ready()
451441
for row in context.table:
452442
filename = row[0]
453443
deleteResource(filename, 'file')

0 commit comments

Comments
 (0)