Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber + selenium fails randomly

My selenium tests like to fails randomly. As an example I have this scenario

Scenario: I should be able to edit a user
  Given I created a user with the login "[email protected]"
  And I am viewing the user with login "[email protected]"
  Then I should see "Edit this user"
  When I click "Edit this user"
  Then I should be editing the user with login "[email protected]"
  When I press "Update"
  Then I should be viewing the user with login "[email protected]"
  And I should see "User was successfully updated."

This, along with the others, work fine using the basic webrat :rails mode. In selenium, the line

Then I should be editing the user with login "[email protected]"

and

Then I should be viewing the user with login "[email protected]"

fail randomly, in that sometimes the first fails, other times the seconds fails. Using the website by hand results in the correct operation, and like I said, the webrat/rails mode works fine.

Rails 2.2.2 Cucumber 0.3.7 selenium 1.1.14 (fixed to work with FF3)

Some extra info:

Scenario: I should be able to edit a user                         # features/admin_priviledges.feature:26
  Given I created a user with the login "[email protected]"        # features/step_definitions/global_steps.rb:18
  And I am viewing the user with login "[email protected]"         # features/step_definitions/global_steps.rb:60
  Then I should see "Edit this user"                              # features/step_definitions/webrat_steps.rb:93
  When I click "Edit this user"                                   # features/step_definitions/webrat_steps.rb:18
  Then I should be editing the user with login "[email protected]" # features/step_definitions/global_steps.rb:66
    expected: "/users/8/edit",
         got: "/users/8" (using ==)
    Diff:
    @@ -1,2 +1,2 @@
    -/users/8/edit
    +/users/8
     (Spec::Expectations::ExpectationNotMetError)
    features/admin_priviledges.feature:31:in `Then I should be editing the user with login "[email protected]"'
  When I press "Update"                                           # features/step_definitions/webrat_steps.rb:14
  Then I should be viewing the user with login "[email protected]" # features/step_definitions/global_steps.rb:66
  And I should see "User was successfully updated." 

Then /^I should be (editing|viewing) the (\w+) with (\w+) "([^\"]*)"$/ do |action,model,field,value|
  func = make_func(action,model)
  m = find_model_by_field_and_value(model,field,value)
  URI.parse(current_url).path.should == eval("#{func}(m)")
end

Those are the relevant steps. The press "Update" one is a standard webrat step (click_button)

like image 344
Daniel Huckstep Avatar asked Jun 08 '09 17:06

Daniel Huckstep


2 Answers

Changing a webrat step from

When /^I press "([^\"]*)"$/ do |button|
  click_button(button)
end

to

When /^I press "([^\"]*)"$/ do |button|
  click_button(button)
  selenium.wait_for_page_to_load
end

it works. Telling selenium to wait fixes!

like image 86
Daniel Huckstep Avatar answered Oct 31 '22 20:10

Daniel Huckstep


Have you looked at the timing of the requests? My gut says it is that selenium is moving too fast.

Can you post your cucumber steps and the error messages and log details for each failure?

like image 36
srboisvert Avatar answered Oct 31 '22 20:10

srboisvert