Change driver to Rack :: Test if driver wants to POST in Selenium test

Conclusion

You can switch the WebDriver even during the test by using the method Capybara.current_driver =.

problem

If you specify WebDriver using Selenium as the driver of Capybara and execute feature spec, you can actually run the browser and test through WebDriver.

There are times when you want to make a set of scenarios that are completed by operating the OAuth authorization server with a browser and then making an API call, such as obtaining an access token in a test. However, like the actual browser, POST and PUT cannot be issued as a request suddenly.

solution

If you switch the WebDriver to : rack_test (Rack :: Test) usingCapybara.current_driver =in the middle of the example, Rack :: Test has a method that can issue various HTTP requests, so You can issue POST and PUT in the example.

spec/rails_helper.rb


Capybara.default_driver = :selenium

spec/features/authorization_spec.rb


# `js: true`Since it is a test that specifies, the driver is initially`:selenium`It has become
# cf. https://github.com/teamcapybara/capybara#using-capybara-with-rspec
scenario 'User authorizes and client gets access token', js: true do
  #Write browser operation
  visit oauth_authorization_path
  fill_in id: 'id', with: id
  fill_in id: 'password', with: password
  click_button 'Login'

  # ...

  #here`page.driver.post`Throws NoMethodError

  #Rack the driver::Change to Test
  Capybara.current_driver = :rack_test

  #Rack that can be drawn from page::The test driver object
  #Since it has various HTTP methods, POST can be issued.
  page.driver.post oauth_token_path # ...

  # ...
end

Recommended Posts

Change driver to Rack :: Test if driver wants to POST in Selenium test
How to test file upload screen in Spring + Selenium
How to implement UI automated test using image comparison in Selenium
I want to change the value of Attribute in Selenium of Ruby
How to change the maximum and maximum number of POST data in Spark
Selenium Web Driver with user-data-dir set to crash in Chrome headless mode
How to change app name in rails
How to filter JUnit Test in Gradle
Change List <Optional <T >> to Optional <List <T >> in Java
If you want to satisfy the test coverage of private methods in JUnit
Volume that wants to use a lot of logical operators in if statements