2010-05-13 100 views
5

我使用rails 2.3.5,這就是我所做的。我安裝了最新的黃瓜,黃瓜欄和水豚。水豚硒和JavaScript Destroy

rails demo 
cd demo 
ruby script/generate cucumber --rspec --capybara 
ruby script/generate feature post title:string body:text published:boolean 
ruby script/generate scaffold post title:string body:text published:boolean 
rake db:migrate 
rake cucumber 

所有的測試都通過了。現在我想用Javascript來測試。

此時這場景看起來像

Scenario: Delete post 
    Given the following posts: 
     |title|body|published| 
     |title 1|body 1|false| 
     |title 2|body 2|true| 
     |title 3|body 3|false| 
     |title 4|body 4|true| 
    When I delete the 3rd post 
    Then I should see the following posts: 
     |Title|Body|Published| 
     |title 1|body 1|false| 
     |title 2|body 2|true| 
     |title 4|body 4|true| 

我在上面添加@javascript。

現在,當我運行耙黃瓜,然後我看到一個確認頁面。但是,我點擊之前什麼也沒有發生

我需要做什麼以確保自動點擊並進行測試。

回答

8

嘛同類一個黑客,但我覺得現在它的唯一的出路:

When /^I confirm a js popup on the next step$/ do 
    page.evaluate_script("window.alert = function(msg) { return true; }") 
    page.evaluate_script("window.confirm = function(msg) { return true; }") 
end 

你必須把這個步驟就在觸發確認彈出窗口中的一個的前面(如下鏈接) 。它將修改標準警報並確認行爲始終返回true。所以你不必自己點擊「OK」按鈕。

+0

討厭必須這樣做,但它的工作原理! – 2010-09-06 19:36:47

0

感謝史蒂芬爲他解決,這裏就是我的修改,以便它讀取好一點:

When /^I follow "([^"]*)" and click OK$/ do |text| 
    page.evaluate_script("window.alert = function(msg) { return true; }") 
    page.evaluate_script("window.confirm = function(msg) { return true; }") 
    When %{I follow "#{text}"} 
end 
2

我實現了對Tobias的解決方案的變化。

我想有步驟,像When I follow the "Delete" link for customer "Alice Angry",所以我有以下幾點:

When /^(.*) and (?:|I)click "OK"$/ do |step| 
    click_ok_after { When step } 
end 

module JavascriptHelpers 
    def click_ok_after 
    begin 
     page.evaluate_script("window.alert = function(msg) { return true; }") 
     page.evaluate_script("window.confirm = function(msg) { return true; }") 
    rescue Capybara::NotSupportedByDriverError 
     # do nothing: we're not testing javascript 
    ensure 
     yield 
    end 
    end 
end 
World(JavascriptHelpers) 

完整的解釋可以在博客文章我寫在這裏http://davidsulc.com/blog/2011/07/10/cucumber-tweaks/被發現(包括用於測試內容的有益的步驟定義在HTML容器內)。