Skip to content

Commit

Permalink
Merge pull request #93 from neetjn/issue-92-neetjn
Browse files Browse the repository at this point in the history
Issue 92 - Elements Stale Checks
  • Loading branch information
neetjn committed Apr 11, 2018
2 parents 0b40290 + a74d5e1 commit 45db29f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
35 changes: 31 additions & 4 deletions pyscc/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from pyscc.resource import Resource


ELEMENTS_STALE_WAIT_TIME = 3


# pylint: disable=too-many-public-methods
class Element(Resource):
"""
Expand Down Expand Up @@ -440,6 +443,10 @@ def __find_elements(self):
return self.controller.browser.find_elements_by_css_selector(self.selector) \
or self.controller.browser.find_elements_by_xpath(self.selector)

def __wait_elements_not_stale(self, timeout):
# pylint: disable=line-too-long
self.controller.wait(timeout, condition=lambda: [element.text for element in self.get()])

def fmt(self, **kwargs):
"""
Used to format selectors.
Expand All @@ -465,14 +472,19 @@ def count(self):
"""
return len(self.get())

def text(self, raw=False):
def text(self, raw=False, check_stale_element=False):
"""
Get list of element text values.
:param raw: Extract inner html from element.
:type raw: bool
:param check_stale_element: Ensure retrieved WebElement instances are finished mounting.
:type check_stale_element: bool
:return: [string, ...], None
"""
if check_stale_element:
# wait in the event element list is actively loading
self.__wait_elements_not_stale(ELEMENTS_STALE_WAIT_TIME)
found = self.get()
if found:
collection = []
Expand All @@ -485,22 +497,32 @@ def text(self, raw=False):
return collection
return []

def value(self):
def value(self, check_stale_element=False):
"""
Get list of input element values.
:param check_stale_element: Ensure retrieved WebElement instances are finished mounting.
:type check_stale_element: bool
:return: [string, ...], None
"""
if check_stale_element:
# wait in the event element list is actively loading
self.__wait_elements_not_stale(ELEMENTS_STALE_WAIT_TIME)
return [self.controller.js.get_value(element) for element in self.get()]

def get_attribute(self, attribute):
def get_attribute(self, attribute, check_stale_element=False):
"""
Used to fetch list of elements attributes.
:param attribute: Attribute of elements to target.
:type attribute: string
:param check_stale_element: Ensure retrieved WebElement instances are finished mounting.
:type check_stale_element: bool
:return: [(None, bool, int, float, string), ...]
"""
if check_stale_element:
# wait in the event element list is actively loading
self.__wait_elements_not_stale(ELEMENTS_STALE_WAIT_TIME)
return [self.controller.js.get_attribute(element, attribute) for element in self.get()]

def set_attribute(self, attribute, value):
Expand All @@ -517,14 +539,19 @@ def set_attribute(self, attribute, value):
self.controller.js.set_attribute(element, attribute, value)
return self

def get_property(self, prop):
def get_property(self, prop, check_stale_element=False):
"""
Used to fetch list of elements properties.
:param prop: Property of elements to target.
:type prop: string
:param check_stale_element: Ensure retrieved WebElement instances are finished mounting.
:type check_stale_element: bool
:return: None, bool, int, float, string
"""
if check_stale_element:
# wait in the event element list is actively loading
self.__wait_elements_not_stale(ELEMENTS_STALE_WAIT_TIME)
return [self.controller.js.get_property(element, prop) for element in self.get()]

def set_property(self, prop, value):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_component_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def test_elements_wrapper_text(self):
"""test elements wrapper text aggregation"""
self.app.wait(timeout=1) # wait for transitions
self.assertEqual(len(self.tasks.text()), 3)
self.assertEqual(len(self.tasks.text(check_stale_element=True)), 3)
for task in self.tasks.text():
self.assertIn('2017', task)
for task in self.tasks.text(raw=True):
Expand All @@ -213,6 +214,7 @@ def test_elements_wrapper_attributes(self):
self.assertEqual(len(attributes), 3)
for attr in attributes:
self.assertEqual(attr, 'barfoo')
self.assertEqual(len(self.tasks.get_attribute('foobar', check_stale_element=True)), 3)

def test_elements_wrapper_properties(self):
"""test elements wrapper property aggregation and specification"""
Expand All @@ -223,3 +225,4 @@ def test_elements_wrapper_properties(self):
self.assertEqual(len(properties), 3)
for prop in properties:
self.assertEqual(prop, 'barfoo')
self.assertEqual(len(self.tasks.get_property('foobar', check_stale_element=True)), 3)

0 comments on commit 45db29f

Please sign in to comment.