Skip to content

Commit c82ce35

Browse files
Alvaro LasernaAlvaro Laserna
authored andcommitted
add exists method
1 parent 0492211 commit c82ce35

3 files changed

Lines changed: 114 additions & 1 deletion

File tree

tests/selenium_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_template_matching(self, selenium_driver: TestUIDriver):
4444
.swipe(start_x=50, start_y=50, end_x=100, end_y=100)
4545
selenium_driver.navigate_to(
4646
"https://www.testdevlab.com/"
47-
).e('css', '#email').send_keys('some@email.com')
47+
).e('css', '#email').wait_until_exists(10).send_keys('some@email.com')
4848
selenium_driver.raise_errors()
4949

5050
@pytest.mark.signup

testui/elements/testui_collection.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,39 @@ def wait_until_all_visible(self, seconds=10.0, log=True):
6565
f"been found after {time.time() - start}s: \n {compose_error}"
6666
)
6767

68+
def wait_until_all_exist(self, seconds=10.0, log=True):
69+
"""
70+
Wait until all elements in collection are visible
71+
:param seconds: timeout
72+
:param log: log to console
73+
"""
74+
start = time.time()
75+
threads = []
76+
for arg in self.args:
77+
threads.append(
78+
threading.Thread(
79+
target=arg.wait_until_exists, args=(seconds, log)
80+
)
81+
)
82+
for thread in threads:
83+
thread.start()
84+
for thread in threads:
85+
thread.join()
86+
if time.time() < start + seconds:
87+
logger.log(
88+
f"{self.args[0].device_name}: Collection of elements has been "
89+
f"found after {time.time() - start}s"
90+
)
91+
else:
92+
compose_error = ""
93+
error: str
94+
for error in self.__errors:
95+
compose_error = compose_error + f"{error} \n"
96+
self.__show_error(
97+
f"{self.args[0].device_name}: Collection of elements has not "
98+
f"been found after {time.time() - start}s: \n {compose_error}"
99+
)
100+
68101
def find_visible(self, seconds=10, return_el_number=False):
69102
"""
70103
Find first visible element in collection

testui/elements/testui_element.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,49 @@ def is_visible(self, log=True, **kwargs) -> bool:
270270

271271
return is_visible
272272

273+
def exist(self, log=True, **kwargs) -> bool:
274+
"""
275+
Check if element exists
276+
:param log: Boolean
277+
:return: Boolean
278+
"""
279+
is_not = False
280+
281+
# Allows passing "is_not" as a kwarg to not overwrite self.__is_not.
282+
# This is not meant to be used by the user.
283+
if "is_not" in kwargs:
284+
is_not = kwargs["is_not"]
285+
else:
286+
is_not = self.__is_not
287+
self.__is_not = False
288+
289+
exists = False
290+
try:
291+
exists = self.get_element()
292+
293+
if log:
294+
if exists:
295+
self.__put_log(
296+
f'{self.device_name}: element "{self.locator_type}: '
297+
f'{self.locator}" exists'
298+
)
299+
else:
300+
self.__put_log(
301+
f'{self.device_name}: element "{self.locator_type}: '
302+
f'{self.locator}" does not exist'
303+
)
304+
except Exception:
305+
if log:
306+
self.__put_log(
307+
f'{self.device_name}: element "{self.locator_type}: '
308+
f'{self.locator}" does not exists'
309+
)
310+
311+
if is_not:
312+
return not exists
313+
314+
return exists
315+
273316
def is_visible_in(self, seconds) -> bool:
274317
"""
275318
Check if element is visible in a certain amount of time
@@ -344,6 +387,43 @@ def wait_until_visible(self, seconds=10.0, log=True):
344387
if is_not:
345388
err_text = "was"
346389

390+
self.__show_error(
391+
f"{self.device_name}: Element {err_text} found visible with the following "
392+
f'locator: "{self.locator_type}: {self.locator}" '
393+
f"after {time.time() - start}s"
394+
)
395+
396+
return self
397+
398+
def wait_until_exists(self, seconds=10.0, log=True):
399+
"""
400+
Wait until element is visible
401+
:param seconds: seconds
402+
:param log: Boolean
403+
"""
404+
start = time.time()
405+
406+
is_not = self.__is_not
407+
self.__is_not = False
408+
409+
is_visible = self.exist(log=False, is_not=is_not)
410+
while time.time() < start + seconds and not is_visible:
411+
time.sleep(0.2)
412+
is_visible = self.exist(log=False, is_not=is_not)
413+
414+
if is_visible:
415+
if log:
416+
self.__put_log(
417+
f'{self.device_name}: Element "{self.locator_type}: '
418+
f'{self.locator}" was visible in {time.time() - start}s'
419+
)
420+
421+
return self
422+
423+
err_text = "was not"
424+
if is_not:
425+
err_text = "was"
426+
347427
self.__show_error(
348428
f"{self.device_name}: Element {err_text} found with the following "
349429
f'locator: "{self.locator_type}: {self.locator}" '

0 commit comments

Comments
 (0)