Skip to content

Commit

Permalink
selenium: replace retries with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
naisanzaa committed Dec 27, 2023
1 parent cd1749f commit d27ae96
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions automon/integrations/seleniumWrapper/browser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import json
import time
import base64
import datetime
import tempfile
Expand Down Expand Up @@ -514,15 +515,14 @@ def wait_for(
self,
value: str or list,
by: By = By.XPATH,
retries: int = 5,
timeout: int = 3,
fail_on_error: bool = True,
**kwargs) -> str or False:
"""wait for something"""
if not retries:
retries = 5
timeout_start = time.time()
timeout_elapsed = round(abs(timeout_start - time.time()), 1)

retry = 0
while True:
while timeout_elapsed < timeout:
try:
if isinstance(value, list):
values = value
Expand Down Expand Up @@ -557,22 +557,17 @@ def wait_for(
return value
except Exception as error:
logger.error(str(dict(
timeout=f'{timeout_elapsed}/{timeout}',
error=error,
by=by,
url=self.url,
value=value,
error=error,
)))
Sleeper.seconds(0.2)

retry += 1

logger.error(str(dict(
url=self.url,
retry=f'{retry}/{retries}',
)))
Sleeper.seconds(0.1)
import asyncio
# await asyncio.sleep(0.1)

if retry == retries:
break
timeout_elapsed = round(abs(timeout_start - time.time()), 1)

if fail_on_error:
raise Exception(str(dict(
Expand All @@ -586,31 +581,31 @@ def wait_for(
def wait_for_element(
self,
element: str or list,
retries: int = None,
timeout: int = 3,
fail_on_error: bool = True,
**kwargs
) -> str or False:
"""wait for an element"""
return self.wait_for(
value=element,
by=self.by.ID,
retries=retries,
timeout=timeout,
fail_on_error=fail_on_error,
**kwargs
)

def wait_for_xpath(
self,
xpath: str or list,
retries: int = None,
timeout: int = 3,
fail_on_error: bool = True,
**kwargs
) -> str or False:
"""wait for an xpath"""
return self.wait_for(
value=xpath,
by=self.by.XPATH,
retries=retries,
timeout=timeout,
fail_on_error=fail_on_error,
**kwargs
)

0 comments on commit d27ae96

Please sign in to comment.