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

There are many examples of how to run javascript tests with Capybara / Selenium / Rspec , in which you can write the test as follows:

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

However, with minitest you cannot pass a second parameter to instruct selenium to run a test.

Does anyone have any ideas on how to do this?

+7
source share
4 answers

Hmm, I noticed a couple of 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 want to leave the faster one :rack_test as default_driver and mark only those tests that require a driver that supports JavaScript using :js => true or @javascript , respectively.

0
source

What the flag does :js is very simple. It switches the current default driver (rack test) to another one that supports javascript (selenium, webkit). You can do the same 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 
+17
source

To update some information, there is now a more powerful selenium-webdriver with Ruby Bindings .

To quickly test JavaScript on firefox from a terminal using Rails 4 and selenium-webdriver, you need to follow these 4 steps:

  • Add Gem to Your Gemfile

    gem 'selenium-webdriver' gem 'minitest-rails'

  • Set gemstone

    package install

  • Generate Test Case (Ref: Official Guide )

    bin / rails generate Integration_test your_case_name

  • Start writing test code in a test case. In fact, you can write it in ruby ​​without Capybara, to write a case with Capybara, you can turn to Capybara at Github

Minimum sample:

 # create a driver driver = Selenium::WebDriver.for :firefox # navigate to a page driver.navigate.to 'url_to_page' # get element with class box box = driver.find_element(:css, '.box') # check width # get width by execute JavaScript assert_equal 100, driver.execute_script('return $(arguments[0]).width()', box) 

Currently there are not enough resources to work with selenium-webdriver, CI server (Jenkins) and Rails minitest smoothly, I created a simple project and I hope that it will help to get started quickly and easily: Rails Selenium workstation

Also rate comments that allow me to better respond.

0
source

https://github.com/wojtekmach/minitest-metadata seems to provide a solution to just that.

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

All Articles