• Stars
    star
    28
  • Rank 882,216 (Top 18 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Chrome driver for Capybara using Chrome's remote debugging protocol

Capybara::Chrome

Use Capybara to drive Chrome in headless mode via the debugging protocol.

Installation

Add this line to your application's Gemfile:

gem 'capybara-chrome'

And then execute:

$ bundle

Or install it yourself as:

$ gem install capybara-chrome

Using Docker

Add this line to your Dockerfile to install Chrome:

  RUN curl -sL -o chrome-stable.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb ; dpkg -i chrome-stable.deb ; apt-get install -fy; rm -rf /var/lib/apt/lists/*

In my experience, headless Chrome cannot be run as root, so you need to run rspec as an unprivileged user and use this flag with docker run

docker run --security-opt seccomp=unconfined

Usage

Capybara.javascript_driver = :chrome
Capybara::Chrome.configuration.chrome_port = 9222 # optional

The standard port for the debugging protocol is 9222. Visit localhost:9222 in a Chrome tab to watch the tests execute. Note, the port will be random by default.

Using thin

I like using thin instead of WEBrick as my Capybara server. It's a little faster, gives me greater control of logging, shows errors, and allows me to disable signal handlers. Below are my settings:

Capybara.register_server :thin do |app, port, host|
  require "rack/handler/thin"
  Thin::Logging.silent = false
  # Thin::Logging.debug = true # uncomment to see request and response codes
  # Thin::Logging.trace = true # uncomment to see full requests/responses
  Rack::Handler::Thin.run(app, Host: host, Port: port, signals: false)
end
Capybara.server = :thin

Debugging

Use byebug instead of binding.pry when debugging a test. The Pry debugger tends to hang when visit is called.

Using the repl

You can use the capybara-chrome browser without providing a rack app. This can be helpful in debugging.

[2] pry(main)> driver = Capybara::Chrome::Driver.new(nil, port:9222); driver.start; browser = driver.browser
[3] pry(main)> browser.visit "http://google.com"
=> true
[4] pry(main)> browser.current_url
=> "https://www.google.com/?gws_rd=ssl"
[5] pry(main)>

Further, you can run a local netcat server and point the capybara-chrome browser to it to see the entire request that's being sent.

Terminal one contains the browser:

[1] pry(main)> driver = Capybara::Chrome::Driver.new(nil, port:9222); driver.start; browser = driver.browser
[2] pry(main)> browser.header "x-foo", "bar"
[3] pry(main)> browser.visit "http://localhost:8000"

Terminal two prints the request

$ while true; do { echo -e "HTTP/1.1 200 OK \r\n"; echo "hi"; } | nc -l 8000; done
GET / HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/68.0.3440.106 Safari/537.36
x-foo: bar
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate

Videos

A video showing capybara-chrome running in a browser tab

Preview of visualizing capybara-chrome

A video demonstrating debugging an rspec test with byebug

Preview of debugging capybara-chrome

A video showing capybara-chrome running against a netcat backend

Preview of debugging capybara-chrome with netcat

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/sandro/capybara-chrome.

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

specjour

distributed rspec & cucumber via bonjour
Ruby
213
star
2

ephemeral_response

Automatic recording, replaying, and expiry of web responses.
Ruby
96
star
3

ruby-fsevent

Abandoned, use Guard. A native extension exposing the OS X FSEvents API. RubyCocoa not required.
Ruby
45
star
4

bench_press

Abandoned. simple ruby benchmarking
Ruby
43
star
5

runkeeper

API client for Runkeeper (http://runkeeper.com)
Ruby
23
star
6

capify_slicehost

Capistrano script to setup an Ubuntu slice from slicehost and install a rails stack
10
star
7

githubber

github bookmarklet which lets you test markdown for a readme page
JavaScript
10
star
8

yaba

Yet Another Base App - a rails baseapp including email and openid authentication, rspec, haml, and aasm
Ruby
10
star
9

rlastfm

ruby wrapper/scraper for last.fm
Ruby
8
star
10

on-webby

turriate.com on webby
Ruby
7
star
11

assembly_line

Organize your test suite's object graphs in semantic blocks
Ruby
7
star
12

metamuse

Search for music metadata
Ruby
7
star
13

annotrack

Summarize activity on pivotal tracker for a given user on a specific day.
Ruby
6
star
14

homeward

Rakefile and generators to get my new apps up and running
Ruby
6
star
15

sandro.github.com

Internet.stubs(sandro)
6
star
16

mseeqr

Ruby wrapper for seeqpod's mobile service.
Ruby
6
star
17

srg

Sandro's rails generator
Ruby
6
star
18

tagooru

Ruby wrapper for the tagoo search engine
Ruby
6
star
19

railify

Edge Rails generator with configured git repo and rspec, haml and resource_controller gems.
6
star
20

culerity_fakeweb

Example rails app using fakeweb to return known responses in culerity
Ruby
6
star
21

html2textile

(alpha) Scripts and Library for converting HTML to Textile
5
star
22

outlandmall-razor

Community script for the outland mall
HTML
4
star
23

reaper

Harvest library for destroying invoices, time entries, expenses, projects, clients, and employees.
2
star
24

imigrate

Interface-driven migrations in Go. A simple tool to run database migrations for sql drivers that don't conform to database/sql.
Go
2
star
25

hash_proxy

SPIKE: A distributed, persistent, key/value store providing language-agnostic storage-engines.
Ruby
2
star
26

pass.rb

RSpec environment preloader for Rails. Similar to spork, and spin.
Ruby
1
star
27

phaser-lessons

programming lessons via phaser https://sandro.github.io/phaser-lessons/
JavaScript
1
star
28

websocket_browse

Websocket experiment which controls an iframe in another browser
1
star
29

angular-movies

Angular spike (not interesting)
JavaScript
1
star
30

authenticated_gist

Post gists under your github account
Ruby
1
star
31

go_callback

CGo callback example.
C
1
star
32

srunr

some spec runner thing
Ruby
1
star
33

specjour_rails_app

An example rails app for testing specjour
Ruby
1
star