Search code examples
ruby-on-railsrubyif-statementab-testing

If and Page.Should have_css in Ruby


I am a newbie in Ruby and I have a problem with if condition and page.should have when I programmed the A/B Test for log in function.

This is my code:

if (page.should have_css("body.bg-b")) || (page.should have_css("body.bg-c")) 
    #do something           
else
    #do something
end

It works fine in the if condition. My problem is in the case the page doesn't have bg-b or bg-c, it should go to else condition, right, but it stopped running and said

Capybara::ExpectationNotMet: expected to find css "body.bg-b" but there were no matches

Is there something wrong with this code?

Thanks


Solution

  • The keyword "should" seems you're asserting something.

    If you simply want to make a IF, you should write something like this :

    if (page.has_css?("body.bg-b")) || (page.has_css?("body.bg-c")) 
       # Check something with should
    else
       # Check something else with should
    end
    

    It you want to check the page have a css or the other one, you should write

    (page.has_css?("body.bg-b")) || (page.has_css?("body.bg-c")).should == true
    

    Or

    if !page.has_css?("body.bg-b")
      page.should have_css("body.bg-c")
    end