Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only run selenium test if previous selenium test fails

I have several 'it' blocks in my selenium test file (using Ruby and rspec) that test various portions of my web application. Each 'it' block stops executing and goes to the next 'it' block if any of the conditions or code fails.

  • Is there a way to run an 'it' block only if the previous fails or call a function to react to the failed test?
  • Is there a better way to accomplish what I am wanting to do that doesn't involve an 'it' block?

Example 'it' block

it "should load example.com" do
  page.open("http://example.com")
  page.wait_for_page_to_load(25)
end
like image 932
Mike Grace Avatar asked Dec 05 '25 00:12

Mike Grace


1 Answers

I don't like my current solution and think there is a better way to accomplish this, but for now...

  • wrap original test code that was in the 'it' block in a begin rescue block
  • put code to respond to failure in the rescue section

.

it "should load example.com" do
  begin
    page.open("http://example.com")
    page.wait_for_page_to_load(25)
    "wow".should == "cool"
  rescue Exception => e
    // code that responds to failed test
  end
end

Why I don't like this

  • I feel dirty writing test code like this (it feels wrong!)
  • The rspec reports a pass unless the rescue code also fails
like image 180
Mike Grace Avatar answered Dec 06 '25 17:12

Mike Grace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!