Skip to main content

JUnit-XML based Cypress report

Using the terminal, you can create JUnit-XML based reports from Cypress and upload them to Test Management.

Executing a Test Case with Cypress

You can initiate a Cypress project with this git repository. Use this sample code to execute a Test Case.

describe('First test suite', () => {

  it('Verify Browserstack logo is visible', () => {

      cy.visit('https://www.browserstack.com/');
      cy.get('#logo').should('be.visible');

  })

  it('Verify Header is present', () => {

      cy.visit('https://www.browserstack.com/');

      cy.get('h1').first().should('be.exist');

  })

  it('Verify Product menu are present', () => {

      cy.visit('https://www.browserstack.com/');


      cy.get('#product-menu-toggle').first().should('be.exist');

      cy.get('#developers-menu-toggle').first().should('be.exist');

      cy.get('#developers-menu-toggle').first().should('be.exist');

  })

  it('Verify menu Links are  present', () => {

      cy.visit('https://www.browserstack.com/');


      cy.get("a[title='Pricing']").first().should('be.exist');

      cy.get("a[title='Sign In']").first().should('be.exist');

      cy.get("a[title='Live for Teams']").first().should('be.exist');

  })

})


describe('Second Test Suite', () => {
  it('second bstack test', () => {
    cy.visit('https://example.cypress.io')
  })
})

Create test report with Cypress

Install the cypress-junit-reporter package.

npm install cypress-junit-reporter --save-dev

Enable the JUnit-reporter in the configuration file.

const { defineConfig } = require('cypress')
module.exports = defineConfig({
  reporter: 'junit',
  reporterOptions: {
    mochaFile: 'results/test-results.xml',
    outputs: true,
    testCaseSwitchClassnameAndName: true
  }
})

Sample JUnit-XML report generated from Cypress in the results folder.

<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Mocha Tests" time="26.088" tests="5" failures="3">
  <testsuite name="Root Suite" timestamp="2023-06-29T14:58:33" tests="0" file="cypress/e2e/first-test.cy.js" time="0.000" failures="0">
  </testsuite>
  <testsuite name="First test suite" timestamp="2023-06-29T14:58:33" tests="4" time="23.182" failures="3">
    <testcase name="First test suite Verify Browserstack logo is visible" time="0.000" classname="Verify Browserstack logo is visible">
      <failure message="Timed out retrying after 4000ms: Expected to find element: `#logo`, but never found it." type="AssertionError"><![CDATA[AssertionError: Timed out retrying after 4000ms: Expected to find element: `#logo`, but never found it.
    at Context.eval (webpack:///./cypress/e2e/first-test.cy.js:6:22)]]></failure>
    </testcase>
    <testcase name="First test suite Verify Header is present" time="2.142" classname="Verify Header is present">
    </testcase>
    <testcase name="First test suite Verify Product menu are present" time="0.000" classname="Verify Product menu are present">
      <failure message="Timed out retrying after 4000ms: Expected to find element: `#product-menu-toggle`, but never found it." type="AssertionError"><![CDATA[AssertionError: Timed out retrying after 4000ms: Expected to find element: `#product-menu-toggle`, but never found it.
    at Context.eval (webpack:///./cypress/e2e/first-test.cy.js:23:9)]]></failure>
    </testcase>
    <testcase name="First test suite Verify menu Links are  present" time="0.000" classname="Verify menu Links are  present">
      <failure message="Timed out retrying after 4000ms: Expected to find element: `a[title=&apos;Sign In&apos;]`, but never found it." type="AssertionError"><![CDATA[AssertionError: Timed out retrying after 4000ms: Expected to find element: `a[title='Sign In']`, but never found it.
    at Context.eval (webpack:///./cypress/e2e/first-test.cy.js:38:9)]]></failure>
    </testcase>
  </testsuite>
  <testsuite name="Second Test Suite" timestamp="2023-06-29T14:58:56" tests="1" time="2.875" failures="0">
    <testcase name="Second Test Suite second bstack test" time="0.576" classname="second bstack test">
    </testcase>
  </testsuite>
</testsuites>

Steps to upload the JUnit-XML test report

Open the project directory in the Terminal and load the variables.

export TEST_MANAGEMENT_API_TOKEN="*************28a42"
export TEST_MANAGEMENT_PROJECT_NAME="<Project Name>"
export JUNIT_XML_FILE_PATH="<Report Path>"
export TEST_RUN_NAME="<Test Run Name"
Note: You can get your TEST_MANAGEMENT_API_TOKEN from the Active API Key section in the Settings of BrowserStack Test Management.

Upload the JUnit-XML test report using curl command.

curl -k -X POST https://test-management.browserstack.com/api/v1/import/results/xml/junit \
-u $TEST_MANAGEMENT_API_TOKEN \
-F project_name="$TEST_MANAGEMENT_PROJECT_NAME" \
-F "file_path=@$JUNIT_XML_FILE_PATH" \
-F test_run_name="$TEST_RUN_NAME"
Note: Report upload will take a few seconds to about a minute depending on the size of your test-suite. Do not interrupt the cURL command until you get the response back.

You can also upload JUnit-XML test report with Cypress using CI/CD.

Access the test run report from the console generated URL

After your report is successfully uploaded, you see a message similar to the following in your terminal. This message also generates a URL with "url" parameter that you can use to access your automated test run.

{"message":"File uploaded successfully.","url":"https://test-management.browserstack.com/projects/<project id>/test-runs/<test run id>","success":true}

View test run report in Test Management

  1. Log in to the BrowserStack Test Management dashboard.
  2. Select the relevant project that has the test report uploaded.
  3. Click Test Runs.
  4. Open the Test Run generated from automation test exectution.
  5. You will find all the test cases with their respective results here.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Download Copy