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

Try and use it

Your forked MovieDB should already include a feature spec that uses a real browser: it carries js: true metadata, which switches Capybara from Rack::Test to the Selenium driver. Add js: true to some of your other feature specs to test it yourself.

When you run such a spec with NO_HEADLESS=1 geordi rspec spec/features/..., you should see a browser opening. Get someone to help you if this does not work.

Some of your features might now fail. You might find solutions below:

A walk through the forest

Read the source of Capybara's Finders#find / Finders#all Show archive.org snapshot and Base#synchronize Show archive.org snapshot . Why does find wait for an element, while all doesn't? Why do specs that use all tend to become flaky?

Recognize common culprits

Have a look at this code:

class User < ApplicationRecord
  def self.find_by_email(email)
    @cached_users ||= {}
    @cached_users[email] ||= User.find_by_email!(email)
  end
end

Could it cause your tests to become flaky?

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)