Back to Test Automation
2026-01-208 min read

fail CI or block a pull request (Test Automation)

Learn fail CI or block a pull request (Test Automation) step by step with clear examples and exercises.

Why This Matters

In this lesson, we will learn how to use test automation tools like Selenium, Cypress, and Playwright with JavaScript to fail Continuous Integration (CI) or block pull requests based on UI coverage. This technique ensures that no new untested parts are added to your application without a deliberate decision.

Why This Matters

Test automation is crucial for maintaining the quality of software applications, especially in agile development environments where frequent code changes and releases occur. By setting up test automation to fail CI or block pull requests based on UI coverage, you can:

  1. Ensure that new features are thoroughly tested before they are merged into the main branch.
  2. Reduce the risk of regressions caused by untested code changes.
  3. Improve overall application stability and reliability.
  4. Save time for developers by automating repetitive testing tasks.
  5. Increase confidence in your codebase, making it easier to collaborate with other team members.

Prerequisites

To follow this lesson, you should have a basic understanding of:

  1. JavaScript programming language.
  2. Node.js and npm (Node Package Manager).
  3. One or more test automation tools like Selenium, Cypress, or Playwright.
  4. Git for version control and managing pull requests.
  5. CI/CD pipelines such as Jenkins, CircleCI, or GitHub Actions.

Core Concept

Test automation involves writing scripts that automatically execute a series of tests to verify the functionality, performance, and UI of an application. In this lesson, we will focus on using test automation to fail CI or block pull requests based on UI coverage.

Test Automation Tools

Selenium, Cypress, and Playwright are popular open-source test automation tools that support JavaScript. They allow you to write tests for web applications by simulating user interactions, validating page elements, and verifying application behavior.

Selenium

Selenium is a powerful and widely-used test automation framework that supports multiple programming languages (including JavaScript) and browsers. It provides APIs for creating, executing, and managing tests. However, Selenium can be slow due to its reliance on the browser's rendering engine.

Cypress

Cypress is a newer test automation tool that focuses on providing a faster and more reliable testing experience compared to Selenium. It runs directly in the browser and does not require a separate server or browser driver, which significantly improves test execution speed. Cypress also provides real-time reloading of the application during tests, making it easier to debug issues.

Playwright

Playwright is another modern test automation tool that supports multiple browsers and platforms (including Chromium, Firefox, and WebKit). It offers fast test execution times, network interception capabilities, and the ability to interact with web pages using both DOM and CSS selectors.

UI Coverage Reports

Test automation tools generate reports that show the coverage of your application's user interface (UI) in terms of tested elements, views, and pages. These reports can be used to identify untested parts of your application and set policies for failing CI or blocking pull requests based on UI coverage.

Failing CI or Blocking Pull Requests

To fail CI or block pull requests based on UI coverage, you need to integrate test automation tools with your CI/CD pipeline and configure them to generate UI coverage reports. Then, you can set up policies that trigger build failures or prevent merging of pull requests when overall coverage falls below a certain threshold or critical views regress.

Failing Builds

To fail builds based on UI coverage, you can use the test automation tool's API to retrieve the coverage report and check its status during the CI pipeline execution. If the coverage report indicates that certain elements, views, or pages are untested, the build will be marked as failed.

Blocking Pull Requests

To block pull requests based on UI coverage, you can integrate the test automation tool with your version control system (e.g., Git) and configure it to automatically generate UI coverage reports for each pull request. You can then set up policies that prevent merging of pull requests if they introduce untested elements, views, or pages.

Worked Example

In this example, we will use Cypress to create a test suite that fails CI based on UI coverage. We'll write tests for a simple web application with multiple pages and form inputs.

Setting Up the Project

  1. Install Node.js and npm if you haven't already: https://nodejs.org/en/download/
  2. Create a new directory for your project and navigate to it in the terminal.
  3. Run npm init to create a package.json file for your project.
  4. Install Cypress by running npm install cypress --save-dev.
  5. Initialize Cypress by running npx cypress open. This will create a cypress folder with the necessary configuration files and launch the Cypress Test Runner.

Writing Tests

Create a new file called ui_coverage.spec.js in the cypress/integration directory. In this file, we'll write tests for our simple web application.

describe('UI Coverage', function() {
it('Checks coverage of home page', function() {
cy.visit('/');

// Add assertions here to verify that all elements on the home page are tested
});

it('Checks coverage of about page', function() {
cy.visit('/about');

// Add assertions here to verify that all elements on the about page are tested
});

// Add more tests for other pages and form inputs as needed
});

Configuring UI Coverage Reports

To generate UI coverage reports, you need to add a few configurations to your cypress.json file:

{
"env": {
"baseUrl": "http://your-app-url"
},
"reporter": "mochawesome",
"reporterOptions": {
"reportDir": "cypress/reports/ui_coverage",
"overwrite": false,
"html": false,
"json": true
}
}

Failing CI Based on UI Coverage

To fail CI based on UI coverage, you can use the Cypress API to retrieve the JSON UI coverage report and check its status during the CI pipeline execution. Here's an example using GitHub Actions:

  1. Create a new file called .github/workflows/ci.yml in your project root directory.
  2. Add the following content to the file:
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Fetch the entire history to generate UI coverage reports for pull requests
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 14
- run: npm ci
- run: npm run cypress run --headed --record --reporter mochawesome --reporter-options reportDir=cypress/reports/ui_coverage,overwrite=false,json=true
- name: Check UI Coverage
id: check_ui_coverage
uses: actions/github-script@v4
with:
script: |
const fs = require('fs');
const reportPath = 'cypress/reports/ui_coverage/mochawesome.json';
const coverageData = JSON.parse(fs.readFileSync(reportPath, 'utf8'));
if (coverageData.failed === 0) {
throw new Error('UI Coverage is not complete');
}
- name: Publish Artifacts
uses: actions/upload-artifact@v2
with:
name: ui_coverage_report
path: cypress/reports/ui_coverage

This workflow will run the tests, generate UI coverage reports, and fail the build if the reports indicate that all elements are not tested. You can customize the threshold for failing builds based on your application's requirements.

Common Mistakes

  1. Forgetting to add assertions in test cases to verify that all elements on a page are tested.
  2. Not configuring UI coverage reports properly, resulting in incomplete or incorrect coverage data.
  3. Setting too strict UI coverage thresholds, causing unnecessary build failures and delays.
  4. Failing to integrate test automation tools with CI/CD pipelines and version control systems.
  5. Ignoring UI coverage reports during development, leading to untested code changes being merged into the main branch.

Practice Questions

  1. How can you configure Cypress to generate UI coverage reports for each pull request?
  2. What steps are required to fail a build based on UI coverage using GitHub Actions and Cypress?
  3. How can you set up policies to block pull requests based on UI coverage in GitHub?
  4. Why is it important to integrate test automation tools with CI/CD pipelines and version control systems?
  5. What are some common mistakes when setting up test automation to fail CI or block pull requests based on UI coverage?

FAQ

Q: Can I use Selenium instead of Cypress for failing CI or blocking pull requests based on UI coverage?

A: Yes, you can use Selenium to achieve the same goal. However, Cypress offers faster test execution times and a more user-friendly API compared to Selenium.

Q: Can I use Playwright instead of Cypress for failing CI or blocking pull requests based on UI coverage?

A: Yes, you can use Playwright as well. It supports multiple browsers and offers fast test execution times, network interception capabilities, and the ability to interact with web pages using both DOM and CSS selectors.

Q: How can I set up policies to fail builds or block pull requests based on UI coverage in Jenkins?

A: To set up policies in Jenkins, you need to integrate your test automation tool with Jenkins and configure it to generate UI coverage reports. Then, you can use Jenkins plugins like the Maven Surefire Report Plugin or the TestNG Report Generator Plugin to parse the UI coverage reports and trigger build failures based on the report data.

Q: How can I improve test automation performance when failing CI or blocking pull requests based on UI coverage?

A: To improve test automation performance, you can optimize your test suites by focusing on high-risk areas, using selectors that target specific elements instead of entire pages, and minimizing the use of network requests and external APIs in tests. Additionally, you can parallelize tests to run multiple tests concurrently, reducing overall execution time.

Q: How can I handle false positives when failing CI or blocking pull requests based on UI coverage?

A: False positives can occur when test automation tools incorrectly identify elements as untested. To minimize false positives, you should ensure that your tests are robust and reliable, use selectors that target specific elements instead of entire pages, and regularly review and update your test suites to reflect changes in the application's UI. Additionally, you can configure your test automation tool to ignore certain elements or views that are known to be stable and unlikely to change.

fail CI or block a pull request (Test Automation) | Test Automation | XQA Learn