2009-10-27 73 views
1

下面的作品就好了:檢查元素不存在使用WebRat

Then /^I should see a button called "([^\"]*)"$/ do |name| 
    response.should have_selector("li") do |li| 
    li.should have_selector("a") do |a| 
     a.should contain(name) 
    end 
    end 
end 

現在,我需要這樣的步驟來補充它:

Then /^I should not see a button called "([^\"]*)"$/ do |name| 
    [snip...] 
end 

標準「而且我不應該看「並不令人滿意,因爲它會在頁面的任何位置拾取目標短語 - 而不僅僅是在按鈕內。我特別需要檢查沒有包含目標文本的按鈕。

我的第一反應是去嘗試這樣的:

Then /^I should see a button called "([^\"]*)"$/ do |name| 
    response.should have_selector("li") do |li| 
    li.should have_selector("a") do |a| 
     a.should_not contain(name) 
    end 
    end 
end 

當然,只要有網頁上的任何按鍵,不包括目標文本將通過,即使有也是一個,但或多個包含目標文本的按鈕。

您的想法?

非常感謝,

史蒂文。

回答

1

問題解決了!我給每一個控制類「控制」,然後寫 此步驟定義:

Then /^I should not see a control called "([^\"]*)"$/ do |name| 
    response.should have_selector("a.control", :content => name, :count 
=> 0) 
end 

當然,這個假設我很高興給他們所有的一類 「控制」 ......

s。

+0

很高興你解決了它! – Rodreegez 2009-10-30 12:32:40

2

您可以使用assert_not_contain(也許not_contain,但我懷疑它不在rdoc)。

a.should assert_not_contain(name) 
+0

是的,assert_not_contain(name)是你想要的方法,如果你採取這種方法。 – Rodreegez 2009-10-27 11:52:10

+0

它工作。非常感激。 – 2009-10-29 09:48:10

+0

其實,並不是那麼快... 但我不能將代碼粘貼到評論框中,所以我在WebRat Google Group中繼續。 – 2009-10-29 10:11:24

0

是你的按鈕實際上是鏈接?對於按鈕(其中有一個提交值),我使用:

assert_have_selector "input[type=submit][value=\"#{text}\"]" 

和:

assert_have_no_selector "input[type=submit][value=\"#{text}\"]" 

心連心。

+0

是的,你是對的。從技術上講,他們是鏈接。我只是叫他們按鈕,因爲這是他們如何出現在界面中。 – 2009-10-29 09:47:28

+0

你讓我思考。在我的所有測試中,我只是將「按鈕」更改爲「控制」。 – 2009-10-29 09:54:57