我正在尝试在Cucumber和Capybara的Ruby on Rails中测试我的应用程序的功能:当您单击"删除"按钮时,有一个确认说"你确定吗?然后它应该单击"确定"。
起初我只是尝试
Given('I accept the popup') do
click_button('OK')
end
然后黄瓜抛出一个错误:
Unable to find button "OK" (Capybara::ElementNotFound)
然后我尝试了:
Given('I accept the popup') do
page.driver.browser.switch_to.alert.accept
end
如如何测试与黄瓜的确认对话框中所述?黄瓜抛出此错误:
undefined method `switch_to' for #<Capybara::RackTest::Browser:0x0000000009241c20> (NoMethodError)
然后我尝试在我的"test.feature"中的场景之前添加"@javascript",例如:
@javascript
Scenario: Admin can manage scales
Given I am on Scales page
Given I destroy a scale
Given I accept the popup
Then I should see "Scale deleted"
然后黄瓜抛出一个错误:
Unable to find Mozilla geckodriver. Please download the server from https://github.com/mozilla/geckodriver/releases and place it somewhere on your PATH. More info at https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver.
我很困惑。我是否配置了错误的环境?
我的宝石文件:
group :test do
gem 'shoulda-matchers'
gem 'simplecov', :require => false
gem 'rails-controller-testing', '1.0.2'
gem 'minitest-reporters', '1.1.14'
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
gem 'capybara'
gem 'launchy'
gem 'selenium-webdriver'
gem 'cucumber-rails', :require => false
gem 'cucumber-rails-training-wheels'
end
我的web_steps.rb:
require 'uri'
require 'cgi'
require 'selenium-webdriver'
当使用不支持JS(RackTest(的驱动程序进行测试时,显然您无法测试JS触发的系统模式。通过将@javascript标签添加到测试中,您已经告诉Capybara交换为使用支持JS(硒驱动程序(的驱动程序。
您得到的下一个错误是不言自明的 - 您的系统中没有安装硒与 Firefox 通信所需的geckodriver
- 如果您已将驱动程序配置为与 Chrome 通信,则需要chromedriver
。 安装它们的最简单方法是将webdrivers
添加到测试宝石中 - https://github.com/titusfortner/webdrivers#usage
一旦你解决了这个问题,那么你就需要编写你的步骤,以便他们最终运行代码。
page.accept_confirm do
click_button('delete') # The action that causes the confirm modal to appear
end
如果您还想验证确认模式中的消息,它将是
page.accept_confirm "Are you sure? do
click_button('delete')
end