Skip to content

Commit

Permalink
Merge pull request fecgov#440 from jmcarp/feature/robuster-tests
Browse files Browse the repository at this point in the history
Handle stale element errors in Chrome.
  • Loading branch information
Marco Segreto committed Aug 6, 2015
2 parents b08ec0e + 8664f18 commit 0313c54
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
10 changes: 5 additions & 5 deletions tests/selenium/base_test_class.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import time
import logging
import unittest

Expand All @@ -8,6 +7,7 @@

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException

from openfecwebapp.sauce import SauceClient

Expand Down Expand Up @@ -127,13 +127,13 @@ def check_filter(self, name, value, column, result, refresh=True, expand=True):
self.check_filter_results(column, result)

def check_filter_results(self, index, result):
# Wait for debounce interval
time.sleep(0.35)
utils.wait_for_ajax(self.driver)
values = [
utils.wait_for_event(self.driver, 'draw.dt', 'draw')
# Handle stale reference errors in Chrome
get_values = lambda: [
row.find_elements_by_tag_name('td')[index].text
for row in self.driver.find_elements_by_css_selector('tbody tr')
]
values = utils.try_until(get_values, (StaleElementReferenceException, ))
if callable(result):
for value in values:
self.assertTrue(result(value))
Expand Down
2 changes: 1 addition & 1 deletion tests/selenium/test_itemized.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def toggle_sort(driver, index):
table = driver.find_element_by_css_selector('table#results')
column = table.find_elements_by_tag_name('th')[index]
utils.try_until(lambda: column.click())
utils.wait_for_ajax(driver)
utils.wait_for_event(driver, 'draw.dt', 'draw')


class TestReceipts(SearchPageTestCase):
Expand Down
20 changes: 18 additions & 2 deletions tests/selenium/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,28 @@ def wait_for_ajax(driver, timeout=10, interval=0.1):
elapsed += interval


def wait_for_event(driver, name, label, timeout=10, interval=0.1):
elapsed = 0
driver.execute_script('window.{0} = false'.format(label))
driver.execute_script('''
$(document).on("{0}", function() {{
window.{1} = true;
}});
'''.format(name, label))
while True:
active = driver.execute_script('return window.{0} === true'.format(label))
if active or elapsed > timeout:
break
time.sleep(interval)
elapsed += interval
driver.execute_script('$(document).off("{0}")'.format(name))


def try_until(work, errors=(Exception, ), timeout=10, interval=0.1):
elapsed = 0
while True:
try:
work()
return
return work()
except errors:
time.sleep(interval)
elapsed += interval
Expand Down

0 comments on commit 0313c54

Please sign in to comment.