Skip to content

Commit

Permalink
Added wait for element to be in viewport.
Browse files Browse the repository at this point in the history
  • Loading branch information
VicGrygorchyk committed Nov 6, 2019
1 parent 6f73e41 commit 47badf5
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions se_wrapper/waits.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,32 @@ def nested(web_element):
timeout,
f"TimeoutException while waiting for the element to have a child {child_css_selector}")

def element_to_be_in_viewport(self, element, timeout: TimeoutType = DEFAULT_TIMEOUT):
"""Wait until element inside viewport's coordinates."""
find_viewport_pos_script = """
var height = document.documentElement.clientHeight;
var width = document.documentElement.clientWidth;
var arr = [height, width];
return arr;
"""
rect_ = self._webdriver.execute_script(find_viewport_pos_script)
height = rect_[0]
width = rect_[1]

def get_element_pos():

nonlocal height, width
pos_x = element.location.get('x')
pos_y = element.location.get('y')
if all((pos_x < width, pos_y < height)):
return element
return None

return self.wait_fluently(get_element_pos,
timeout,
f"TimeoutException while waiting {timeout} sec for element "
f"to be in viewport.")

def _wait_until(self, condition, timeout: TimeoutType = DEFAULT_TIMEOUT):
"""Wrapper method around Selenium WebDriverWait() with until().
:param condition: Selenium expected_condtions
Expand Down Expand Up @@ -255,17 +281,18 @@ def _switch_on_element_type(target, string, web_element_type, wrapped_element_ty

@staticmethod
def wait_fluently(condition: Callable, timeout: TimeoutType, err_msg: str):
"""Custom wait for special cases.
"""Custom wait for special cases where driver is not needed as arg for condition.
:param condition: function to verify if Condition is True
:param timeout: time to wait for positive condition.
:param err_msg: error message
:return: True if condition, else raises TimeoutException
:return: element if condition is True, else raises TimeoutException
"""
start_time = time.time()
while True:
if time.time() - start_time >= timeout:
raise TimeoutException(err_msg)
if condition:
return True
res = condition()
if res:
return res
time.sleep(0.3)

0 comments on commit 47badf5

Please sign in to comment.