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

后端 未结 4 908
没有蜡笔的小新
没有蜡笔的小新 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
    

提交回复
热议问题