How do you perform javascript tests with Minitest,

2020-06-16 05:41发布

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?

4条回答
▲ chillily
2楼-- · 2020-06-16 05:57

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")
  end
end
查看更多
Fickle 薄情
3楼-- · 2020-06-16 06:09

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

To test JavaScript on firefox from terminal quickly with Rails 4 and selenium-webdriver, you need to do the 4 steps below:

  1. Add gem to your Gemfile

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

  2. Install gem

    bundle install

  3. Generate test case (Ref: Official Guide)

    bin/rails generate integration_test your_case_name

  4. Start to write test code in your test case. Actually you can write it in ruby without Capybara, to write case with Capybara you can refer to Capybara at Github

Minitest 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 it seems lack of resources with respect to how to work with selenium-webdriver, CI server (Jenkins) and Rails minitest smoothly, I have created a simple project and hope it can help any one getting started quickly and easily: Rails Selenium Working Case

Also appreciate the comments that let me can make better answer.

查看更多
我只想做你的唯一
4楼-- · 2020-06-16 06:11

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.

查看更多
看我几分像从前
5楼-- · 2020-06-16 06:20

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
查看更多
登录 后发表回答