Mayshar
Mayshar

Reputation: 190

Having trouble telling ruby/watir webdriver to check for two fail conditions

I am trying to write a simple cucumber step where it checks to see if one of two phrases are on the page, but my attempts at if statements have failed. Here is the step i currently have

Then(/^I should see a pdf preview of the magazine$/) do
  @browser.windows.last.use
   @browser.text.should_not include "Page not found"

But, ideally, I want the step to check and see if the text "Page not found" is present, if so fail the test. If "page not found" is NOT present, then check to see if the text "Forbidden" is present, if so, fail the test. If neither phrase is present, then pass the test.

Where i think I am stumbling, is I dont know how to tell it, to stop and fail the test.

Ideally, I would think it would look like this...

if text "Page not found" visible?
   fail test
elseif text "Forbidden" visible?
   fail test
else
   pass test

But I dont know how to put that into ruby/watir-webdriver code

Do I just have to make it throw exceptions if the test fails? If its that easy, will that fail the test, or pass it but give me a message and how do I make it pass if neither phrase is visible?

Upvotes: 0

Views: 685

Answers (1)

Justin Ko
Justin Ko

Reputation: 46836

A Cucumber step will stop and fail the test as soon as any of the assertions fail (or an exception is raised). Conversely, a Cucumber step will pass if all assertions pass (or there are no exceptions raised).

As a result, you can simply do:

Then(/^I should see a pdf preview of the magazine$/) do
  @browser.windows.last.use
  @browser.text.should_not include "Page not found" # first assertion
  @browser.text.should_not include "Forbidden" # second assertion
end

With this step:

  • In the situation where "Page not found" is displayed, the first assertion will fail. As a result the step will be marked as Fail (with the message that the text includes "Page not found"). The rest of the Cucumber step will not be executed - ie the second assertion will not be tested.
  • In the situation where "Forbidden" is displayed, the first assertion will pass so Cucumber will continue to execute the line. When the second assertion is run, it will fail and therefore cause the step to fail (with the message that the text includes "Forbidden").
  • In the situation where neither text is present, both assertions will pass. Since Cucumber was able to complete the step without any failed assertions (or exceptions), the step will be marked as Pass.
  • In the situation where both texts are displayed, the step will Fail, but the error message will only indicate the first failure - ie "Page not found" was displayed.

Upvotes: 1

Related Questions