kmancusi kmancusi - 7 months ago 14
Ruby Question

Rspec; verifying an element state when the attribute that doesn't have a value

I am doing some automation testing using Rspec and Watir.

What I currently would like to do is verify the presence of a button element's attribute titled

hidden
. So in pseudo-code I would like to do this:


Find button element; press click
Verify button element now has another attribute titled "hidden"
perform further actions


Is it possible to find attributes of this nature, or do they always need to say something like
hidden=hidden
?

Answer

You can use the built-in hidden? method:

<input type="submit" value="button">
browser.button.hidden?
#=> false

<input type="submit" value="button" hidden>
browser.button.hidden?
#=> true

Then, you can create an rspec example that uses an expectation to validate:

describe "Button" do
  it "should be hidden" do
    expect(browser.button.hidden?).to be true
  end
end

And expect(browser.button.hidden?).to be true is clunky. As Justin Ko astutely points out, rspec provides some syntactic sugar in the form of predicate matchers to make it cleaner: expect(browser.button).to be_hidden.