DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • How to Submit a Post to DZone
  • Mocking Kafka for Local Spring Development
  • A Deep Dive into Tracing Agentic Workflows (Part 1)
  • Retesting Best Practices for Agile Teams: A Quick Guide to Bug Fix Verification

Capybara Setup With Chrome Headless Webdriver on CircleCI With Ruby on Rails Project

Selenium WebDriver tutorial for Rails project and RSpec with Capybara gems.

By 
Artur Trzop user avatar
Artur Trzop
·
Jan. 14, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.7K Views

Join the DZone community and get the full member experience.

Join For Free

I’ve been using Capybara-WebKit for a long time, but, while switching from CircleCI 1.0 to CircleCI 2.0, I had some problems using it on the CI.

This triggered me to try Chrome Headless with Selenium Webdriver. I will show you how to configure Circle CI 2.0 and your Ruby on Rails project to use Capybara, Selenium, and Chrome headless together.

Add Capybara and Selenium WebDriver

Let’s add Capybara and Selenium WebDriver gems to Gemfile:

Ruby
 




x


 
1
# Gemfile
2
group :development, :test do
3
  gem 'capybara'
4
  gem 'selenium-webdriver'
5
end


and run bundle install.

If you already have the gems in your Gemfile then ensure you have the latest version with bundle update capybara selenium-webdriver.

If you want to make sure Capybara feature specs will work on your development machine, run the following:

$ brew install chromedriver

If your feature specs fail, then upgrade the driver because you may have installed old one.

$ brew upgrade chromedriver

Configure Capybara

Add a config file for Capybara:

Ruby
 




xxxxxxxxxx
1
18


 
1
# spec/support/config/capybara.rb
2
JS_DRIVER = :selenium_chrome_headless
3

          
4
Capybara.default_driver = :rack_test
5
Capybara.javascript_driver = JS_DRIVER
6
Capybara.default_max_wait_time = 2
7

          
8
RSpec.configure do |config|
9
  config.before(:each) do |example|
10
    Capybara.current_driver = JS_DRIVER if example.metadata[:js]
11
    Capybara.current_driver = :selenium if example.metadata[:selenium]
12
    Capybara.current_driver = :selenium_chrome if example.metadata[:selenium_chrome]
13
  end
14

          
15
  config.after(:each) do
16
    Capybara.use_default_driver
17
  end
18
end


Ensure you load config files from the spec/support directory:

Ruby
 




xxxxxxxxxx
1


 
1
# spec/rails_helper.rb
2

          
3
# The following line is provided for convenience purposes. It has the downside
4
# of increasing the boot-up time by auto-requiring all files in the support
5
# directory. Alternatively, in the individual `*_spec.rb` files, manually
6
# require only the support files necessary.
7
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }


Example Feature Spec

We can create an example feature spec to test if everything works:

Ruby
 




xxxxxxxxxx
1
26


 
1
# spec/features/home_spec.rb
2
feature 'Homepage Features' do
3
  before { visit root_path }
4

          
5
  # it won't run js code but it is fast
6
  it do
7
    expect(page).to have_content 'Hello World'
8
  end
9

          
10
  # it will run js code
11
  it '', :js do
12
    expect(page).to have_content 'Hello World'
13
  end
14

          
15
  # it will open Firefox
16
  # remove x from xit to run the test in Firefox on your machine to preview
17
  xit '', :selenium do
18
    expect(page).to have_content 'Hello World'
19
  end
20

          
21
  # it will open Chrome
22
  # remove x from xit to run the test in Chrome on your machine to preview
23
  xit '', :selenium_chrome do
24
    expect(page).to have_content 'Hello World'
25
  end
26
end


Now run tests on your development machine with bin/rspec spec/features/home_spec.rb or bundle exec rspec spec/features/home_spec.rb.

Configure CircleCI 2.0 to Run Chrome Headless

Here is an example of the .circleci/config.ymlfile:

YAML
 




xxxxxxxxxx
1
55


 
1
# .circleci/config.yml
2
version: 2
3
jobs:
4
  build:
5
    parallelism: 1
6
    working_directory: ~/project-name
7
    docker:
8
      # this is important to use proper image with browsers support
9
      - image: circleci/ruby:2.4.2-node-browsers
10
        environment:
11
          PGHOST: 127.0.0.1
12
          PGUSER: project-name
13
          RAILS_ENV: test
14
      - image: circleci/postgres:9.4.12-alpine
15
        environment:
16
          POSTGRES_DB: project-name_test
17
          POSTGRES_PASSWORD: ""
18
          POSTGRES_USER: project-name
19
      - image: redis:3.2.7
20
    steps:
21
      - checkout
22

          
23
      # Restore bundle cache
24
      - type: cache-restore
25
        # remove space between { {
26
        key: project-name-{ { checksum "Gemfile.lock" }}
27

          
28
      # Bundle install dependencies
29
      - run: bundle install --path vendor/bundle
30

          
31
      # Store bundle cache
32
      - type: cache-save
33
        # remove space between { {
34
        key: project-name-{ { checksum "Gemfile.lock" }}
35
        paths:
36
          - vendor/bundle
37

          
38
      # Prepare .env, useful if you use dotenv gem
39
      - run: cp .env.example .env
40

          
41
      # Database setup
42
      - run: bundle exec rake db:create
43
      - run: bundle exec rake db:schema:load
44

          
45
      # Run rspec in parallel
46
      - type: shell
47
        command: |
48
          bundle exec rspec --profile 10 \
49
                            --format RspecJunitFormatter \
50
                            --out /tmp/test-results/rspec.xml \
51
                            --format progress
52

          
53
      # Save artifacts
54
      - type: store_test_results
55
        path: /tmp/test-results


Speed up Your Tests With Circle CI Parallelisation

If your feature specs are very long, you can save some time by running multiple parallel CI nodes. For instance, you can set it to 6 in .circleci/config.yml and use dynamic RSpec specs allocation across CI nodes with the knapsack_pro gem and Queue Mode to get optimal test suite split to save as much time as possible.

YAML
 




xxxxxxxxxx
1
18


 
1
# .circleci/config.yml
2
jobs:
3
  build:
4
    parallelism: 6
5

          
6
  steps:
7
    # some tests that are not balanced and executed only on first CI node
8
    - run: case $CIRCLE_NODE_INDEX in 0) npm test ;; esac
9

          
10
    # auto-balancing CI build time execution to be flat and optimal (as fast as possible).
11
    # Queue Mode does dynamic tests allocation so the previous not balanced run command won't
12
    # create a bottleneck on the CI node
13
    - run:
14
      name: RSpec via knapsack_pro Queue Mode
15
      command: |
16
        # export word is important here!
17
        export RAILS_ENV=test
18
        bundle exec rake "knapsack_pro:queue:rspec[--format documentation]"


You can learn how RSpec test suite parallelisation works in this one minute video.

Summary

Now you are good to push your code to a repository and see how your Capybara feature specs work with Chrome Headless on CircleCI 2.0.

Capybara (software)

Published at DZone with permission of Artur Trzop. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook