215 Browser automation with Capybara and Selenium WebDriver [2d]

Updated . Posted . Visible to the public.

We use Selenium WebDriver Show archive.org snapshot with Capybara for end-to-end tests that need a real browser.

Important

Work on this lesson in advisor mode.

Learning goals

  • You can explain how Capybara drives a browser, how its drivers differ (e.g. rack_test vs. Selenium), and when a feature spec needs a real browser.
  • You can run a feature spec in a real browser, headless or visible, and watch what it does.
  • You can explain why browser tests are flaky by nature β€” asynchronous JavaScript, timing, and the app and the browser running in separate threads or processes β€” and how Capybara's waiting and retrying counters that.
  • You can diagnose and fix a flaky feature spec with our standard toolbox, e.g. capybara-lockstep and waiting finders instead of sleep.
  • You can handle browser dialogs, file downloads and screenshots in a feature spec.
  • You can explain how the app and the browser share one test database and how it is reset between examples.
  • You know what running tests in parallel and on a CI server requires from your test setup.

Resources

Read what's new to you, skim what's familiar, skip what you already master. Stop when you can meet the learning goals.

Your agent can also generate an overview, a tutorial or an explanation for anything here, tailored to what you already know. Just ask.

Capybara

Flaky tests and our toolbox

Dialogs, downloads, screenshots

Exercises

Watch it run

Your counter spec from Working with the DOM runs in a real browser: its js: true metadata switches Capybara from Rack::Test to the Selenium driver, which remote-controls Chrome. By default Chrome runs headless, so you never see it.

Run the counter spec with NO_HEADLESS=1 geordi rspec spec/features/... and watch Capybara visit the page and click the button. Get someone to help you if no browser opens.

Dialogs, downloads, screenshots

  • Ask before a movie is deleted: show a confirm() dialog with your own JavaScript. Write a js: true spec for both outcomes, using accept_confirm and dismiss_confirm.
  • Click "Export as CSV" from Basic file uploads and image versions in a js: true spec, and check the content of the downloaded file. Your MovieDB tells Chrome to save downloads to /tmp (see spec/support/capybara.rb).
  • Break a js: true spec on purpose. Find the screenshot and the HTML page that capybara-screenshot saved to tmp/capybara, and open both.

Learn from real flaky fixes

Our colleagues have fixed many flaky specs in the makandra cards app, which you cloned in an earlier lesson. Find these fixes with git log -i --grep=flak. Most of them change only a few lines.

Pick three commits with different causes. For each, explain:

  • What made the spec flaky?
  • Why did it pass most of the time?
  • Why does the fix work?

Then find out how the app sets up capybara-lockstep, and explain what it makes Capybara wait for.

Profile picture of Henning Koch
Henning Koch
Last edit
Michael LeimstΓ€dtner
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2015-09-04 13:59)