在Ruby on Rails中使用Cucumber/Capybara测试弹出窗口

3

我正在编写测试,以测试确认和取消确认弹出窗口的功能。然而,我收到以下错误:

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

我正在使用prickle gem,我已将其添加到我的Gemfile中,如下所示:

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

我按照这里的说明,在我的Gemfile中添加了几行代码,以包含prickle。最初我将其作为World do添加到代码中,就像github页面上展示的那样,但是由于World被用了两次(另一次在cucumber库中),所以这样会导致错误。以下是我添加到env.rb文件中的代码:

require 'prickle/capybara'    # require

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

World(Prickle_helper)

任何想法,是什么导致了我的错误?谢谢!
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接