Back to Test Automation
2025-12-125 min read

Cypress.browser (Test Automation)

Learn Cypress.browser (Test Automation) step by step with clear examples and exercises.

Title: Test Automation with Cypress.browser (JavaScript)

Why This Matters

In software testing, automating repetitive tasks can significantly reduce human effort and errors. With tools like Selenium, Playwright, and Cypress, we can write scripts to test web applications automatically. In this lesson, we'll focus on using the Cypress.browser API for test automation with JavaScript examples.

The Cypress.browser API provides essential details about the browser instance used for testing, enabling us to create more targeted and effective test cases. By understanding and utilizing this API, you can improve the quality of your tests and reduce the time spent on manual testing tasks.

Prerequisites

Before diving into the core concept, ensure you have:

  1. Basic understanding of JavaScript (ES6)
  2. Familiarity with Node.js and npm
  3. Knowledge of web technologies like HTML, CSS, and HTTP
  4. Installation of Cypress Test Runner in your project
  5. A basic understanding of how to write tests using Cypress
  6. Familiarity with browser families (Chromium, Gecko, Webkit, Edge)
  7. Understanding of browser versions and their importance in testing
  8. Knowledge of how to navigate to URLs and interact with elements using Cypress APIs like cy.visit() and cy.get()

Core Concept

Cypress.browser provides properties related to the browser instance used for testing. It allows you to access various details about the browser, such as its display name, rendering engine, version, and more. You can use these properties in your tests to create customized test cases that cater to specific scenarios or browser types.

// Import Cypress
const { browser } = require('cypress')

describe('Browser Information', () => {
it('Logs browser information', () => {
// Get the browser object
const myBrowser = browser

// Log the browser details
console.log(myBrowser)
})
})

In this example, we're importing Cypress and accessing its browser instance using const { browser } = require('cypress'). We then create a test case to log the browser information when it runs.

When you run the test, the output will display various properties of the browser object, such as:

{
channel: 'stable',
displayName: 'Chrome',
family: 'chromium',
isChosen: true,
majorVersion: 80,
name: 'chrome',
path: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
version: '80.0.3987.87',
isHeaded: true,
isHeadless: false
}

Worked Example

Let's create a simple test using Cypress.browser to verify the browser information and check if it's Chrome or Firefox.

describe('Browser Information', () => {
it('Verifies browser type', () => {
// Get the browser object
const myBrowser = browser

// Check if the browser is Chrome
if (myBrowser.family === 'chromium') {
expect(myBrowser.name).to.equal('chrome')
} else if (myBrowser.family === 'gecko') {
expect(myBrowser.name).to.equal('firefox')
} else {
throw new Error(`Unsupported browser: ${myBrowser.family}`)
}
})
})

In this example, we're checking the family property of the browser object to determine if it's Chrome or Firefox. If the browser family is not supported, an error is thrown.

Common Mistakes

  1. Forgetting to import Cypress: Ensure you include the necessary import statement at the beginning of your test file: const { browser } = require('cypress').
  2. Not checking for supported browsers: If your test script doesn't handle unsupported browsers, it may fail unexpectedly or produce incorrect results. You should include error handling or conditional logic to ensure your tests work as intended for all supported browsers.
  3. Ignoring browser properties: Not utilizing the various properties of the browser object can limit the effectiveness and scope of your tests.
  4. Assuming all browsers support the same features: Different browsers have different capabilities and behaviors, so it's essential to account for these differences in your tests.
  5. Not handling headless mode correctly: When running Cypress in headless mode, some browser properties may not be available or behave differently. Ensure you test both headless and non-headless scenarios as needed.
  6. Not using the correct API for navigation and interaction: While Cypress.browser provides properties related to the browser instance, it does not allow you to perform actions directly on the browser. For that, you should use other Cypress APIs like cy.visit(), cy.get(), and others.
  7. Not considering browser version differences: Different versions of the same browser may have different behaviors or support different features, so it's essential to account for these differences in your tests as well.

Practice Questions

  1. Write a test to verify if the browser version is greater than 80 for Chrome.
describe('Browser Version', () => {
it('Verifies Chrome version is greater than 80', () => {
const myBrowser = browser

expect(myBrowser.majorVersion).to.be.gte(80)
})
})
  1. Extend the previous worked example to support additional browsers like Safari and Edge.
describe('Browser Information', () => {
it('Verifies browser type', () => {
const myBrowser = browser

if (myBrowser.family === 'chromium') {
expect(myBrowser.name).to.equal('chrome')
} else if (myBrowser.family === 'gecko') {
expect(myBrowser.name).to.equal('firefox')
} else if (myBrowser.family === 'webkit') {
expect(myBrowser.name).to.equal('safari')
} else if (myBrowser.family === 'edge') {
expect(myBrowser.name).to.equal('edge')
} else {
throw new Error(`Unsupported browser: ${myBrowser.family}`)
}
})
})
  1. Write a test to verify that the current URL contains a specific substring (e.g., 'example.com').
describe('URL Verification', () => {
it('Verifies URL contains "example.com"', () => {
cy.url().should('include', 'example.com')
})
})
  1. Write a test to click an element with the class my-element and verify that its text content changes after clicking.
describe('Element Interaction', () => {
it('Clicks an element and verifies text change', () => {
cy.get('.my-element').click()
cy.get('.my-element').should('contain', 'Expected Text After Click')
})
})

FAQ

  1. Why should I use Cypress.browser in my tests?

Using Cypress.browser allows you to access various properties of the browser instance, which can be useful for writing more targeted and effective test cases. By understanding and utilizing this API, you can improve the quality of your tests and reduce the time spent on manual testing tasks.

  1. What happens if I run the test on an unsupported browser?

If your test script doesn't handle unsupported browsers, it may fail unexpectedly or produce incorrect results. You should include error handling or conditional logic to ensure your tests work as intended for all supported browsers.

  1. How can I find out which browsers are supported by Cypress?

You can check the Cypress documentation for a complete list of supported browsers and their configurations.

  1. Can I use Cypress.browser to interact with the browser programmatically, like navigating to a URL or clicking an element?

While Cypress.browser provides properties related to the browser instance, it does not allow you to perform actions directly on the browser. For that, you should use other Cypress APIs like cy.visit(), cy.get(), and others.

  1. How can I ensure my tests are robust and handle different scenarios effectively?

To create robust tests, consider using data-driven testing, parameterized tests, and test fixtures to cover a wide range of scenarios. Additionally, you should write clear and concise test cases that focus on specific functionality and edge cases.

Cypress.browser (Test Automation) | Test Automation | XQA Learn