8000 GitHub - irfanahmadein/cuprite at dockerize · GitHub
[go: up one dir, main page]

Skip to content
irfanahmadeinPublic
forked from rubycdp/cuprite

About

Headless Chrome driver for Capybara

Resources

Stars

0 stars

Watchers

1 watching

Forks

 
 

Repository files navigation

Cuprite - Headless Chrome driver for Capybara

Build Status

Cuprite is a pure Ruby driver (read as no Java/Selenium/WebDriver/ChromeDriver requirement) for Capybara. It allows you to run your Capybara tests on a headless Chrome or Chromium browser by CDP protocol.

The emphasis was made on raw CDP protocol because Headless Chrome allows you to do so many cool things that are barely supported by WebDriver because it should have consistent design with other browsers. The design of the driver will be as close to Poltergeist as possible but it's not a goal.

Installation

gem "cuprite"

and run bundle install.

In your test setup add:

require "capybara/cuprite"
Capybara.javascript_driver = :cuprite

Installing Chromium

As Chromium is stopped being built as a package for Linux don't even try to install it this way because it will either be outdated or unofficial package. Both are bad. Download it from official source.

Supported features

All the mandatory capybara features plus optional ones:

  • page.evaluate_script and page.execute_script
  • page.within_frame
  • page.status_code
  • page.response_headers
  • page.save_screenshot
  • page.driver.render_base64(format, options)
  • page.driver.scroll_to(left, top)
  • page.driver.basic_authorize(user, password)
  • element.send_keys(*keys)
  • page.driver.set_proxy(ip, port, type, user, password)
  • window API
  • cookie handling

Clicking coordinates

Sometimes its desirable to click a very specific area of the screen. You can accomplish this with page.driver.click(x, y), where x and y are the screen coordinates.

Manipulating request headers

Manipulate HTTP request headers like a boss:

page.driver.headers # => {}
page.driver.headers = { "User-Agent" => "Cuprite" }
page.driver.add_headers("Referer" => "https://example.com")
page.driver.headers # => { "User-Agent" => "Cuprite", "Referer" => "https://example.com" }

Notice that headers= will overwrite already set headers. You should use add_headers if you want to add a few more. These headers will apply to all subsequent HTTP requests (including requests for assets, AJAX, etc). They will be automatically cleared at the end of the test.

Inspecting network traffic

You can inspect the network traffic (i.e. what resources have been loaded) on the current page by calling page.driver.network_traffic. This returns an array of request objects. A request object has a response method containing data about the response.

Please note that network traffic is not cleared when you visit new page. You can manually clear the network traffic by calling page.driver.clear_network_traffic or page.driver.reset

Manipulating cookies

The following methods are used to inspect and manipulate cookies:

  • page.driver.cookies - a hash of cookies accessible to the current page. The keys are cookie names. The values are Cookie objects, with the following methods: name, value, domain, path, size, secure?, httponly?, session?, expires.
  • page.driver.set_cookie(name, value, options = {}) - set a cookie. The options hash can take the following keys: :domain, :path, :secure, :httponly, :expires. :expires should be a Time object.
  • page.driver.remove_cookie(name) - remove a cookie
  • page.driver.clear_cookies - clear all cookies

Customization

You can customize the way that Capybara sets up Cuprite via the following code in your test setup:

Capybara.register_driver :cuprite do |app|
  Capybara::Cuprite::Driver.new(app, options)
end

options is a hash of options. The following options are supported:

  • :browser_path (String) - Path to chrome binary, you can also set ENV variable as BROWSER_PATH=some/path/chrome bundle exec rspec.
  • :headless (Boolean) - Set browser as headless or not, true by default.
  • :slowmo (Integer | Float) - Set a delay to wait before sending command. Usefull companion of headless option, so that you have time to see changes.
  • :logger (Object responding to puts) - When present, debug output is written to this object.
  • :timeout (Numeric) - The number of seconds we'll wait for a response when communicating with browser. Default is 30.
  • :js_errors (Boolean) - When true, JavaScript errors get re-raised in Ruby.
  • :window_size (Array) - The dimensions of the browser window in which to test, expressed as a 2-element array, e.g. [1024, 768]. Default: [1024, 768]
  • :browser_options (Hash) - Additional command line options, see them all e.g. { "ignore-certificate-errors" => nil }
  • :extensions (Array) - An array of JS files to be preloaded into the browser
  • :port (Integer) - Remote debugging port for headless Chrome
  • :host (String) - Remote debugging address for headless Chrome
  • :url_blacklist (Array) - array of strings to match against requested URLs
  • :url_whitelist (Array) - array of strings to match against requested URLs
  • :process_timeout (Integer) - How long to wait for the Chrome process to respond on startup

URL Blacklisting & Whitelisting

Cuprite supports URL blacklisting, which allows you to prevent scripts from running on designated domains:

page.driver.browser.url_blacklist = ["http://www.example.com"]

and also URL whitelisting, which allows scripts to only run on designated domains:

page.driver.browser.url_whitelist = ["http://www.example.com"]

If you are experiencing slower run times, consider creating a URL whitelist of domains that are essential or a blacklist of domains that are not essential, such as ad networks or analytics, to your testing environment.

Architecture

In general there are 3 threads only: main thread in which we call methods like find(:xpath, ...) and interact with the driver, thread in Client and a thread in WebSocket. When driver is initialized we run external process (browser) and interact with it by websocket protocol. WebSocket has it's own thread to read and write from socket.

Imagine we want to call driver.visit I'll skip some details but we eventually call visit on Page instance where it sends a command Page.navigate thru Client and Websocket to the browser. This is async we can now do whatever we want in main thread or just sleep and wait for a response. Thread in WebSocket still works and parses all responses from the browser putting it into a queue. Also there is other type of responses called events, they don't have ids and they also put into a queue. Another thread in Client also works popping responses and events and processes events in the background while putting responses into another queue. We could have called wait in main thread and it just waits until needed response show up. All commands are processed sequentially anyway while one thread reads/writes socket and another one handles events in the background and puts responses into sequential queue. We can disregard command response if we don't need it.

Chrome devtools protocol supports many domains Target, Page for example. Some like Target only supported by the main ws_url address, some like Page are supported by page only and each page has it's own ws_url. So when driver is initialized there are only 3 threads and we can send commands to main ws_url so that we can create page and connect to a different ws_url.

When message is read it is put into a queue in WebSocket.

License

Copyright 2018-2019 Machinio

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Headless Chrome driver for Capybara

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

0