2
2
3
3
[ ![ Build Status] ( https://travis-ci.org/rubycdp/cuprite.svg?branch=master )] ( https://travis-ci.org/rubycdp/cuprite )
4
4
5
- Cuprite is a pure Ruby driver (read as _ no_ Java/Selenium/WebDriver/ChromeDriver
6
- requirement) for [ Capybara] ( https://github.com/teamcapybara/capybara ) . It allows
7
- you to run your Capybara tests on a headless [ Chrome] ( https://www.google.com/chrome/ )
8
- or [ Chromium] ( https://www.chromium.org/ ) by [ CDP protocol] ( https://chromedevtools.github.io/devtools-protocol/ ) .
9
- Under the hood it uses [ Ferrum] ( https://github.com/route/ferrum ) which is
10
- high-level API to the browser again by CDP protocol.
11
-
12
- The emphasis was made on raw CDP protocol because Headless Chrome allows you to
13
- do so many things that are barely supported by WebDriver because it should have
14
- consistent design with other browsers. The design of the driver will be as
15
- close to [ Poltergeist] ( https://github.com/teampoltergeist/poltergeist ) as
16
- possible though it's not a goal.
5
+ Cuprite is a pure Ruby driver (read as _ no_ Selenium/WebDriver/ChromeDriver
6
+ dependency) for [ Capybara] ( https://github.com/teamcapybara/capybara ) . It allows
7
+ you to run Capybara tests on a headless Chrome or Chromium. Under the hood it
8
+ uses [ Ferrum] ( https://github.com/rubycdp/ferrum#index ) which is high-level API
9
+ to the browser by CDP protocol. The design of the driver is as close to
10
+ [ Poltergeist] ( https://github.com/teampoltergeist/poltergeist ) as possible though
11
+ it's not a goal.
12
+
13
+ Since Cuprite uses [ Ferrum] ( https://github.com/rubycdp/ferrum#examples ) there
14
+ are many useful methods you can call even using this driver:
15
+
16
+ ``` ruby
17
+ browser = page.driver.browser
18
+ browser.mouse.move(x: 123 , y: 456 ).down.up
19
+ ```
20
+
17
21
18
22
## Install
19
23
20
- Add these lines to your ` Gemfile ` and run ` bundle install ` .
24
+ Add this to your ` Gemfile ` and run ` bundle install ` .
21
25
22
26
``` ruby
23
- gem " cuprite" , group: :test
27
+ group :test do
28
+ gem " cuprite"
29
+ end
24
30
```
25
31
26
32
In your test setup add:
@@ -33,77 +39,51 @@ Capybara.register_driver(:cuprite) do |app|
33
39
end
34
40
```
35
41
36
- If you already had tests on Poltergeist then it should simply work, for Selenium
37
- you better check your code for ` .manage ` calls because things are much easier
38
- with Cuprite, see the documentation below.
42
+ if you use ` Docker ` don't forget to pass ` no-sandbox ` option:
43
+
44
+ ``` ruby
45
+ Capybara ::Cuprite ::Driver .new (app, browser_options: { ' no-sandbox' : nil })
46
+ ```
39
47
40
- ## Install Chrome
48
+ If you already have tests on Poltergeist then it should simply work, for
49
+ Selenium you better check your code for ` manage ` calls because it works
50
+ differently in Cuprite, see the documentation below.
41
51
42
- There's no official Chrome or Chromium package for Linux don't install it this
43
- way because it either will be outdated or unofficial, both are bad. Download it
44
- from official [ source] ( https://www.chromium.org/getting-involved/download-chromium ) .
45
- Chrome binary should be in the ` PATH ` or ` BROWSER_PATH ` or you can pass it as an
46
- option
47
52
48
53
## Customization
49
54
50
- You can customize options with the following code in your test setup:
55
+ See the full list of options for
56
+ [ Ferrum] ( https://github.com/rubycdp/ferrum#customization ) .
57
+
58
+ You can pass options with the following code in your test setup:
51
59
52
60
``` ruby
53
61
Capybara .register_driver(:cuprite ) do |app |
54
62
Capybara ::Cuprite ::Driver .new (app, options)
55
63
end
56
64
```
57
65
58
- #### Running in Docker
59
-
60
- In docker as root you must pass the no-sandbox browser option:
61
-
62
- ``` ruby
63
- Capybara ::Cuprite ::Driver .new (app, browser_options: { ' no-sandbox' : nil })
64
- ```
66
+ ` Cuprite ` -specific options are:
65
67
66
68
* options ` Hash `
67
- * ` :browser_path ` (String) - Path to chrome binary, you can also set ENV
68
- variable as ` BROWSER_PATH=some/path/chrome bundle exec rspec ` .
69
- * ` :headless ` (Boolean) - Set browser as headless or not, ` true ` by default.
70
- * ` :slowmo ` (Integer | Float) - Set a delay to wait before sending command.
71
- Usefull companion of headless option, so that you have time to see changes.
72
- * ` :logger ` (Object responding to ` puts ` ) - When present, debug output is
73
- written to this object.
74
- * ` :timeout ` (Numeric) - The number of seconds we'll wait for a response when
75
- communicating with browser. Default is 5.
76
- * ` :js_errors ` (Boolean) - When true, JavaScript errors get re-raised in Ruby.
77
- * ` :window_size ` (Array) - The dimensions of the browser window in which to
78
- test, expressed as a 2-element array, e.g. [ 1024, 768] . Default: [ 1024, 768]
79
- * ` :browser_options ` (Hash) - Additional command line options,
80
- [ see them all] ( https://peter.sh/experiments/chromium-command-line-switches/ )
81
- e.g. ` { "ignore-certificate-errors" => nil } `
82
- * ` :extensions ` (Array) - An array of JS files to be preloaded into the browser
83
- * ` :port ` (Integer) - Remote debugging port for headless Chrome
84
- * ` :host ` (String) - Remote debugging address for headless Chrome
85
- * ` :url ` (String) - URL for a running instance of Chrome. If this is set, a
86
- browser process will not be spawned.
87
69
* ` :url_blacklist ` (Array) - array of strings to match against requested URLs
88
70
* ` :url_whitelist ` (Array) - array of strings to match against requested URLs
89
- * ` :process_timeout ` (Integer) - How long to wait for the Chrome process to
90
- respond on startup
91
71
92
- ### Remote debugging
93
72
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.
73
+ ## Debugging
74
+
75
+ If you pass ` inspector ` option, remote debugging will be enabled if you run
76
+ tests with ` INSPECTOR=true ` . Then you can put ` page.driver.debug ` or
77
+ ` page.driver.debug(binding) ` in your test to pause it. This will launch the
78
+ browser where you can inspect the content.
99
79
100
80
``` ruby
101
81
Capybara .register_driver :cuprite do |app |
102
82
Capybara ::Cuprite ::Driver .new (app, inspector: ENV [' INSPECTOR' ])
103
83
end
104
84
```
105
85
106
- then somewhere deep in the test:
86
+ then somewhere in the test:
107
87
108
88
``` ruby
109
89
it " does something useful" do
117
97
```
118
98
119
99
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 .
100
+ the content and also if you passed ` binding ` an ` irb ` or ` pry ` console will be
101
+ opened where you can further experiment with the test .
122
102
123
- ### Clicking/Scrolling
103
+ ## Clicking/Scrolling
124
104
125
105
* ` page.driver.click(x, y) ` Click a very specific area of the screen.
126
- * ` page.driver.scroll_to(left, top) ` Scroll to given position.
127
- * ` element.send_keys(*keys) ` Send keys to given node.
106
+ * ` page.driver.scroll_to(left, top) ` Scroll to a given position.
107
+ * ` element.send_keys(*keys) ` Send keys to a given node.
128
108
129
- ### Request headers
109
+ ## Request headers
130
110
131
111
Manipulate HTTP request headers like a boss:
132
112
@@ -142,10 +122,10 @@ Notice that `headers=` will overwrite already set headers. You should use
142
122
subsequent HTTP requests (including requests for assets, AJAX, etc). They will
143
123
be automatically cleared at the end of the test.
144
124
145
- ### Network traffic
125
+ ## Network traffic
146
126
147
- * ` page.driver.network_traffic ` Inspect network traffic (resources have been
148
- loaded) on the current page. This returns an array of request objects.
127
+ * ` page.driver.network_traffic ` Inspect network traffic (loaded resources) on
128
+ the current page. This returns an array of request objects.
149
129
150
130
``` ruby
151
131
page.driver.network_traffic # => [Request, ...]
@@ -155,7 +135,8 @@ request.response
155
135
156
136
* ` page.driver.wait_for_network_idle ` Natively waits for network idle and if
157
137
there are no active connections returns or raises ` TimeoutError ` error. Accepts
158
- the same options as Ferrum's [ ` wait_for_idle ` ] ( https://github.com/route/ferrum#wait_for_idleoptions )
138
+ the same options as
139
+ [ ` wait_for_idle ` ] ( https://github.com/rubycdp/ferrum#wait_for_idleoptions )
159
140
160
141
``` ruby
161
142
page.driver.wait_for_network_idle
@@ -167,7 +148,7 @@ manually clear the network traffic by calling `page.driver.clear_network_traffic
167
148
or ` page.driver.reset `
168
149
169
150
170
- ### Manipulating cookies
151
+ ## Manipulating cookies
171
152
172
153
The following methods are used to inspect and manipulate cookies:
173
154
@@ -182,27 +163,28 @@ The following methods are used to inspect and manipulate cookies:
182
163
* ` page.driver.remove_cookie(name) ` - remove a cookie
183
164
* ` page.driver.clear_cookies ` - clear all cookies
184
165
185
- ### Screenshot
166
+ ## Screenshot
186
167
187
168
Besides capybara screenshot method you can get image as Base64:
188
169
189
170
* ` page.driver.render_base64(format, options) `
190
171
191
- ### Authorization
172
+ ## Authorization
192
173
193
174
* ` page.driver.basic_authorize(user, password) `
194
175
* ` page.driver.set_proxy(ip, port, type, user, password) `
195
176
196
- ### URL Blacklisting & Whitelisting
177
+ ## URL Blacklisting & Whitelisting
178
+
197
179
Cuprite supports URL blacklisting, which allows you to prevent scripts from
198
180
running on designated domains:
199
181
200
182
``` ruby
201
183
page.driver.browser.url_blacklist = [" http://www.example.com" ]
202
184
```
203
185
204
- and also URL whitelisting, which allows scripts to only run
205
- on designated domains:
186
+ and also URL whitelisting, which allows scripts to only run on designated
187
+ domains:
206
188
207
189
``` ruby
208
190
page.driver.browser.url_whitelist = [" http://www.example.com" ]
0 commit comments