Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

There are a lot of examples on how to perform javascript tests with Capybara/Selenium/Rspec in which you can write a test like so:

it "does something", :js => true do
  ...
end

However with minitest you can't pass a second parameter to instruct selenium to perform the test.

Does anyone have any ideas on how this can be done?

like image 721
chris Avatar asked Apr 13 '11 20:04

chris


People also ask

What is Capybara in testing?

Capybara is a web-based test automation software that simulates scenarios for user stories and automates web application testing for behavior-driven software development. It is written in the Ruby programming language. Capybara. Developer(s)

What is Capybara selenium?

Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and comes with Rack::Test and Selenium support built in. WebKit is supported through an external gem.

What are rails system tests?

System tests allow you to test your application the way your users experience it and help you test your JavaScript as well. System tests inherit from Capybara and perform in browser tests for your application. Fixtures are a way of organizing test data; they reside in the fixtures directory.


1 Answers

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")
  end
end

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
  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
like image 185
Simon Perepelitsa Avatar answered Sep 25 '22 12:09

Simon Perepelitsa