How do you perform javascript tests with Minitest, Capybara, Selenium? How do you perform javascript tests with Minitest, Capybara, Selenium? selenium selenium

How do you perform javascript tests with Minitest, Capybara, Selenium?


What :js flag is doing is very simple. It switches the current driver from default (rack-test) to another one that supports javascript execution (selenium, webkit). You can do the same thing in 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")  endend

Of course you can abstract this into a module for convenience:

require "minitest/autorun"module PossibleJSDriver  def require_js    Capybara.current_driver = :selenium  end  def teardown    super    Capybara.use_default_driver  endendclass MiniTest::Unit::TestCase  include PossibleJSDriverendclass 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")  endend


Hmm I noticed a couple lines in the docs that seem to say that the above can only be done in Rspec

However, if you are using RSpec or Cucumber, you may instead want to consider leaving the faster :rack_test as the default_driver, and marking only those tests that require a JavaScript-capable driver using :js => true or @javascript, respectively.


https://github.com/wojtekmach/minitest-metadata seems to have provided a solution to exactly this.

You can do the following:

describe "something under test" do  it "does not use selenium for this test" do    visit "/"    assert body.has_content?("Hello world")  end  it "does use selenium for this test", :js => true do    visit "/"    click_link "Hide" # a link that uses a javascript click event, for example    assert body.has_no_link?("Hide")  endend