2016-04-21 60 views
1

我正在做一些使用Rspec和Watir的自動化測試。
我目前想要做的是驗證是否存在標題爲hidden的按鈕元素的屬性。因此,在僞代碼,我想這樣做:
Rspec;驗證一個元素的狀態,當屬性沒有值

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

是否有可能找到這種性質的屬性,還是他們總是需要這樣說hidden=hidden

回答

4

您可以使用內置的hidden?方法:

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

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

然後,您可以創建一個使用expectation來驗證一個RSpec例如:

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

而且expect(browser.button.hidden?).to be true是笨重。正如Justin Ko敏銳地指出的那樣,rspec以謂詞匹配器的形式提供了一些語法糖,以使它更簡潔:expect(browser.button).to be_hidden

+0

對於RSpec,[謂詞匹配器](https://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/built-in-matchers/predicate-matchers)可能更好 - 'expect(browser.button).to be_hidden' –

+0

@Justin Ko:Good call。試圖保持簡單。我已更新您的建議。 – orde

+0

謝謝,這個作品 - 我需要做一些重構來利用語法糖,但總體來說#hidden?作品! – kmancusi

相關問題