You can switch the WebDriver even during the test by using the method Capybara.current_driver =
.
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.
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