Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find_by with 0 second wait_time only checks once #739

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions splinter/driver/webdriver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,38 @@ def find_by_text(self, text):
)


def _find(self, finder, selector):
"""Search for elements. Returns a list of results.

Arguments:
finder: The function to use for the element search.
selector: The search query.

Returns:
list

"""
elements = None
elem_list = []

try:
elements = finder(selector)
if not isinstance(elements, list):
elements = [elements]

except (
NoSuchElementException,
StaleElementReferenceException,
):
# This exception is sometimes thrown if the page changes
# quickly
pass

if elements:
elem_list = [self.element_class(element, self) for element in elements]

return elem_list

def find_by(self, finder, selector, original_find=None, original_query=None, wait_time=None):
"""Wrapper for finding elements.

Expand All @@ -232,32 +264,24 @@ def find_by(self, finder, selector, original_find=None, original_query=None, wai
ElementList

"""
elements = None
elem_list = []

func_name = getattr(getattr(finder, _meth_func), _func_name)
find_by = original_find or func_name[func_name.rfind("_by_") + 4 :]
query = original_query or selector

wait_time = wait_time or self.wait_time
end_time = time.time() + wait_time
while time.time() < end_time:
try:
elements = finder(selector)
if not isinstance(elements, list):
elements = [elements]

except (
NoSuchElementException,
StaleElementReferenceException,
):
# This exception is sometimes thrown if the page changes
# quickly
pass
# Zero second wait time means only check once
if wait_time == 0:
elem_list = _find(self, finder, selector)
else:
wait_time = wait_time or self.wait_time
end_time = time.time() + wait_time

while time.time() < end_time:
elem_list = _find(self, finder, selector)

if elements:
elem_list = [self.element_class(element, self) for element in elements]
break
if elem_list:
break

return ElementList(elem_list, find_by=find_by, query=query)

Expand Down
21 changes: 13 additions & 8 deletions tests/is_element_present.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ def test_is_element_present_by_xpath_returns_false_if_element_is_not_present(sel
self.assertTrue(self.browser.is_element_not_present_by_xpath("//h4"))

def test_is_element_not_present_by_xpath_returns_false_if_element_is_present(self):
"should is element not present by xpath returns false if element is present"
"""should is_element_not_present_by_xpath returns False if element is present"""
self.browser.find_by_css(".add-async-element").click()
self.assertFalse(self.browser.is_element_not_present_by_xpath("//h4"))
self.browser.find_by_xpath("//h4")
assert not self.browser.is_element_not_present_by_xpath("//h4")

def test_is_element_not_present_by_xpath_using_a_custom_wait_time(self):
"should is element not present by xpath verify if element is not present using a custom wait time"
Expand Down Expand Up @@ -84,9 +85,10 @@ def test_is_element_not_present_by_tag_using_a_custom_wait_time(self):
self.assertTrue(self.browser.is_element_not_present_by_tag("h4", wait_time=3))

def test_is_element_not_present_by_tag_returns_false_if_element_is_present(self):
"should is element not present by tag returns false if element is present"
"""should is_element_not_present_by_tag returns False if element is present"""
self.browser.find_by_css(".add-async-element").click()
self.assertFalse(self.browser.is_element_not_present_by_tag("h4"))
self.browser.find_by_tag("h4")
assert not self.browser.is_element_not_present_by_tag("h4")

def test_is_element_present_by_text(self):
"should is element present by text verify if element is present"
Expand Down Expand Up @@ -165,9 +167,11 @@ def test_is_element_not_present_by_id_using_a_custom_wait_time(self):
)

def test_is_element_not_present_by_id_returns_false_if_element_is_present(self):
"should is element not present by id returns False if element is present"
"""should is_element_not_present_by_id returns False if element is present"""
self.browser.find_by_css(".add-async-element").click()
self.assertFalse(self.browser.is_element_not_present_by_id("async-header"))
self.browser.find_by_id("async-header")
assert not self.browser.is_element_not_present_by_id("async-header")


def test_is_element_present_by_name(self):
"should is element present by name verify if element is present"
Expand Down Expand Up @@ -196,6 +200,7 @@ def test_is_element_not_present_by_name_using_a_custom_wait_time(self):
)

def test_is_element_not_present_by_name_returns_false_if_element_is_present(self):
"should is element not present by name returns false if element is present"
"""should is_element_not_present_by_name returns False if element is present"""
self.browser.find_by_css(".add-async-element").click()
self.assertFalse(self.browser.is_element_not_present_by_name("async-input"))
self.browser.find_by_name("async-input")
assert not self.browser.is_element_not_present_by_name("async-input")