Here’s a basic example of a Capybara test for logging into Sauce Demo. This assumes you’re using RSpec for your testing framework.
First, make sure you have Capybara and Selenium installed. You can add them to your Gemfile if you’re using Bundler:
|
|
Then run:
|
|
Now, here’s a simple test to automate the login process using Capybara and RSpec:
|
|
Explanation:
1. Capybara Setup: We’re using selenium_chrome as the driver to run the tests in the Chrome browser.
2. RSpec Test: The test navigates to the login page, fills in the username and password fields (standard_user and secret_sauce), clicks the login button, and then verifies that the user has been redirected to the “Products” page, indicating a successful login.
3. Capybara Matchers: expect(page).to have_content('Products') ensures that after login, the page contains the word “Products,” confirming we’re on the inventory page.
Running the Test:
To run this test, you can execute:
|
|
This test will open a browser window, interact with the page, and check the login functionality of the Sauce Demo website.