Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sandro/capybara-chrome
Chrome driver for Capybara using Chrome's remote debugging protocol
https://github.com/sandro/capybara-chrome
capybara headless headless-chrome rspec testing
Last synced: 7 days ago
JSON representation
Chrome driver for Capybara using Chrome's remote debugging protocol
- Host: GitHub
- URL: https://github.com/sandro/capybara-chrome
- Owner: sandro
- License: mit
- Created: 2018-06-19T01:11:36.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-06-04T16:41:43.000Z (over 3 years ago)
- Last Synced: 2024-10-22T16:43:04.011Z (17 days ago)
- Topics: capybara, headless, headless-chrome, rspec, testing
- Language: Ruby
- Homepage:
- Size: 75.2 KB
- Stars: 28
- Watchers: 15
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-puppeteer - capybara-chrome
README
# Capybara::Chrome
Use [Capybara](https://github.com/teamcapybara/capybara) to drive Chrome in headless mode via the [debugging protocol](https://chromedevtools.github.io/devtools-protocol/).
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'capybara-chrome'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install capybara-chrome
### Using DockerAdd this line to your Dockerfile to install Chrome:
```dockerfile
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`
```bash
docker run --security-opt seccomp=unconfined
```
## Usage
```ruby
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](https://github.com/macournoyer/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:```ruby
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](http://img.youtube.com/vi/SLmkx5z-lAA/0.jpg)](http://www.youtube.com/watch?v=SLmkx5z-lAA)
A video demonstrating debugging an rspec test with `byebug`
[![Preview of debugging capybara-chrome](http://img.youtube.com/vi/McEQG9YEAdE/0.jpg)](http://www.youtube.com/watch?v=McEQG9YEAdE)
A video showing capybara-chrome running against a netcat backend
[![Preview of debugging capybara-chrome with netcat](http://img.youtube.com/vi/B1__LeLyXBo/0.jpg)](http://www.youtube.com/watch?v=B1__LeLyXBo)
## 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](https://opensource.org/licenses/MIT).