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?
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)
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With