Automation testing can have different goals, strategies, and challenges depending on a project goal or team size. The most common goals and challenges are better test performance and having more browser behavior control. However, it can also be related to having better and more versatile debugging tools and closely mimicking real-user interactions. Capybara is a library written in Ruby that is used for automation functional testing. It can work with several gems – open source libraries, to achieve those, but one gem provides everything mentioned above – Cuprite. Cuprite is a Capybara driver for UI automation that runs the test on headless Chrome or Chromium. It uses Ferrum, a high-level API, to control the browser using CDP (Chrome DevTools Protocol). CDP allows direct connection to the browser and provides various tools to inspect, debug, instrument, and profile Chrome, Chromium, and other browsers.

Cuprite’s advantages, like better test performance, better browser control, debugging tools, and real-user interactions, come from its integration with CDP. The majority of other drivers use the WebDriver protocol. It is used for different actions like starting the browser, clicking on elements, opening new tabs, etc. Generally speaking, other drivers have some of the features that Cuprite offers. It is important to note that to use these features with other drivers, some additional configurations are needed, sometimes even additional tools. But even with these configurations and tools, it is unlikely that the performance and the result will match Cuprite’s. Here are some of the benefits of using Cuprite, and at the end, there is a setup process.


Better test performance

Cuprite offers significant performance improvements over other drivers used by Capybara. These performance improvements contribute to faster and more reliable test execution. Its features make it one of the best choices for automation testing scenarios where test performance is the priority.

Imagine working on a large automation project with several hundred test scripts, each containing at least 30 tests. In cases like this, every second, not to use larger time units, is crucial and contributes to the test execution. 

Some sources reported that performance improved by 25% or even 40%. These performance improvements usually demand that some tests be edited to catch up with their speed. Here are some of its features that contribute to this performance:

Headless browser

Cuprite runs on headless Chrome or Chromium. Both are optimized for performance and generally require much fewer resources when compared with running a full graphical browser. Not rendering the UI also contributes to faster test execution. Even so, running tests in the headless browsers is sometimes not the best approach. When a test fails, sometimes it is impossible to see what happened, so we can configure Cuprite not to run in headless mode.

Direct browser interaction

Cuprite interacts directly with the browser through CDP. It helps minimize the latency and improves the execution speed of browser commands and interactions.

Handling asynchronous operations

Its integration with DevTools allows Cuprite to control asynchronous operations and dynamic content. Asynchronous operations enable processes to operate independently. It also has more reliable and speedy interactions with asynchronous elements and AJAX requests. These requests allow web pages to update asynchronously.

Decreased resource consumption

Using the lightweight headless Chrome and Chromium browsers reduces memory and CPU usage. It contributes to faster test execution and reduced impact on the application under test.


Browser behavior control

Because of its integration with the CDP, Cuprite provides excellent control over browser behavior.  These include advanced capabilities for network control, adjusting the browser settings, device emulation, and efficient handling of JavaScript behaviors.

Enhanced the control of the browser behaviors

Cuprite allows various browser settings to be adjusted, such as viewport size, user agent, and other settings. All this is achieved directly through the DevTools Protocol. Cuprite also allows the usage of cookies, enabling the sign-in form to be configured to be completed almost instantly.

Using the advanced network control

Advanced network control features such as throttling speed, simulation of the offline conditions, and intercepting network requests and responses through the DevTools Protocol are also available with Cuprite.

Handling of the JavaScript behaviors

Cuprite can control JavaScript execution. Furthermore, it can interact with the JavaScript context in real time, which enables better handling of dynamic content and JavaScript-heavy applications.


Debugging tools

Due to Cuprite’s direct integration with CDP, it has access to several debugging tools. The most important are real-time network and performance monitoring, network control, and error diagnostics. As it often happens, we are unsure if some locator is correct, if a method using that locator is working, or if a button click did not trigger a request. Cuprite narrows the issue through network control and helps us discover what happened more easily.

Network and performance monitoring

Cuprite’s integration with DevTools enables analysis of the rendering time, resource loading details, JavaScript console logs, and errors. Apart from the performance information, the network control allows real-time inspection and manipulation of the requests. Furthermore, it also enables the simulation of different network conditions while monitoring the network activity.

Error diagnostics

DevTools also provides extensive error diagnostics with detailed stack traces and the necessary information.


Real user interactions

With Chrome DevTools Protocol’s advantages of advanced interactions, web features, and realistic rendering, Cuprite provides an accurate simulation of user behavior. All this is necessary for creating an excellent and representative testing environment.

Advanced and improved browser interactions

Cuprite allows a variety of advanced user interactions that can be challenging with other drivers.  These interactions are drag-and-drop, hovering, double-click, point-and-click, context-click, etc. Moreover, Cuprite offers excellent control over browser adjustments. It provides accurate handling of user input and user interactions as well.

Realistic rendering and behavior

As we have already said, Cuprite runs headless Chrome or Chromium. Because of that, it provides the same rendering engine and the same behavior as the real-browser session. This rendering and behavior include CSS, JavaScript execution, and dynamic content updates.

Support for modern web features

Since Cuprite uses the latest version of Chrome, it operates with modern web features like HTML5, CSS3, and advanced JavaScript APIs. Thanks to this, we are sure the tests are running on the current web technologies.


Setup of Cuprite with Capybara

The setup of Cuprite with Capybara is straightforward, which is one more advantage of it. Follow these steps:

1. Add Capybara and Cuprite to the Gemfile

# Gemfile

gem ‘capybara’
gem ‘cuprite’


2. Run
bundle install to install the gems

3. Configure Cuprite as the Capybara driver

The test setup file contains all the project settings. For RSpec, a testing framework, the most common names for this file are spec_helper.rb or helper.rb.

# helper.rb
require ‘capybara/rspec’
require ‘cuprite’

Capybara.register_driver :cuprite do |app|
   options = {
        # You can add custom options here, such as:
        # headless: true,
        # timeout: 60,
        # etc.
    }

   Capybara::Cuprite::Driver.new(app, **options)
end

Capybara.default_driver = :cuprite


The previous example shows that Cuprite offers different driver options. Depending on the current testing needs, these are adjusted.

Capybara.register_driver :cuprite do |app|
   options = {
      headless: true,
      js_errors: true,
      # Other options can be set here
   }

   Capybara::Cuprite::Driver.new(app, **options)
end


4. Tests

Once the previous steps are completed, we can start writing tests.

require ‘helper’

Rspec.describe ‘MyFeature’, type:  :feature do
   it ‘logs in’ do
      visit ‘/some_path’
      fill_in ‘Email’, with: ‘[email protected]’
      fill_in ‘Password’, with: ‘Password’
      click_button ‘Submit’
 
      expect(page).to have_content(‘Welcome!’)
   end
end


Conclusion

Integration of Cuprite with Capybara is an excellent choice for modern web automation testing. Firstly, Capybara and Cuprite setups are user-friendly. It alone makes this duo even more appealing. Secondly, since Cuprite uses the Chrome DevTools Protocol, numerous advantages can improve the testing strategy’s efficiency, reliability, and depth. Cuprite’s advanced capabilities and Capybara’s versatile testing framework provide a powerful solution for web automation. With Cuprite and Capybara, teams can utilize modern browser features, achieve faster test runs, and get deeper insights into application behavior. All this leads to more reliable and efficient testing processes – the ultimate goal of the QA process.


“Utilizing Cuprite with Capybara” Tech Bite was brought to you by Hazim Okanović, Junior Quality Assurance Engineer at Atlantbh.

Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.

 

Leave a comment

Your email address will not be published. Required fields are marked *