Shelex / cypress-allure-plugin

cypress plugin to use allure reporter api in tests
https://shelex.github.io/cypress-allure-plugin-example/
Apache License 2.0
156 stars 43 forks source link
allure allure-report allure2 cypress cypress-io cypress-plugin

cypress-allure-plugin

SWUbanner

Plugin for integrating allure reporter in Cypress with support of Allure API.

Build Downloads semantic-release
version License

Installation

Setup

Cypress v10+

Cypress before v10

Autocompletion

 "compilerOptions": {
        "allowJs": true,
        "baseUrl": "./",
        "types": ["@shelex/cypress-allure-plugin"],
        "noEmit": true
    },

Configuration

Plugin is customizable via Cypress environment variables:

General

env variable name description default
allure enable Allure plugin false
allureReuseAfterSpec reuse existing after:spec event listener which is mandatory for handling test results. may be already used by other plugins, and if it is your case (see #150) - just set to true false
allureResultsPath customize path to allure results folder allure-results
allureClearSkippedTests remove skipped tests from report false

Steps

env variable name description default
allureLogCypress log cypress chainer (commands) and display them as steps in report true
allureAvoidLoggingCommands specify names of cypress commands to not be logged as allure steps []
allureLogGherkin log gherkin steps from cucumber-preprocessor inherits allureLogCypress value if not specified directly

Attachments

env variable name description default
allureAttachRequests attach cy.request and cy.api headers, body, response headers, response body false
allureSkipAutomaticScreenshots do not attach screenshots automatically (for those who uses custom scripts, etc.) false
allureOmitPreviousAttemptScreenshots remove screenshots from previous retries false
allureAddVideoOnPass attach video for passed tests, works when video is enabled false

Links

env variable name description default
tmsPrefix just a prefix substring or pattern with * for links to test management system ``
issuePrefix prefix for links to bug tracking system ``

No idea

env variable name description default
allureAddAnalyticLabels add framework and language labels (allure uses it for analytics only, have no idea why you need it) false

These options could be passed in multiple ways, you can check docs.
But also you can use allure.properties file (however allure=true is still required to be passed as cypress env variable):

allure.results.directory=allure-results
allure.link.issue.pattern=https://example.com/project/test/issue/
allure.link.tms.pattern=https://example.com/testcases/TEST/
allure.reuse.after.spec=false
allure.clear.skipped=false
allure.cypress.log.commands=true
allure.cypress.log.gherkin=true
allure.cypress.log.requests=true
allure.skip.automatic.screenshot=false
allure.omit.previous.attempt.screenshot=false
allure.video.passed=false
allure.analytics=false

Execution

npx cypress run --env allure=true
Cypress.Allure.reporter.runtime.writer;

How to open report

Assuming allure is already installed:

Examples

See cypress-allure-plugin-example project, which is already configured to use this plugin, hosting report as github page and run by github action. It has configuration with complete history (allure can display 20 build results ) with links to older reports and links to CI builds.

There are also existing solutions that may help you prepare your report infrastructure:

Troubleshooting

Answers to common questions/issues:

Debugging

API

There are three options of using allure api inside tests:

  1. Using interface from Cypress.Allure.reporter.getInterface() - synchronous
const allure = Cypress.Allure.reporter.getInterface();
allure.feature('This is our feature');
allure.epic('This is epic');
allure.issue('google', 'https://google.com');
  1. Using Cypress custom commands, always starting from cy.allure() - chainer
cy.allure()
    .feature('This is feature')
    .epic('This is epic')
    .issue('google', 'https://google.com')
    .parameter('name', 'value')
    .tag('this is nice tag', 'as well as this');
  1. Using Cypress-cucumber-preprocessor with cucumber tags:
@testID("id_of_test_for_testops")
@parentSuite("someParentSuite")
@suite("someSuite")
@subSuite("someSubSuite")
@epic("thisisepic")
@feature("nice")
@story("cool")
@severity("critical")
@owner("IAMOwner")
@issue("jira","JIRA-1234")
@tms("tms","TC-1234")
@link("other","url")
@someOtherTagsWillBeAddedAlso
Scenario: Here is scenario
...

Allure API available:

Cypress commands as steps

Commands are producing allure steps automatically based on cypress events and are trying to represent how code and custom commands are executed with nested structure.
Moreover, steps functionality could be expanded with:

To disable tracking of specific cypress commands to be not logged as steps in allure you can set env variable allureAvoidLoggingCommands which should contain an array of command names to be ignored, for example:

allureAvoidLoggingCommands: ["intercept", "myCustomCommand"]

To disable tracking of all cypress commands for specific code block you can use logCommandSteps api method:

// disable tracking cypress commands:
cy.allure().logCommandSteps(false);
cy.login(username, password);
// enable tracking cypress commands back again:
cy.allure().logCommandSteps();

Screenshots and Videos

Screenshots are attached automatically, for other type of content feel free to use testAttachment (for current test), attachment (for current executable), fileAttachment (for existing file).

Videos are attached for failed tests only from path specified in cypress config videosFolder and in case you have not passed video=false to Cypress configuration. In case you want to attach videos for passed tests please use allureAddVideoOnPass=true env variable.

It is done with the help of After Spec API. It will be used for:

In lower versions some other heuristics would be used, but they are not as reliable as after:spec.

Test name duplicates

By default Allure calculates hash from test title to identify test and show its' proper previous results.
This may lead to tests having the same name being counted by allure as retries of the same test. There are several ways to avoid this situation:

Suite structuring

Allure support 3 levels of suite structure:

They are defined automatically by structure passed from cypress mocha test object with titles of each parent. So an array of names of describe blocks is just transformed into: [parentSuite, suite, "subsuite1 -> subsuite2 -> ..."]

However, since v2.29.0 you can modify the strategy of defining names for structuring the tests by overwriting the function in support/index file using Cypress.Allure.reporter.getInterface().defineSuiteLabels which will accept your function:

// remove all describe block names and leave just last one:
Cypress.Allure.reporter
    .getInterface()
    .defineSuiteLabels((titlePath, fileInfo) => {
        return [titlePath.pop()];
    });

This function will have 2 arguments. titlePath is that array of describe names, and fileInfo is a parsed representation of a filepath for cases when you want to include folder or filename into some names, or just wrap suites in folders, or implement any of your ideas how to structure tests in reports.

// supplement parentSuite name with folder name
Cypress.Allure.reporter
    .getInterface()
    .defineSuiteLabels((titlePath, fileInfo) => {
        const [parentSuite, suite, ...subSuites] = titlePath;
        return [`${fileInfo.folder} | ${parentSuite}`, suite, ...subSuites];
    });
// make folder name a parentSuite:
Cypress.Allure.reporter
    .getInterface()
    .defineSuiteLabels((titlePath, fileInfo) => {
        return [fileInfo.folder, ...titlePath];
    });
// remove any other describe blocks and just show last one:
Cypress.Allure.reporter
    .getInterface()
    .defineSuiteLabels((titlePath, fileInfo) => {
        return [titlePath.pop()];
    });
// remove describe names and just place tests in folder -> filename structure:
Cypress.Allure.reporter
    .getInterface()
    .defineSuiteLabels((titlePath, fileInfo) => {
        return [fileInfo.folder, fileInfo.name];
    });

Gherkin and links

It is posible to pass tms link or issue link with tags tms("ABC-111") and issue("ABC-222"). However, that will not work well with Scenario Outlines which may have different examples being linked to different tasks or test cases in tms. So, plugin will also parse your scenario outlines with examples and in case header in table will be tms or issue - it will add it as link to report.

    Scenario Outline: Some scenario
        Given User want to link test <number> to tms
        When User creates examples table with tms and issue headers
        Then Links will be added to allure
        Examples:
            | tms    | issue   | number |
            | TEST-1 | JIRA-11 | 1      |
            | TEST-2 | JIRA-22 | 2      |

VS Code Helper plugin for cypress + cucumber

In case you are using VS Code and Cypress Helper (latest) extension, it has configuration for allure cucumber tags autocompletion in feature files:

"cypressHelper.cucumberTagsAutocomplete": {
        "enable": true,
        "allurePlugin": true,
        "tags": ["focus", "someOtherTag"]
    }

Credits

Thanks to Serhii Korol who made Allure-mocha reporter. Integration with Cypress internal mocha runner was based on that solution.

License

Copyright 2020-2023 Oleksandr Shevtsov ovr.shevtsov@gmail.com.
This project is licensed under the Apache 2.0 License.