Skip to content

Commit

Permalink
Merge pull request #3 from VicGrygorchyk/dev
Browse files Browse the repository at this point in the history
Fix some waits
  • Loading branch information
VicGrygorchyk authored Jan 20, 2020
2 parents a272e59 + 3498d34 commit c6e21a1
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 143 deletions.
1 change: 0 additions & 1 deletion selen_kaa/component/__init__.py

This file was deleted.

23 changes: 0 additions & 23 deletions selen_kaa/component/component.py

This file was deleted.

24 changes: 0 additions & 24 deletions selen_kaa/component/components_array.py

This file was deleted.

46 changes: 18 additions & 28 deletions selen_kaa/element/element_waits.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,99 +12,89 @@ def __init__(self, se_web_element, webdriver: WebDriver, timeout: TimeoutType):
self._timeout = timeout
self._wait = Wait(webdriver)

def be_visible(self, timeout: TimeoutType):
def be_visible(self, timeout: TimeoutType = None):
"""True when an element is visible on the html page.
:param timeout: time to wait element visibility.
"""
timeout_ = timeout if timeout else self._timeout
timeout_ = timeout if timeout is not None else self._timeout
return self._wait.element_to_be_visible(self.__web_element, timeout=timeout_)

def be_invisible(self, timeout: TimeoutType):
def be_invisible(self, timeout: TimeoutType = None):
"""True if an element is not visible on the html page.
:param timeout: time to wait element visibility.
"""
timeout_ = timeout if timeout else self._timeout
timeout_ = timeout if timeout is not None else self._timeout
return self._wait.element_to_be_invisible(self.__web_element, timeout_)

def have_class(self, expected_class: str, timeout: TimeoutType):
def have_class(self, expected_class: str, timeout: TimeoutType = None):
"""True when an element has a specific class.
:param expected_class: class_name for expected class (not css_selector).
:param timeout: time to wait for an element to have a class name.
"""
timeout_ = timeout if timeout else self._timeout
timeout_ = timeout if timeout is not None else self._timeout
return self._wait.element_to_get_class(self.__web_element, expected_class, timeout_)

def include_element(self, child_css_selector: str, timeout: TimeoutType):
def include_element(self, child_css_selector: str, timeout: TimeoutType = None):
"""True when an element gets a desired child element.
:param child_css_selector: a css selector for a child element.
:param timeout: time to wait for the condition.
"""
timeout_ = timeout if timeout else self._timeout
timeout_ = timeout if timeout is not None else self._timeout
return self._wait.element_to_include_child_element(
self.__web_element,
child_css_selector,
timeout_
)

def contain_text(self, text: str, timeout: TimeoutType):
def contain_text(self, text: str, timeout: TimeoutType = None):
"""True if an element contains a provided text in its text attribute.
:param text: expected text.
:param timeout: time to wait for the condition.
"""
timeout_ = self._timeout
if timeout:
timeout_ = timeout
timeout_ = timeout if timeout is not None else self._timeout
return self._wait.element_to_contain_text(self.__web_element, text, timeout_)

def have_similar_text(self, text: str, timeout: TimeoutType):
def have_similar_text(self, text: str, timeout: TimeoutType = None):
"""True if an element has a similar text in texts attribute.
Not precise comparision, e.g. returns True for:
"some" in "this is some text", " test\n" and "test", "TEST" and "test"
:param text: a text to compare for similarity.
:param timeout: time to wait for the condition.
"""
timeout_ = self._timeout
if timeout:
timeout_ = timeout
timeout_ = timeout if timeout is not None else self._timeout
return self._wait.element_have_similar_text(self.__web_element, text, timeout_)

def have_exact_text(self, text: str, timeout: TimeoutType):
def have_exact_text(self, text: str, timeout: TimeoutType = None):
"""True if an element has exactly provided text, and no other text.
Precise comparision, e.g. returns False if "some" == "this is some text"
:param text: exact text to search inside an element
:param timeout: time to wait for the condition.
"""
timeout_ = self._timeout
if timeout:
timeout_ = timeout
timeout_ = timeout if timeout is not None else self._timeout
return self._wait.element_to_have_exact_text(self.__web_element, text, timeout_)

def not_present_in_dom(self, timeout: TimeoutType):
def not_present_in_dom(self, timeout: TimeoutType = None):
"""True for an element to be stale or absent in DOM.
:param timeout: equal to the self.timeout if other not passed.
"""
timeout_ = self._timeout
if timeout:
timeout_ = timeout
timeout_ = timeout if timeout is not None else self._timeout
return self._wait.element_not_present(self.__web_element, timeout_)

def be_on_the_screen(self, timeout: TimeoutType):
def be_on_the_screen(self, timeout: TimeoutType = None):
"""True for an element is present on the screen (inside the viewport).
Checks if element's coordinates match viewport height and width.
Different from `to_be_visible` as `to_be_visible` checks element has size > 1px
and display is not `:none`.
:param timeout: equal to the self.timeout if other not passed.
"""
timeout_ = self._timeout
if timeout:
timeout_ = timeout
timeout_ = timeout if timeout is not None else self._timeout
return self._wait.element_to_be_in_viewport(self.__web_element, timeout_)
55 changes: 18 additions & 37 deletions selen_kaa/element/expectations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,120 +11,101 @@ class Expectations(ElementWaits):
Errors are handled by returning False.
"""

def be_visible(self, timeout: TimeoutType):
def be_visible(self, timeout: TimeoutType = None):
"""True when an element is visible on the html page.
:param timeout: time to wait element visibility.
"""
timeout_ = timeout if timeout else self._timeout
try:
return super().be_visible(timeout=timeout_)
return super().be_visible(timeout)
except TimeoutException:
return False

def be_invisible(self, timeout: TimeoutType):
def be_invisible(self, timeout: TimeoutType = None):
"""True if an element is not visible on the page.
:param timeout: time to wait element visibility.
"""
timeout_ = timeout if timeout else self._timeout
try:
return super().be_invisible(timeout=timeout_)
return super().be_invisible(timeout)
except TimeoutException:
return False

def have_class(self, expected_class: str, timeout: TimeoutType):
def have_class(self, expected_class: str, timeout: TimeoutType = None):
"""True when an element has a specific class.
:param expected_class: class_name for expected class (not css_selector).
:param timeout: time to wait for an element ho have a class name.
"""
timeout_ = timeout if timeout else self._timeout
try:
return super().have_class(expected_class, timeout=timeout_)
return super().have_class(expected_class, timeout)
except TimeoutException:
return False

def include_element(self, child_css_selector: str, timeout: TimeoutType):
def include_element(self, child_css_selector: str, timeout: TimeoutType = None):
"""True when an element gets a desired child element.
:param child_css_selector: a css selector for a child element.
:param timeout: time to wait for the condition.
"""
timeout_ = timeout if timeout else self._timeout
try:
return super().include_element(child_css_selector, timeout_)
return super().include_element(child_css_selector, timeout)
except TimeoutException:
return False

def contain_text(self, text: str, timeout: TimeoutType):
def contain_text(self, text: str, timeout: TimeoutType = None):
"""True if an element contains a provided text in its text attribute.
:param text: expected text.
:param timeout: time to wait for the condition.
"""
timeout_ = self._timeout
if timeout:
timeout_ = timeout
try:
return super().contain_text(text, timeout_)
return super().contain_text(text, timeout)
except TimeoutException:
return False

def have_similar_text(self, text: str, timeout: TimeoutType):
def have_similar_text(self, text: str, timeout: TimeoutType = None):
"""True if an element has a similar text in texts attribute.
Not precise comparision, e.g. returns True for:
"some" in "this is some text", " test\n" and "test", "TEST" and "test"
:param text: a text to compare for similarity.
:param timeout: time to wait for the condition.
"""
timeout_ = self._timeout
if timeout:
timeout_ = timeout
try:
return super().have_similar_text(text, timeout_)
return super().have_similar_text(text, timeout)
except TimeoutException:
return False

def have_exact_text(self, text: str, timeout: TimeoutType):
def have_exact_text(self, text: str, timeout: TimeoutType = None):
"""True if an element has exactly provided text, and no other text.
Precise comparision, e.g. returns False if "some" == "this is some text"
:param text: exact text to search inside an element
:param timeout: time to wait for the condition.
"""
timeout_ = self._timeout
if timeout:
timeout_ = timeout
try:
return super().have_exact_text(text, timeout_)
return super().have_exact_text(text, timeout)
except TimeoutException:
return False

def not_present_in_dom(self, timeout: TimeoutType):
def not_present_in_dom(self, timeout: TimeoutType = None):
"""True for an element to be stale or absent in DOM.
:param timeout: equal to the self.timeout if other not passed. :type: int
"""
timeout_ = self._timeout
if timeout:
timeout_ = timeout
try:
return super().not_present_in_dom(timeout_)
return super().not_present_in_dom(timeout)
except TimeoutException:
return False

def be_on_the_screen(self, timeout: TimeoutType):
def be_on_the_screen(self, timeout: TimeoutType = None):
"""True for an element is present on the screen (inside the viewport).
False if element's coordinates don't match viewport height and width.
:param timeout: equal to the self.timeout if other not passed.
"""
timeout_ = self._timeout
if timeout:
timeout_ = timeout
try:
return super().be_on_the_screen(timeout_)
return super().be_on_the_screen(timeout)
except TimeoutException:
return False
2 changes: 1 addition & 1 deletion selen_kaa/element/se_web_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ def get_class(self):
return self.web_element.get_attribute("class")

def __repr__(self):
return f"{self.web_element} with selector {self.selector}."
return f"Selen-kaa WebElement with selector `{self.selector}`."
15 changes: 8 additions & 7 deletions selen_kaa/waits.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class Wait:

DEFAULT_TIMEOUT = 4
PULL_FREQUENCY = 0.2

def __init__(self, webdriver: WebDriver):
self._webdriver: WebDriver = webdriver
Expand Down Expand Up @@ -60,7 +61,7 @@ def element_to_be_invisible(self, target: ElementType, timeout: TimeoutType = DE
def wrapped_webelement_disappears():
try:
# init web_element within wait's timeout, not web_element's
target.get_web_element_by_timeout(timeout)
target.get_web_element_by_timeout(self.PULL_FREQUENCY)
if target.web_element.is_displayed():
return False
# return True if element is not stale and is not displayed
Expand Down Expand Up @@ -88,11 +89,11 @@ def element_not_present(self, target: ElementType, timeout: TimeoutType = DEFAUL

def no_wrapped_webelement_in_dom():
try:
target.get_web_element_by_timeout(timeout)
target.get_web_element_by_timeout(self.PULL_FREQUENCY)
if target.web_element.is_enabled():
return False
# it might be unreached condition, but keep it for code consistency
return target
# return False even element isn't enabled, but still present
return False
except (NoSuchElementException, StaleElementReferenceException):
return target

Expand Down Expand Up @@ -396,15 +397,15 @@ def wait_fluently(condition: Callable, timeout: TimeoutType, err_msg: str):
:return: element if condition is True, else raises TimeoutException
"""
if not timeout:
if timeout is None:
timeout = 0
start_time = time.time()
while True:
if time.time() - start_time >= timeout:
raise TimeoutException(err_msg)
res = condition()
if res:
return res
if time.time() - start_time >= timeout:
raise TimeoutException(err_msg)
time.sleep(0.3)

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def get_requirements():

setup(
name="selen-kaa",
version="0.0.3.0",
version="0.0.3.1.0",
author="Viktor Grygorchuk",
author_email="[email protected]",
keywords=['Selenium', 'Test Automation'],
description="A lightweight wrapper around Selenium python repo.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/VicGrygorchyk/selen_kaa.git",
download_url="https://github.com/VicGrygorchyk/selen_kaa/archive/0.0.3.0.tar.gz",
download_url="https://github.com/VicGrygorchyk/selen_kaa/archive/0.0.3.1.0.tar.gz",
packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "test*"]),
install_requires=get_requirements(),
classifiers=[
Expand Down
Empty file.
Loading

0 comments on commit c6e21a1

Please sign in to comment.