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

后端 未结 4 886
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 23:34

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 som         


        
相关标签:
4条回答
  • 2021-02-15 23:56

    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
    
    0 讨论(0)
  • 2021-02-15 23:57

    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.

    0 讨论(0)
  • 2021-02-16 00:08

    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.

    0 讨论(0)
  • 2021-02-16 00:09

    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
    
    0 讨论(0)
提交回复
热议问题