我怎么能避免“不建议在全球范围内包括Capybara :: DSL!”用黄瓜?

时间:2014-08-08 14:07:15

标签: ruby cucumber capybara

将我的Cucumber测试从Selenium-Webdriver切换到Capybara后,一切都按预期工作。但我一直收到这个警告:

"including Capybara::DSL in the global scope is not recommended!"

谷歌搜索此错误消息显示了一堆结果,但所有这些都是针对RSpec;实质上,他们建议将include Capybara::DSL移动到RSpec配置(参见例如Why do I get "including Capybara::DSL in the global scope is not recommended!")。我试图用我的Cucumber配置做同样的事,但无济于事(参见下面的方法部分)。

我当前的代码

特征/ youtube.feature:

Feature: Searching for videos 

  Scenario: Searching for videos 
  Given I go to the YouTube web site
    And I search for "text adventure" 
   Then I should see the text "GET LAMP: The Text Adventure Documentary"

特征/步骤/ youtube_steps.rb:

require 'capybara'
require 'capybara/dsl'
require 'rspec'

include RSpec::Expectations
include Capybara::DSL

Capybara.default_driver = :selenium
Capybara.run_server = false

When(/^I search for "(.*?)"$/) do |value|
  page.fill_in("search_query", :with => value)
  page.click_button("search-btn")
end

Then(/^I should see the text "(.*?)"$/) do |value|
  page.should have_content(value)
end

Given(/^I go to the YouTube web site$/) do
  Capybara.app_host = "http://www.youtube.com" 
  page.visit("")
end

方法我尝试过

  • include Capybara::DSL放入Before挂钩中(结果:黄瓜找不到像page那样的Capybara特定方法
  • config.extend Capybara::DSL放入BeforeConfiguration挂钩中(结果:黄瓜找不到像page那样的Capybara特定方法
  • 使用include Capybara代替include Capybara::DSL(结果:另一个警告,告诉我这已经过时,我应该使用include Capybara::DSL

我应该把include Capybara::DSL放在哪里去除警告?

1 个答案:

答案 0 :(得分:4)

您希望将Capybara :: DSL包含在Cucumber步骤中而不是全局范围内。 Cucumber允许您使用World

执行此操作

Capybara :: DSL通过使用以下行包含在世界中:

World(Capybara::DSL)

如@engineersmnky所述,您可能只想:

require 'capybara/cucumber'

这会将Capybara :: DSL添加到世界,以及RSpec :: Expectations。它还设置了几个钩子。您可以通过查看capybara/cucumber.rb file来确切了解这会做什么。