Improve code clarity and error messages for css selection

This commit is contained in:
Jay Zoldak
2013-09-16 11:09:54 -04:00
parent 2ff056df8c
commit 30b13d3cf1
9 changed files with 63 additions and 43 deletions

View File

@@ -88,7 +88,6 @@ def initial_setup(server):
"""
Launch the browser once before executing the tests.
"""
# from nose.tools import set_trace; set_trace()
world.absorb(settings.SAUCE.get('SAUCE_ENABLED'), 'SAUCE_ENABLED')
if not world.SAUCE_ENABLED:
@@ -166,15 +165,18 @@ def reset_databases(scenario):
xmodule.modulestore.django.clear_existing_modulestores()
# Uncomment below to trigger a screenshot on error
# @after.each_scenario
@after.each_scenario
def screenshot_on_error(scenario):
"""
Save a screenshot to help with debugging.
"""
if scenario.failed:
world.browser.driver.save_screenshot('/tmp/last_failed_scenario.png')
try:
output_dir = '{}/log'.format(settings.TEST_ROOT)
image_name = '{}/{}.png'.format(output_dir, scenario.name.replace(' ', '_'))
world.browser.driver.save_screenshot(image_name)
except WebDriverException:
LOGGER.error('Could not capture a screenshot')
@after.all
def teardown_browser(total):

View File

@@ -5,7 +5,7 @@ from lettuce import world
import time
import platform
from urllib import quote_plus
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import WebDriverException, TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
@@ -54,8 +54,11 @@ def wait_for_present(css_selector, timeout=30):
Throws an error if the wait_for time expires.
Otherwise this method will return None
"""
WebDriverWait(driver=world.browser.driver,
timeout=60).until(EC.presence_of_element_located((By.CSS_SELECTOR, css_selector,)))
try:
WebDriverWait(driver=world.browser.driver,
timeout=60).until(EC.presence_of_element_located((By.CSS_SELECTOR, css_selector,)))
except TimeoutException:
raise TimeoutException("Timed out waiting for {} to be present.".format(css_selector))
@world.absorb
@@ -65,8 +68,11 @@ def wait_for_visible(css_selector, timeout=30):
Throws an error if the wait_for time expires.
Otherwise this method will return None
"""
WebDriverWait(driver=world.browser.driver,
timeout=timeout).until(EC.visibility_of_element_located((By.CSS_SELECTOR, css_selector,)))
try:
WebDriverWait(driver=world.browser.driver,
timeout=timeout).until(EC.visibility_of_element_located((By.CSS_SELECTOR, css_selector,)))
except TimeoutException:
raise TimeoutException("Timed out waiting for {} to be visible.".format(css_selector))
@world.absorb
@@ -76,8 +82,11 @@ def wait_for_invisible(css_selector, timeout=30):
Throws an error if the wait_for time expires.
Otherwise this method will return None
"""
WebDriverWait(driver=world.browser.driver,
timeout=timeout).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, css_selector,)))
try:
WebDriverWait(driver=world.browser.driver,
timeout=timeout).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, css_selector,)))
except TimeoutException:
raise TimeoutException("Timed out waiting for {} to be invisible.".format(css_selector))
@world.absorb
@@ -89,8 +98,11 @@ def wait_for_clickable(css_selector, timeout=30):
"""
# Sometimes the element is clickable then gets obscured.
# In this case, pause so that it is not reported clickable too early
WebDriverWait(world.browser.driver,
timeout=timeout).until(EC.element_to_be_clickable((By.CSS_SELECTOR, css_selector,)))
try:
WebDriverWait(world.browser.driver,
timeout=timeout).until(EC.element_to_be_clickable((By.CSS_SELECTOR, css_selector,)))
except TimeoutException:
raise TimeoutException("Timed out waiting for {} to be clickable.".format(css_selector))
@world.absorb
@@ -114,7 +126,8 @@ def css_click(css_selector, index=0, wait_time=30):
This method will return True if the click worked.
"""
wait_for_clickable(css_selector, timeout=wait_time)
assert world.css_find(css_selector)[index].visible
assert_true(world.css_find(css_selector)[index].visible,
msg="Element {}[{}] is present but not visible".format(css_selector, index))
# Sometimes you can't click in the center of the element, as
# another element might be on top of it. In this case, try
@@ -146,7 +159,8 @@ def css_click_at(css_selector, index=0, x_coord=10, y_coord=10, timeout=5):
'''
wait_for_clickable(css_selector, timeout=timeout)
element = css_find(css_selector)[index]
assert element.visible
assert_true(element.visible,
msg="Element {}[{}] is present but not visible".format(css_selector, index))
element.action_chains.move_to_element_with_offset(element._element, x_coord, y_coord)
element.action_chains.click()
@@ -158,7 +172,7 @@ def id_click(elem_id):
"""
Perform a click on an element as specified by its id
"""
css_click('#%s' % elem_id)
css_click('#{}'.format(elem_id))
@world.absorb