Commit 70ce6a11 by Jonah Stanley

Merge pull request #333 from edx/jonahstanley/fix-flakey-video-test

Jonahstanley/fix flakey video test
parents b7eb287a d1661fa6
......@@ -99,7 +99,7 @@ def assert_policy_entries(expected_keys, expected_values):
def get_index_of(expected_key):
for counter in range(len(world.css_find(KEY_CSS))):
# Sometimes get stale reference if I hold on to the array of elements
key = world.css_find(KEY_CSS)[counter].value
key = world.css_value(KEY_CSS, index=counter)
if key == expected_key:
return counter
......@@ -108,7 +108,7 @@ def get_index_of(expected_key):
def get_display_name_value():
index = get_index_of(DISPLAY_NAME_KEY)
return world.css_find(VALUE_CSS)[index].value
return world.css_value(VALUE_CSS, index=index)
def change_display_name_value(step, new_value):
......
......@@ -61,7 +61,7 @@ def i_select_a_link_to_the_course_outline(step):
@step('I am brought to the course outline page$')
def i_am_brought_to_course_outline(step):
assert_in('Course Outline', world.css_find('.outline .page-header')[0].text)
assert_in('Course Outline', world.css_text('.outline .page-header'))
assert_equal(1, len(world.browser.windows))
......
......@@ -144,11 +144,12 @@ def log_into_studio(
world.is_css_present(signin_css)
world.css_click(signin_css)
login_form = world.browser.find_by_css('form#login_form')
login_form.find_by_name('email').fill(email)
login_form.find_by_name('password').fill(password)
login_form.find_by_name('submit').click()
def fill_login_form():
login_form = world.browser.find_by_css('form#login_form')
login_form.find_by_name('email').fill(email)
login_form.find_by_name('password').fill(password)
login_form.find_by_name('submit').click()
world.retry_on_exception(fill_login_form)
assert_true(world.is_css_present('.new-course-button'))
......@@ -223,7 +224,7 @@ def shows_captions(step, show_captions):
# Prevent cookies from overriding course settings
world.browser.cookies.delete('hide_captions')
if show_captions == 'does not':
assert world.css_find('.video')[0].has_class('closed')
assert world.css_has_class('.video', 'closed')
else:
assert world.is_css_not_present('.video.closed')
......@@ -232,7 +233,7 @@ def shows_captions(step, show_captions):
def save_button_disabled(step):
button_css = '.action-save'
disabled = 'is-disabled'
assert world.css_find(button_css)[0].has_class(disabled)
assert world.css_has_class(button_css, disabled)
def type_in_codemirror(index, text):
......
......@@ -162,7 +162,7 @@ def verify_date_or_time(css, date_or_time):
"""
Verifies date or time field.
"""
assert_equal(date_or_time, world.css_find(css).first.value)
assert_equal(date_or_time, world.css_value(css))
def i_see_the_set_dates():
......
......@@ -47,7 +47,7 @@ def other_user_login(_step, name):
@step(u's?he does( not)? see the course on (his|her) page')
def see_course(_step, doesnt_see_course, gender):
class_css = 'span.class-name'
all_courses = world.css_find(class_css)
all_courses = world.css_find(class_css, wait_time=1)
all_names = [item.html for item in all_courses]
if doesnt_see_course:
assert not _COURSE_NAME in all_names
......
......@@ -24,7 +24,7 @@ def add_update(_step, text):
@step(u'I should( not)? see the update "([^"]*)"$')
def check_update(_step, doesnt_see_update, text):
update_css = 'div.update-contents'
update = world.css_find(update_css)
update = world.css_find(update_css, wait_time=1)
if doesnt_see_update:
assert len(update) == 0 or not text in update.html
else:
......
......@@ -90,8 +90,8 @@ def add_assignment_type(step, new_name):
add_button_css = '.add-grading-data'
world.css_click(add_button_css)
name_id = '#course-grading-assignment-name'
f = world.css_find(name_id)[4]
f._element.send_keys(new_name)
new_assignment = world.css_find(name_id)[-1]
new_assignment._element.send_keys(new_name)
@step(u'I have populated the course')
......@@ -104,8 +104,7 @@ def populate_course(step):
def changes_not_persisted(step):
reload_the_page(step)
name_id = '#course-grading-assignment-name'
ele = world.css_find(name_id)[0]
assert(ele.value == 'Homework')
assert(world.css_value(name_id) == 'Homework')
@step(u'I see the assignment type "(.*)"$')
......@@ -118,8 +117,8 @@ def i_see_the_assignment_type(_step, name):
def get_type_index(name):
name_id = '#course-grading-assignment-name'
f = world.css_find(name_id)
for i in range(len(f)):
if f[i].value == name:
return i
all_types = world.css_find(name_id)
for index in range(len(all_types)):
if world.css_value(name_id, index=index) == name:
return index
return -1
......@@ -169,7 +169,7 @@ def edit_latex_source(step):
@step('my change to the High Level Source is persisted')
def high_level_source_persisted(step):
def verify_text(driver):
return world.css_find('.problem').text == 'hi'
return world.css_text('.problem') == 'hi'
world.wait_for(verify_text)
......@@ -177,7 +177,7 @@ def high_level_source_persisted(step):
@step('I view the High Level Source I see my changes')
def high_level_source_in_editor(step):
open_high_level_source()
assert_equal('hi', world.css_find('.source-edit-box').value)
assert_equal('hi', world.css_value('.source-edit-box'))
def verify_high_level_source_links(step, visible):
......
......@@ -70,7 +70,7 @@ def i_click_to_edit_section_name(_step):
def i_see_complete_section_name_with_quote_in_editor(_step):
css = '.section-name-edit input[type=text]'
assert world.is_css_present(css)
assert_equal(world.browser.find_by_css(css).value, 'Section with "Quote"')
assert_equal(world.css_value(css), 'Section with "Quote"')
@step('the section does not exist$')
......@@ -85,7 +85,7 @@ def i_see_a_release_date_for_my_section(_step):
css = 'span.published-status'
assert world.is_css_present(css)
status_text = world.browser.find_by_css(css).text
status_text = world.css_text(css)
# e.g. 11/06/2012 at 16:25
msg = 'Will Release:'
......
......@@ -7,12 +7,14 @@ from common import *
@step('I fill in the registration form$')
def i_fill_in_the_registration_form(step):
register_form = world.browser.find_by_css('form#register_form')
register_form.find_by_name('email').fill('robot+studio@edx.org')
register_form.find_by_name('password').fill('test')
register_form.find_by_name('username').fill('robot-studio')
register_form.find_by_name('name').fill('Robot Studio')
register_form.find_by_name('terms_of_service').check()
def fill_in_reg_form():
register_form = world.css_find('form#register_form')
register_form.find_by_name('email').fill('robot+studio@edx.org')
register_form.find_by_name('password').fill('test')
register_form.find_by_name('username').fill('robot-studio')
register_form.find_by_name('name').fill('Robot Studio')
register_form.find_by_name('terms_of_service').check()
world.retry_on_exception(fill_in_reg_form)
@step('I press the Create My Account button on the registration form$')
......
......@@ -92,7 +92,7 @@ def i_expand_a_section(step):
def i_see_the_span_with_text(step, text):
span_locator = '.toggle-button-sections span'
assert_true(world.is_css_present(span_locator))
assert_equal(world.css_find(span_locator).value, text)
assert_equal(world.css_value(span_locator), text)
assert_true(world.css_visible(span_locator))
......@@ -108,13 +108,13 @@ def i_do_not_see_the_span_with_text(step, text):
def all_sections_are_expanded(step):
subsection_locator = 'div.subsection-list'
subsections = world.css_find(subsection_locator)
for s in subsections:
assert_true(s.visible)
for index in range(len(subsections)):
assert_true(world.css_visible(subsection_locator, index=index))
@step(u'all sections are collapsed$')
def all_sections_are_collapsed(step):
subsection_locator = 'div.subsection-list'
subsections = world.css_find(subsection_locator)
for s in subsections:
assert_false(s.visible)
for index in range(len(subsections)):
assert_false(world.css_visible(subsection_locator, index=index))
......@@ -50,7 +50,7 @@ def i_click_to_edit_subsection_name(step):
def i_see_complete_subsection_name_with_quote_in_editor(step):
css = '.subsection-display-name-input'
assert world.is_css_present(css)
assert_equal(world.css_find(css).value, 'Subsection With "Quote"')
assert_equal(world.css_value(css), 'Subsection With "Quote"')
@step('I have set a release date and due date in different years$')
......@@ -69,7 +69,7 @@ def i_mark_it_as_homework(step):
@step('I see it marked as Homework$')
def i_see_it_marked__as_homework(step):
assert_equal(world.css_find(".status-label").value, 'Homework')
assert_equal(world.css_value(".status-label"), 'Homework')
############ ASSERTIONS ###################
......
......@@ -8,7 +8,7 @@ from lettuce import world, step
@step('when I view the video it does not have autoplay enabled')
def does_not_autoplay(_step):
assert world.css_find('.video')[0]['data-autoplay'] == 'False'
assert world.css_find('.video_control')[0].has_class('play')
assert world.css_has_class('.video_control', 'play')
@step('creating a video takes a single click')
......
......@@ -10,6 +10,7 @@ from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from lettuce.django import django_url
from nose.tools import assert_true
@world.absorb
......@@ -142,27 +143,32 @@ def id_click(elem_id):
@world.absorb
def css_fill(css_selector, text):
assert is_css_present(css_selector), "{} is not present".format(css_selector)
world.browser.find_by_css(css_selector).first.fill(text)
def css_fill(css_selector, text, index=0, max_attempts=5):
assert is_css_present(css_selector)
return world.retry_on_exception(lambda: world.browser.find_by_css(css_selector)[index].fill(text), max_attempts=max_attempts)
@world.absorb
def click_link(partial_text):
world.browser.find_link_by_partial_text(partial_text).first.click()
def click_link(partial_text, index=0, max_attempts=5):
return world.retry_on_exception(lambda: world.browser.find_link_by_partial_text(partial_text)[index].click(), max_attempts=max_attempts)
@world.absorb
def css_text(css_selector, index=0):
def css_text(css_selector, index=0, max_attempts=5):
# Wait for the css selector to appear
if world.is_css_present(css_selector):
try:
return world.browser.find_by_css(css_selector)[index].text
except StaleElementReferenceException:
# The DOM was still redrawing. Wait a second and try again.
world.wait(1)
return world.browser.find_by_css(css_selector)[index].text
return world.retry_on_exception(lambda: world.browser.find_by_css(css_selector)[index].text, max_attempts=max_attempts)
else:
return ""
@world.absorb
def css_value(css_selector, index=0, max_attempts=5):
# Wait for the css selector to appear
if world.is_css_present(css_selector):
return world.retry_on_exception(lambda: world.browser.find_by_css(css_selector)[index].value, max_attempts=max_attempts)
else:
return ""
......@@ -173,19 +179,18 @@ def css_html(css_selector, index=0, max_attempts=5):
Returns the HTML of a css_selector and will retry if there is a StaleElementReferenceException
"""
assert is_css_present(css_selector)
attempt = 0
while attempt < max_attempts:
try:
return world.browser.find_by_css(css_selector)[index].html
except:
attempt += 1
return ''
return world.retry_on_exception(lambda: world.browser.find_by_css(css_selector)[index].html, max_attempts=max_attempts)
@world.absorb
def css_visible(css_selector):
assert is_css_present(css_selector), "{} is not present".format(css_selector)
return world.browser.find_by_css(css_selector).visible
def css_has_class(css_selector, class_name, index=0, max_attempts=5):
return world.retry_on_exception(lambda: world.css_find(css_selector)[index].has_class(class_name), max_attempts=max_attempts)
@world.absorb
def css_visible(css_selector, index=0, max_attempts=5):
assert is_css_present(css_selector)
return world.retry_on_exception(lambda: world.browser.find_by_css(css_selector)[index].visible, max_attempts=max_attempts)
@world.absorb
......@@ -232,3 +237,18 @@ def click_tools():
@world.absorb
def is_mac():
return platform.mac_ver()[0] is not ''
@world.absorb
def retry_on_exception(func, max_attempts=5):
attempt = 0
while attempt < max_attempts:
try:
return func()
break
except WebDriverException:
world.wait(1)
attempt += 1
except:
attempt += 1
assert_true(attempt < max_attempts, 'Ran out of attempts to execute {}'.format(func))
......@@ -3,6 +3,7 @@
from lettuce import step, world
from django.contrib.auth.models import User
from nose.tools import assert_true
@step('I am an unactivated user$')
......@@ -19,8 +20,11 @@ def i_am_an_activated_user(step):
def i_submit_my_credentials_on_the_login_form(step):
fill_in_the_login_form('email', 'robot@edx.org')
fill_in_the_login_form('password', 'test')
login_form = world.browser.find_by_css('form#login-form')
login_form.find_by_name('submit').click()
def submit_login_form():
login_form = world.browser.find_by_css('form#login-form')
login_form.find_by_name('submit').click()
world.retry_on_exception(submit_login_form)
@step(u'I should see the login error message "([^"]*)"$')
......@@ -49,6 +53,8 @@ def user_is_an_activated_user(uname):
def fill_in_the_login_form(field, value):
login_form = world.browser.find_by_css('form#login-form')
form_field = login_form.find_by_name(field)
form_field.fill(value)
def fill_login_form():
login_form = world.browser.find_by_css('form#login-form')
form_field = login_form.find_by_name(field)
form_field.fill(value)
world.retry_on_exception(fill_login_form)
......@@ -121,7 +121,7 @@ def reset_problem(_step):
def press_the_button_with_label(_step, buttonname):
button_css = 'button span.show-label'
elem = world.css_find(button_css).first
assert_equal(elem.text, buttonname)
world.css_has_text(button_css, elem)
world.css_click(button_css)
......
......@@ -19,6 +19,7 @@ from capa.tests.response_xml_factory import OptionResponseXMLFactory, \
StringResponseXMLFactory, NumericalResponseXMLFactory, \
FormulaResponseXMLFactory, CustomResponseXMLFactory, \
CodeResponseXMLFactory
from nose.tools import assert_true
# Factories from capa.tests.response_xml_factory that we will use
......@@ -312,14 +313,15 @@ def assert_checked(problem_type, choices):
all_choices = ['choice_0', 'choice_1', 'choice_2', 'choice_3']
for this_choice in all_choices:
element = world.css_find(inputfield(problem_type, choice=this_choice))
if this_choice in choices:
assert element.checked
else:
assert not element.checked
def check_problem():
element = world.css_find(inputfield(problem_type, choice=this_choice))
if this_choice in choices:
assert element.checked
else:
assert not element.checked
world.retry_on_exception(check_problem)
def assert_textfield(problem_type, expected_text, input_num=1):
element = world.css_find(inputfield(problem_type, input_num=input_num))
assert element.value == expected_text
element_value = world.css_value(inputfield(problem_type, input_num=input_num))
assert element_value == expected_text
......@@ -11,10 +11,7 @@ def i_register_for_the_course(_step, course):
cleaned_name = TEST_COURSE_NAME.replace(' ', '_')
url = django_url('courses/%s/%s/%s/about' % (TEST_COURSE_ORG, course, cleaned_name))
world.browser.visit(url)
intro_section = world.browser.find_by_css('section.intro')
register_link = intro_section.find_by_css('a.register')
register_link.click()
world.css_click('section.intro a.register')
assert world.is_css_present('section.container.dashboard')
......
......@@ -6,20 +6,26 @@ from lettuce import world, step
@step('I fill in "([^"]*)" on the registration form with "([^"]*)"$')
def when_i_fill_in_field_on_the_registration_form_with_value(step, field, value):
register_form = world.browser.find_by_css('form#register-form')
form_field = register_form.find_by_name(field)
form_field.fill(value)
def fill_in_registration():
register_form = world.browser.find_by_css('form#register-form')
form_field = register_form.find_by_name(field)
form_field.fill(value)
world.retry_on_exception(fill_in_registration)
@step('I submit the registration form$')
def i_press_the_button_on_the_registration_form(step):
register_form = world.browser.find_by_css('form#register-form')
register_form.find_by_name('submit').click()
def submit_registration():
register_form = world.browser.find_by_css('form#register-form')
register_form.find_by_name('submit').click()
world.retry_on_exception(submit_registration)
@step('I check the checkbox named "([^"]*)"$')
def i_check_checkbox(step, checkbox):
world.browser.find_by_name(checkbox).check()
def check_box():
world.browser.find_by_name(checkbox).check()
world.retry_on_exception(check_box)
@step('I should see "([^"]*)" in the dashboard banner$')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment