有很多关于如何使用Capybara / Selenium / Rspec执行
javascript测试的示例,您可以在其中编写如下测试:
it "does something",:js => true do ... end
但是,对于minitest,您无法传递第二个参数来指示selenium执行测试.
有没有人对如何做到这一点有任何想法?
解决方法
什么:js flag正在做的很简单.它将当前驱动程序从默认(机架测试)切换到支持javascript执行的另一个驱动程序(selenium,webkit).你可以用minitest做同样的事情:
require "minitest/autorun" class WebsiteTest < MiniTest::Unit::TestCase def teardown super Capybara.use_default_driver end def test_with_javascript Capybara.current_driver = :selenium visit "/" click_link "Hide" assert has_no_link?("Hide") end def test_without_javascript visit "/" click_link "Hide" assert has_link?("Hide") end end
当然,为方便起见,您可以将其抽象为模块:
require "minitest/autorun" module PossibleJSDriver def require_js Capybara.current_driver = :selenium end def teardown super Capybara.use_default_driver end end class MiniTest::Unit::TestCase include PossibleJSDriver end class WebsiteTest < MiniTest::Unit::TestCase def test_with_javascript require_js visit "/" click_link "Hide" assert has_no_link?("Hide") end def test_without_javascript visit "/" click_link "Hide" assert has_link?("Hide") end end