Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

cypress-testrail

boxblinkracer96.6kMIT2.10.0

Easy and decoupled Cypress TestRail reporter

testrail, cypress, testautomation

readme

(Super Easy) Cypress TestRail Integration

Build Status NPM Downloads GitHub release (latest by date) NPM License

This integration helps you to automatically send test results to TestRail. And yes, super easy and simple!

Add your TestRail credentials in Cypress, decide which test results should be sent to TestRail and you're done!

1. Installation

npm i cypress-testrail --save-dev

Please keep in mind that this integration requires a minimum version of Node.js v13.0.

Note: Versions of Node < Node 13 may work but will require enabling experimental-modules flag.

2. Setup Wizard

The integration has a CLI command that you can use to build your configuration in an interactive way.

Run it with this command and enter your data:

./node_modules/.bin/cypress-testrail 

Please copy the displayed JSON structure of that command to your cypress.env.json file.

You can of course also build such a JSON manually. In addition to this, you can also use ENV variables or process.env variables. Please see the section on variables below for more.

Here is a sample of a JSON from the CLI command.

{
  "testrail": {
    "domain": "my-company.testrail.io",
    "username": "myUser",
    "password": "myPwd",
    "runId": "R123",
    "screenshots": true
  }
}

Please note that you can use both, the password of your TestRail user, or a generated API key for the password field.

3. Execution Modes

The integration has 2 different modes, that you can select while running our Setup CLI command.

3.1 Mode A: Send results to one or more runs in TestRail

With this mode, all results are fired against an existing Test Run or a list of Test Runs in TestRail. This is a good option if you have already prepared your plan in TestRail and just need to have Cypress doing the work for you.

Please keep in mind, that the provided run must not be closed, so that the TestRail API allows you to send results to it.

Results will only be saved, if the sent TestCaseID is existing in that run inside TestRail.

3.2 Mode B: Create new Run in TestRail for every Cypress run

Sometimes you want to create test runs dynamically inside TestRail. For this, just assign the ProjectID and the optional MilestoneID or SuiteId of TestRail in your configuration.

The integration will then start a new run in TestRail and send the results to this one. It is also possible to provide a custom (or dynamically created) name for the new test run.

4. Register Plugin

Just place this line in your plugins/index.js file. There's nothing more that is required to register the TestRail reporter.

const TestRailReporter = require('cypress-testrail');

module.exports = (on, config) => {
    new TestRailReporter(on, config).register();
    return config
}

In addition to this, you can register the reporter with a custom comment. That comment will then be sent to the TestRail result along with the other metadata, such as Cypress version, browser, baseURL and more.

const customComment = 'AUT v' + Cypress.env('MY_APP_VERSION');

new TestRailReporter(on, config, customComment).register();

If you are running Cypress 10 and higher, then there is no classic plugin/index.js file anymore. You can of course still use it. The new cypress.config.js has a configuration option called setupNodeEvents. That one acts as the perfect entrypoint to either directly start the configuration, or just load a separate file.

e2e: {
    // We've imported your old cypress plugins here.
    // You may want to clean this up later by importing these.
    setupNodeEvents(on, config)
    {
        return require('./cypress/plugins/index.js')(on, config)
    }
,
}

If you want to register the plugin for using Cypress in "Open" mode, please also enable experimentalInteractiveRunEvents in cypress.config.js

5. Map Test Cases

We're almost done. You can now map TestRail test cases to your Cypress tests. Please use the TestRail case ID as a prefix inside the Cypress test title. The plugin will automatically extract it, and send the results to your test run in TestRail. The Case ID needs to be at the beginning and separated with an : from the rest of the title.

You can also add multiple Case IDs before the :. Results will be sent for all found test cases.

it('C123: My Test for TestRail case 123', () => {

    cy.get('#sw-field--name').type('John');
    // ...
    // ...

})

it('C123 C54 C36: My Test for multiple TestRail case IDs', () => {

    cy.get('#sw-field--name').type('John');
    // ...
    // ...

})

That's it!

You can now start Cypress (restart after config changes), and all your results should be sent to TestRail as soon as your mapped tests pass or fail!

6. Advanced Features

6.1 Sending Screenshots to TestRail

You can automatically send the latest failure screenshot of Cypress to TestRail. This is not enabled by default. Just enable it, and it will automatically work. Once enabled, the latest failed screenshot is sent to TestRail.

{
  "testrail": {
    "screenshots": true
  }
}

If you want to send all failed screenshots to TestRail, just enable the additional (optional) feature. This will send all failed screenshots of all attempts in Cypress to TestRail.

{
  "testrail": {
    "screenshots": true,
    "screenshotsAll": true
  }
}

6.2 Using multiple Cypress plugins

Let's start with the most important thing: The problem with the Cypress event listeners.

This integration uses events like "before:run" and more. Unfortunately Cypress does not have a list of subscribed event handlers, that means if multiple plugins are using the same event, then they will overwrite each other.

Thanks to @bahmutov we have a solution for this problem (https://github.com/bahmutov/cypress-on-fix).

Please install his package "cypress-on-fix" as described on his website.

6.3 Cucumber Gherkin Support

This integration works with both, plain Cypress tests but also in combination with the Cucumber plugin and Gherkin documents (https://github.com/badeball/cypress-cucumber-preprocessor).

Once installed, you can easily prefix the titles of your Scenario entries with the TestRail case ID. Internally they are converted into Cypress tests, which means everything works as with the plain usage of tests.

Feature: Blog Page Features

Scenario: C123: Filter blog posts by tags
Given I am on the blog page
When I click on tag "testing"
Then I see tag "testing" as title of the page

Installation

Please install the cucumber plugin for Cypress as described on their website. Also consider the problem of having multiple plugins using the same event listeners as described above.

Once done, you need to configure Cucumber, our Cypress TestRail integration and the cypress-on-fix package. Here is a sample configuration with all 3 plugins being used (please note, this is just a sample):

const createBundler = require('@bahmutov/cypress-esbuild-preprocessor');
const {addCucumberPreprocessorPlugin} = require('@badeball/cypress-cucumber-preprocessor');
const {createEsbuildPlugin} = require('@badeball/cypress-cucumber-preprocessor/esbuild');
const {defineConfig} = require('cypress');
const TestRailReporter = require('cypress-testrail');

module.exports = defineConfig({
    e2e: {
        // sample to configure both, gerhkin documents and plain cypress tests
        specPattern: ['cypress/e2e/**/*.feature', 'cypress/e2e/**/*.js'],

        async setupNodeEvents(cypressOn, config) {
            // prepare the fix for event listeners
            const on = require('cypress-on-fix')(cypressOn)

            // configure cucumber
            await addCucumberPreprocessorPlugin(on, config);
            on('file:preprocessor', createBundler({
                plugins: [createEsbuildPlugin(config)],
            }));

            // configure TestRail
            new TestRailReporter(on, config).register();

            return config
        },
    },
});

That's it! When you now run tests based on Gherkin documents, the TestRail integration will automatically send the results to TestRail.

6.4 Get data of new TestRail runs

When using the "Create Run Mode", the integration will now create a new file called created_run.json. This is immediately created after the run was created in TestRail and contains data such as the ID, name and more.

You can use this file to immediately read and use data of the created run in other steps of your CI pipeline, while Cypress is running.

7. Variables

This is a list of all available variables and their explanation.

The list shows you the ENV variable name as well as their JSON structure name. You can use all variables in both scopes.

Examples on how to use it are below the list.

ENV / process.env JSON Required Description
CYPRESS_TESTRAIL_DOMAIN testrail.domain yes TestRail domain
CYPRESS_TESTRAIL_USERNAME testrail.username yes TestRail username
CYPRESS_TESTRAIL_PASSWORD testrail.password yes TestRail password or TestRail API key.
CYPRESS_TESTRAIL_SCREENSHOTS testrail.screenshots no Send last screenshot of failed test.
Values: true/false
CYPRESS_TESTRAIL_SCREENSHOTS_ALL testrail.screenshotsAll no Send all screenshots of failed test. (requires screenshots to be enabled).
Values: true/false
CYPRESS_TESTRAIL_RUN_ID testrail.runId yes (Mode A) TestRail RunID to fire against, e.g. R123
CYPRESS_TESTRAIL_RUN_IDS testrail.runIds yes (Mode A) TestRail RunIDs to fire against, e.g. ["R123", "R456"]. Either provide single runID or this list. Send comma separated as ENV variable from CLI (xxx="R1,R2"
CYPRESS_TESTRAIL_PROJECT_ID testrail.projectId yes (Mode B) TestRail ProjectID, e.g. P45
CYPRESS_TESTRAIL_MILESTONE_ID testrail.milestoneId yes (Mode A) TestRail MilestoneID, e.g. M4
CYPRESS_TESTRAIL_SUITE_ID testrail.suiteId yes/no (Mode B) TestRail SuiteID, e.g. S8.
Some projects might require this!
CYPRESS_TESTRAIL_RUN_NAME testrail.runName no (Mode B) Template for the names of created runs. You can provide a fixed text but also use dynamic variables.

Variables: (__datetime__) => generates e.g. "01/04/2022 12:45:00"
CYPRESS_TESTRAIL_RUN_INCLUDE_ALL testrail.runIncludeAll no Include all test cases in test run creation.
Values: true/false
CYPRESS_TESTRAIL_RUN_CLOSE testrail.closeRun no (Mode B) Automatically close test runs.
Values: true/false
CYPRESS_TESTRAIL_IGNORE_PENDING testrail.ignorePending no If enabled, pending Cypress tests will not be sent to TestRail.
Values: true/false

7.1 Use on CLI

To provide variables on CLI just expose them before executing your actual command.

CYPRESS_TESTRAIL_PROJECT_ID=2 CYPRESS_TESTRAIL_MILESTONE_ID=15 ./node_modules/.bin/cypress run 

7.2 Use in cypress.env.json

You can also provide the variables in a JSON structure like this inside your cypress.env.json file.

{
    "testrail": {
        "domain": "",
        "username": "",
        "password": "",
        "screenshots": false,
        "projectId": "",
        "milestoneId": "",
        "suiteId": "",
        "runName": "",
        "runIncludeAll": false,
        "closeRun": false,
        "screenshots": false
    }
}

8. Copying / License

This repository is distributed under the MIT License (MIT).

changelog

Changelog

All notable changes to this project will be documented in this file.

[2.10.0]

Added

  • Added description to README how to use an Api Key instead of the user password.
  • Added documentation on how to use Cucumber and Gherkin documents with the integration.
  • Added support for process.env variables. All configuration settings will now also consider entries in process.env.
  • Added new option to ignore pending tests. If ignored, these tests will not be sent to TestRail. Please see README for more.
  • Added new created_run.json file that is created in "Create Run Mode". This allows to immediately read and use data of the created run in other steps of your CI pipeline, while Cypress is running.

Fixed

  • Fixed problem with wrong elapsed_time. When skipped tests were detected coming in Cypress v13, it led to a wrong calculation of the elapsed time.

[2.9.0]

Changed

  • Use the Cypress test title instead of the plain "Tested by Cypress" in the TestRail comments (thx @vijigit)
  • Updated Axios dependency to version ^1.6.2 (thx @MelaAxel)

Fixed

  • Fixed a broken elapsedTime error with Cypress v13 in combination with undefined values for the duration of tests.

[2.8.2]

Fixed

  • Fixed broken screenshots upload in Cypress 13.

[2.8.1]

Fixed

  • Fixed broken elapsedTime calculation with Cypress v13.

[2.8.0]

Added

  • Skipped tests in Cypress will now be sent as "blocked" to TestRail (only in createTestRun mode).
  • Add option to provide a list of TestRail RunIDs with runIDs. This will send all test results to all provided run IDs.

Fixed

  • Fixed crash with missing browser when running in UI mode with experimentalInteractiveRunEvents turned on.

[2.7.1]

Added

  • Added better test case extraction by removing comma character, if accidentally existing (thx @asalgado87)
  • Add new output, if the integration is not correctly configured.

[2.7.0]

Added

  • Added new configuration runIncludeAll that automatically adds all test cases to test runs when creating dynamic runs. (thx @williamatpaper)
  • Added new configuration screenshotsAll that sends all screenshots of all attempts of a failed test to TestRail. ( thx @williamatpaper)
  • The integration now also works if you have accidentally provided "http://" and "https://" within your TestRail domain.

[2.6.0]

Changed

  • Results are now sent as "batch" request to TestRail at the end of the file. This safes API requests by only sending 1 request for a spec file (excluding screenshot requests).
  • Changed the output color of debug texts to gray.

[2.5.0]

Added

  • Added new CLI Setup Wizard to build your configuration in an interactive way. (see README)
  • The integration will now automatically send the screenshots to TestRail for every failed test. This is all automatically done as soon as the new variable is enabled (see README).
  • Added option to provide optional "suiteId" when creating new runs. If you have a project with suites, this parameter is required.

Changed

  • new README with better instructions and overview

[2.4.0]

Changed

  • Tests with status "pending" had been sent as "failed" to TestRail. These test results will now be skipped and not sent to TestRail.

Fixed

  • Improved error response handling from TestRail API. Some errors led to an "undefined reading 'status'" exception. There should still be no error, but now we should know better what happens.

[2.3.1]

  • Fixed problem with "ColoredConsole.debug" is not a function in Create-TestRun mode. it should be debug, and not debugg

[2.3.0]

Added

  • It's now possible to add multiple case ids to a test. Just do it like this "C1 C2 C3: my description of the test".
  • Added new colored console outputs for successful or failed API calls.
  • Improve the output of failed API calls to TestRail. The error message is now extracted and shown in a more convenient way.

[2.2.1]

Fixed

  • Fixed cast problem with string replace function when using CLI ENV variables with INT values.

[2.2.0]

Changed

  • RunIDs can now also be provided with a leading "R" and spaces. The integration will automatically remove it.
  • ProjectIDs can now also be provided with a leading "P" and spaces. The integration will automatically remove it.
  • MilestoneIDs can now also be provided with a leading "M" and spaces. The integration will automatically remove it.

Fixed

  • Fixed problem with missing axio package when doing an installation from scratch

[2.1.1]

Changed

  • Elapsed times with "0s" will not be sent anymore. TestRail doesn't like this.
  • Changed runName placeholder from %datetime% to datetime to support better usages with bash for CI pipelines

[2.1.0]

Added

  • Add mode to create TestRuns inside TestRail with custom names, placeholders and more
  • Added option to provide config data as Cypress ENV variables

[2.0.0]

Breaking Changes

We had to change the way how the integration is registered. The new way allows to read additional Cypress events and gather information about browsers, versions and more. Please see the UPGRADE.md file for more about this topic.

Added

  • Add collected data to TestRail result such as Cypress version, browser, baseURL, system and Spec file.
  • Add option to set a custom comment that is passed on to the result in TestRail. You can use this to describe your AUT version, commit hash or more.

Changed

  • Changed the way how the integration is registered.
  • TestRail results are now sent at the end of a spec file, and not directly after a single test anymore.

[1.1.0]

Added

  • Add Cypress error message to result comment
  • Add elapsed time to result

Changed

  • Result comment is now trimmed to keep it neat ;)
  • Improved extraction of Case IDs in Cypress titles by adding a trim to the title.

[1.0.0]

Added

  • Initial version of the Cypress TestRail Integration