All Projects → rubycdp → Cuprite

rubycdp / Cuprite

Licence: mit
Headless Chrome/Chromium driver for Capybara

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Cuprite

Ferrum
Headless Chrome Ruby API
Stars: ✭ 1,009 (+35.8%)
Mutual labels:  chrome, chromium, headless, headless-chrome
Chromeless
🖥 Chrome automation made simple. Runs locally or headless on AWS Lambda.
Stars: ✭ 13,254 (+1683.85%)
Mutual labels:  integration-testing, chrome, headless, headless-chrome
Html Pdf Chrome
HTML to PDF converter via Chrome/Chromium
Stars: ✭ 629 (-15.34%)
Mutual labels:  chrome, chromium, headless, headless-chrome
Phpchrometopdf
A slim PHP wrapper around google-chrome to convert url to pdf or to take screenshots , easy to use and clean OOP interface
Stars: ✭ 127 (-82.91%)
Mutual labels:  chrome, chromium, headless, headless-chrome
Mocha Chrome
☕️ Run Mocha tests using headless Google Chrome
Stars: ✭ 66 (-91.12%)
Mutual labels:  chrome, headless, headless-chrome
Gowitness
🔍 gowitness - a golang, web screenshot utility using Chrome Headless
Stars: ✭ 996 (+34.05%)
Mutual labels:  chrome, headless, headless-chrome
Puppetron
Puppeteer (Headless Chrome Node API)-based rendering solution.
Stars: ✭ 429 (-42.26%)
Mutual labels:  chrome, chromium, headless
Pdf Bot
🤖 A Node queue API for generating PDFs using headless Chrome. Comes with a CLI, S3 storage and webhooks for notifying subscribers about generated PDFs
Stars: ✭ 2,551 (+243.34%)
Mutual labels:  chromium, headless, headless-chrome
Serverless Chrome
🌐 Run headless Chrome/Chromium on AWS Lambda
Stars: ✭ 2,625 (+253.3%)
Mutual labels:  chrome, chromium, headless-chrome
Pychrome
A Python Package for the Google Chrome Dev Protocol [threading base]
Stars: ✭ 469 (-36.88%)
Mutual labels:  chrome, headless, headless-chrome
Headless Chrome Crawler
Distributed crawler powered by Headless Chrome
Stars: ✭ 5,129 (+590.31%)
Mutual labels:  chrome, chromium, headless-chrome
Navalia
A bullet-proof, fast, and reliable headless browser API
Stars: ✭ 950 (+27.86%)
Mutual labels:  chrome, headless, headless-chrome
pythonista-chromeless
Serverless selenium which dynamically execute any given code.
Stars: ✭ 31 (-95.83%)
Mutual labels:  integration-testing, headless, headless-chrome
headless-chrome-alpine
A Docker container running headless Chrome
Stars: ✭ 26 (-96.5%)
Mutual labels:  headless, chromium, headless-chrome
Chrome Devtools Protocol
Chrome Devtools Protocol client for PHP
Stars: ✭ 112 (-84.93%)
Mutual labels:  chrome, headless, headless-chrome
Url To Pdf Api
Web page PDF/PNG rendering done right. Self-hosted service for rendering receipts, invoices, or any content.
Stars: ✭ 6,544 (+780.75%)
Mutual labels:  chrome, headless, headless-chrome
Crawlergo
A powerful dynamic crawler for web vulnerability scanners
Stars: ✭ 1,088 (+46.43%)
Mutual labels:  chromium, headless, headless-chrome
Taiko
A node.js library for testing modern web applications
Stars: ✭ 2,964 (+298.92%)
Mutual labels:  testing-tools, headless, headless-chrome
node-headless-chrome
⚠️ 🚧 Install precompiled versions of the Chromium/Chrome headless shell using npm or yarn
Stars: ✭ 20 (-97.31%)
Mutual labels:  headless, chromium, headless-chrome
Playwright Go
Playwright for Go a browser automation library to control Chromium, Firefox and WebKit with a single API.
Stars: ✭ 272 (-63.39%)
Mutual labels:  chromium, headless, headless-chrome

Cuprite - Headless Chrome driver for Capybara

Cuprite is a pure Ruby driver (read as no Selenium/WebDriver/ChromeDriver dependency) for Capybara. It allows you to run Capybara tests on a headless Chrome or Chromium. Under the hood it uses Ferrum which is high-level API to the browser by CDP protocol. The design of the driver is as close to Poltergeist as possible though it's not a goal.

Cuprite website is designed & supported by Evrone What else we build with Ruby

Install

Add this to your Gemfile and run bundle install.

group :test do
  gem "cuprite"
end

In your test setup add:

require "capybara/cuprite"
Capybara.javascript_driver = :cuprite
Capybara.register_driver(:cuprite) do |app|
  Capybara::Cuprite::Driver.new(app, window_size: [1200, 800])
end

if you use Docker don't forget to pass no-sandbox option:

Capybara::Cuprite::Driver.new(app, browser_options: { 'no-sandbox': nil })

Since Cuprite uses Ferrum there are many useful methods you can call even using this driver:

browser = page.driver.browser
browser.mouse.move(x: 123, y: 456).down.up

If you already have tests on Poltergeist then it should simply work, for Selenium you better check your code for manage calls because it works differently in Cuprite, see the documentation below.

Customization

See the full list of options for Ferrum.

You can pass options with the following code in your test setup:

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

Cuprite-specific options are:

  • options Hash
    • :url_blacklist (Array) - array of strings to match against requested URLs
    • :url_whitelist (Array) - array of strings to match against requested URLs

Debugging

If you pass inspector option, remote debugging will be enabled if you run tests with INSPECTOR=true. Then you can put page.driver.debug or page.driver.debug(binding) in your test to pause it. This will launch the browser where you can inspect the content.

Capybara.register_driver :cuprite do |app|
  Capybara::Cuprite::Driver.new(app, inspector: ENV['INSPECTOR'])
end

then somewhere in the test:

it "does something useful" do
  visit root_path

  fill_in "field", with: "value"
  page.driver.debug(binding)

  expect(page).to have_content("value")
end

In the middle of the execution Chrome will open a new tab where you can inspect the content and also if you passed binding an irb or pry console will be opened where you can further experiment with the test.

Clicking/Scrolling

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

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.

Network traffic

  • page.driver.network_traffic Inspect network traffic (loaded resources) on the current page. This returns an array of request objects.
page.driver.network_traffic # => [Request, ...]
request = page.driver.network_traffic.first
request.response
  • page.driver.wait_for_network_idle Natively waits for network idle and if there are no active connections returns or raises TimeoutError error. Accepts the same options as wait_for_idle
page.driver.wait_for_network_idle
page.driver.refresh

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

  • page.driver.wait_for_reload unlike wait_for_network_idle will wait until the whole page is reloaded or raise a timeout error. It's useful when you know that for example after clicking autocomplete suggestion you expect page to be reloaded, you have a few choices - put sleep or wait for network idle, but both are bad. Sleep makes you wait longer or less than needed, network idle can return earlier even before the whole page is started to reload. Here's the rescue.

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

Screenshot

Besides capybara screenshot method you can get image as Base64:

  • page.driver.render_base64(format, options)

Authorization

  • page.driver.basic_authorize(user, password)
  • page.driver.set_proxy(ip, port, type, user, password)

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.

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].