在Ruby on Rails中测试带有黄瓜/水豚的弹出窗口

时间:2012-10-29 01:32:32

标签: ruby-on-rails ruby ruby-on-rails-3 cucumber capybara

我正在编写测试来测试肯定和解雇一个确认弹出工作。但是,我收到错误:

And I dismiss popup                                     # features/step_definitions/web_steps.rb:74
  undefined method `switch_to' for #<Capybara::RackTest::Browser:0xa6f0dec> (NoMethodError)
  ./features/step_definitions/web_steps.rb:75:in `/^I confirm popup$/'
  features/add_session.feature:121:in `And I confirm popup'

运行我的测试时。

我的.features文件中的步骤如下所示:

And I press "Delete"
And I confirm popup

在web_steps.rb中,我定义了以下内容:

When /^I confirm popup$/ do
  #page.evaluate_script('window.confirm = function() { return true; }')
  #page.click('OK')
  page.driver.browser.switch_to.alert.accept
  #popup.confirm 
end

When /^I dismiss popup$/ do
  page.driver.browser.switch_to.alert.dismiss
  #page.evaluate_script('window.confirm = function() { return true; }')
  #page.click('Cancel')
  #popup.dismiss
end

我正在使用我在Gemfile中包含的prickle gem:

group :test do
  gem 'cucumber-rails'
  gem 'prickle'
end

我按照概述here向我的Gemfile添加了几行以包含刺。我最初有它像世界一样,就像它们在github页面上显示的那样,但是由于World被使用了两次(另一次在黄瓜库中),这给了我错误。以下是我添加到env.rb文件中的行:

require 'prickle/capybara'    # require

module Prickle_helper
  include Capybara::DSL
  include Prickle::Capybara  # include  Prickle
end

World(Prickle_helper)

关于可能导致我的错误的任何想法?谢谢!

1 个答案:

答案 0 :(得分:0)

我在想要使用selenium的方法之前省略了@javascript。例如:

@javascript
Scenario: Delete the class
  Given I am on the home page
  And I press "Delete"
  And I confirm popup
  Then I am on the homepage
  And I should not see the deleted class