Commit b7f745c7 by Jonah Stanley

Merge pull request #228 from edx/jonahstanley/fix-flakey-tests

Jonahstanley/fix flakey tests
parents 0ef4390a 281f9003
......@@ -58,7 +58,7 @@ def css_find(css, wait_time=5):
@world.absorb
def css_click(css_selector, index=0, attempts=5, success_condition=lambda: True):
def css_click(css_selector, index=0, max_attempts=5, success_condition=lambda: True):
"""
Perform a click on a CSS selector, retrying if it initially fails.
......@@ -72,7 +72,7 @@ def css_click(css_selector, index=0, attempts=5, success_condition=lambda: True)
assert is_css_present(css_selector)
attempt = 0
result = False
while attempt < attempts:
while attempt < max_attempts:
try:
world.css_find(css_selector)[index].click()
if success_condition():
......@@ -90,6 +90,38 @@ def css_click(css_selector, index=0, attempts=5, success_condition=lambda: True)
@world.absorb
def css_check(css_selector, index=0, max_attempts=5, success_condition=lambda: True):
"""
Checks a check box based on a CSS selector, retrying if it initially fails.
This function handles errors that may be thrown if the component cannot be clicked on.
However, there are cases where an error may not be thrown, and yet the operation did not
actually succeed. For those cases, a success_condition lambda can be supplied to verify that the check worked.
This function will return True if the check worked (taking into account both errors and the optional
success_condition).
"""
assert is_css_present(css_selector)
attempt = 0
result = False
while attempt < max_attempts:
try:
world.css_find(css_selector)[index].check()
if success_condition():
result = True
break
except WebDriverException:
# Occasionally, MathJax or other JavaScript can cover up
# an element temporarily.
# If this happens, wait a second, then try again
world.wait(1)
attempt += 1
except:
attempt += 1
return result
@world.absorb
def css_click_at(css, x_cord=10, y_cord=10):
'''
A method to click at x,y coordinates of the element
......
......@@ -83,9 +83,12 @@ def click_on_section(step, section):
world.css_click(section_css)
subid = "ui-accordion-accordion-panel-" + str(int(section) - 1)
subsection_css = 'ul[id="%s"]> li > a' % subid
subsection_css = 'ul.ui-accordion-content-active[id=\'%s\'] > li > a' % subid
prev_url = world.browser.url
changed_section = lambda: prev_url != world.browser.url
#for some reason needed to do it in two steps
world.css_find(subsection_css).click()
world.css_click(subsection_css, success_condition=changed_section)
@step(u'I click on subsection "([^"]*)"$')
......
......@@ -135,12 +135,10 @@ def action_button_present(_step, buttonname, doesnt_appear):
@step(u'the button with the label "([^"]*)" does( not)? appear')
def button_with_label_present(step, buttonname, doesnt_appear):
button_css = 'button span.show-label'
elem = world.css_find(button_css).first
if doesnt_appear:
assert_not_equal(elem.text, buttonname)
world.browser.is_text_not_present(buttonname, wait_time=5)
else:
assert_equal(elem.text, buttonname)
world.browser.is_text_present(buttonname, wait_time=5)
@step(u'My "([^"]*)" answer is marked "([^"]*)"')
......
......@@ -142,34 +142,34 @@ def answer_problem(problem_type, correctness):
elif problem_type == "multiple choice":
if correctness == 'correct':
inputfield('multiple choice', choice='choice_2').check()
world.css_check(inputfield('multiple choice', choice='choice_2'))
else:
inputfield('multiple choice', choice='choice_1').check()
world.css_check(inputfield('multiple choice', choice='choice_1'))
elif problem_type == "checkbox":
if correctness == 'correct':
inputfield('checkbox', choice='choice_0').check()
inputfield('checkbox', choice='choice_2').check()
world.css_check(inputfield('checkbox', choice='choice_0'))
world.css_check(inputfield('checkbox', choice='choice_2'))
else:
inputfield('checkbox', choice='choice_3').check()
world.css_check(inputfield('checkbox', choice='choice_3'))
elif problem_type == 'radio':
if correctness == 'correct':
inputfield('radio', choice='choice_2').check()
world.css_check(inputfield('radio', choice='choice_2'))
else:
inputfield('radio', choice='choice_1').check()
world.css_check(inputfield('radio', choice='choice_1'))
elif problem_type == 'string':
textvalue = 'correct string' if correctness == 'correct' else 'incorrect'
inputfield('string').fill(textvalue)
world.css_fill(inputfield('string'), textvalue)
elif problem_type == 'numerical':
textvalue = "pi + 1" if correctness == 'correct' else str(random.randint(-2, 2))
inputfield('numerical').fill(textvalue)
world.css_fill(inputfield('numerical'), textvalue)
elif problem_type == 'formula':
textvalue = "x^2+2*x+y" if correctness == 'correct' else 'x^2'
inputfield('formula').fill(textvalue)
world.css_fill(inputfield('formula'), textvalue)
elif problem_type == 'script':
# Correct answer is any two integers that sum to 10
......@@ -181,8 +181,8 @@ def answer_problem(problem_type, correctness):
if correctness == 'incorrect':
second_addend += random.randint(1, 10)
inputfield('script', input_num=1).fill(str(first_addend))
inputfield('script', input_num=2).fill(str(second_addend))
world.css_fill(inputfield('script', input_num=1), str(first_addend))
world.css_fill(inputfield('script', input_num=2), str(second_addend))
elif problem_type == 'code':
# The fake xqueue server is configured to respond
......@@ -281,11 +281,11 @@ def add_problem_to_course(course, problem_type, extraMeta=None):
def inputfield(problem_type, choice=None, input_num=1):
""" Return the <input> element for *problem_type*.
""" Return the css selector for `problem_type`.
For example, if problem_type is 'string', return
the text field for the string problem in the test course.
*choice* is the name of the checkbox input in a group
`choice` is the name of the checkbox input in a group
of checkboxes. """
sel = ("input#input_i4x-edx-model_course-problem-%s_2_%s" %
......@@ -299,7 +299,7 @@ def inputfield(problem_type, choice=None, input_num=1):
assert world.is_css_present(sel)
# Retrieve the input element
return world.browser.find_by_css(sel)
return sel
def assert_checked(problem_type, choices):
......@@ -312,7 +312,7 @@ def assert_checked(problem_type, choices):
all_choices = ['choice_0', 'choice_1', 'choice_2', 'choice_3']
for this_choice in all_choices:
element = inputfield(problem_type, choice=this_choice)
element = world.css_find(inputfield(problem_type, choice=this_choice))
if this_choice in choices:
assert element.checked
......@@ -321,5 +321,5 @@ def assert_checked(problem_type, choices):
def assert_textfield(problem_type, expected_text, input_num=1):
element = inputfield(problem_type, input_num=input_num)
element = world.css_find(inputfield(problem_type, input_num=input_num))
assert element.value == expected_text
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