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

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

提交回复
热议问题