Skip to content

Commit b2a892a

Browse files
committed
Pass binding to debug method
1 parent cdcc2c7 commit b2a892a

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

README.md

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Cuprite - Headless Chrome driver for Capybara #
1+
# Cuprite - Headless Chrome driver for Capybara
22

33
[![Build Status](https://travis-ci.org/machinio/cuprite.svg?branch=master)](https://travis-ci.org/machinio/cuprite)
44

@@ -15,7 +15,7 @@ consistent design with other browsers. The design of the driver will be as
1515
close to [Poltergeist](https://github.com/teampoltergeist/poltergeist) as
1616
possible though it's not a goal.
1717

18-
## Install ##
18+
## Install
1919

2020
Add these lines to your `Gemfile` and run `bundle install`.
2121

@@ -37,15 +37,15 @@ If you already had tests on Poltergeist then it should simply work, for Selenium
3737
you better check your code for `.manage` calls because things are much easier
3838
with Cuprite, see the documentation below.
3939

40-
## Install Chrome ##
40+
## Install Chrome
4141

4242
There's no official Chrome or Chromium package for Linux don't install it this
4343
way because it either will be outdated or unofficial, both are bad. Download it
4444
from official [source](https://www.chromium.org/getting-involved/download-chromium).
4545
Chrome binary should be in the `PATH` or `BROWSER_PATH` or you can pass it as an
4646
option
4747

48-
## Customization ##
48+
## Customization
4949

5050
You can customize options with the following code in your test setup:
5151

@@ -55,7 +55,7 @@ Capybara.register_driver(:cuprite) do |app|
5555
end
5656
```
5757

58-
#### Running in Docker ####
58+
#### Running in Docker
5959

6060
In docker as root you must pass the no-sandbox browser option:
6161

@@ -89,13 +89,44 @@ Capybara::Cuprite::Driver.new(app, browser_options: { 'no-sandbox': nil })
8989
* `:process_timeout` (Integer) - How long to wait for the Chrome process to
9090
respond on startup
9191

92-
### Clicking/Scrolling ###
92+
### Remote debugging
93+
94+
If you pass `inspector: ENV['INSPECTOR']` option, remote debugging will be
95+
enabled. When this option is enabled, you can insert `page.driver.debug` or
96+
`page.driver.debug(binding)` into your tests to pause the test and launch a
97+
browser which gives you the Chrome inspector to view all of your open pages and
98+
inspect them.
99+
100+
```ruby
101+
Capybara.register_driver :cuprite do |app|
102+
Capybara::Cuprite::Driver.new(app, inspector: ENV['INSPECTOR'])
103+
end
104+
```
105+
106+
then somewhere deep in the test:
107+
108+
```ruby
109+
it "does something useful" do
110+
visit root_path
111+
112+
fill_in "field", with: "value"
113+
page.driver.debug(binding)
114+
115+
expect(page).to have_content("value")
116+
end
117+
```
118+
119+
In the middle of the execution Chrome will open a new tab where you can inspect
120+
page's content and also if you passed binding an `irb` or `pry` console will be
121+
opened where you can further test some expressions.
122+
123+
### Clicking/Scrolling
93124

94125
* `page.driver.click(x, y)` Click a very specific area of the screen.
95126
* `page.driver.scroll_to(left, top)` Scroll to given position.
96127
* `element.send_keys(*keys)` Send keys to given node.
97128

98-
### Request headers ###
129+
### Request headers
99130

100131
Manipulate HTTP request headers like a boss:
101132

@@ -111,7 +142,7 @@ Notice that `headers=` will overwrite already set headers. You should use
111142
subsequent HTTP requests (including requests for assets, AJAX, etc). They will
112143
be automatically cleared at the end of the test.
113144

114-
### Network traffic ###
145+
### Network traffic
115146

116147
* `page.driver.network_traffic` Inspect network traffic (resources have been
117148
loaded) on the current page. This returns an array of request objects.
@@ -136,7 +167,7 @@ manually clear the network traffic by calling `page.driver.clear_network_traffic
136167
or `page.driver.reset`
137168

138169

139-
### Manipulating cookies ###
170+
### Manipulating cookies
140171

141172
The following methods are used to inspect and manipulate cookies:
142173

@@ -151,18 +182,18 @@ The following methods are used to inspect and manipulate cookies:
151182
* `page.driver.remove_cookie(name)` - remove a cookie
152183
* `page.driver.clear_cookies` - clear all cookies
153184

154-
### Screenshot ###
185+
### Screenshot
155186

156187
Besides capybara screenshot method you can get image as Base64:
157188

158189
* `page.driver.render_base64(format, options)`
159190

160-
### Authorization ###
191+
### Authorization
161192

162193
* `page.driver.basic_authorize(user, password)`
163194
* `page.driver.set_proxy(ip, port, type, user, password)`
164195

165-
### URL Blacklisting & Whitelisting ###
196+
### URL Blacklisting & Whitelisting
166197
Cuprite supports URL blacklisting, which allows you to prevent scripts from
167198
running on designated domains:
168199

@@ -181,22 +212,7 @@ If you are experiencing slower run times, consider creating a URL whitelist of
181212
domains that are essential or a blacklist of domains that are not essential,
182213
such as ad networks or analytics, to your testing environment.
183214

184-
### Remote debugging ###
185-
186-
If you use the `inspector: true` option, remote debugging will be enabled. When
187-
this option is enabled, you can insert `page.driver.debug` into your tests to
188-
pause the test and launch a browser which gives you the Chrome inspector to view
189-
all your open pages and inspect them.
190-
191-
You could set the inspector option via an environment variable:
192-
193-
```ruby
194-
Capybara.register_driver :cuprite do |app|
195-
Capybara::Cuprite::Driver.new(app, inspector: ENV['INSPECTOR'])
196-
end
197-
```
198-
199-
## License ##
215+
## License
200216

201217
Copyright 2018-2020 Machinio
202218

lib/capybara/cuprite/driver.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,26 @@ def basic_authorize(user, password)
300300
end
301301
alias_method :authorize, :basic_authorize
302302

303-
def debug
303+
def debug_url
304+
"http://#{browser.process.host}:#{browser.process.port}"
305+
end
306+
307+
def debug(binding = nil)
304308
if @options[:inspector]
305-
Process.spawn(browser.process.path, "http://#{browser.process.host}:#{browser.process.port}")
306-
pause
309+
Process.spawn(browser.process.path, debug_url)
310+
311+
if binding&.respond_to?(:pry)
312+
binding.pry
313+
elsif binding&.respond_to?(:irb)
314+
binding.irb
315+
else
316+
pause
317+
end
307318
else
308319
raise Error, "To use the remote debugging, you have to launch " \
309-
"the driver with `inspector: true` configuration option"
320+
"the driver with `inspector: ENV['INSPECTOR']` " \
321+
"configuration option and run your test suite passing " \
322+
"env variable"
310323
end
311324
end
312325

0 commit comments

Comments
 (0)