Overview

In this tutorial, we will create some tests in JavaScript using CodeceptJS and we'll use mocha-junit-reporter to generate a compatible JUnit XML report.

Requirements

  • codeceptJS
  • webdriverio
  • selenium-standalone
  • mocha-junit-reporter


npm install -g codeceptjs
npm -g install  webdriverio@^4.0.0
npm i -g mocha-junit-reporter
npm install -g selenium-standalone
selenium-standalone install
selenium-standalone start


Code

The following code and the setup instructions were inspired by CodeceptJS startup tutorial.


codecept.json
{
  "tests": "./*_test.js",
  "timeout": 10000,
  "output": "./output",
  "helpers": {
    "WebDriverIO": {
      "url": "http://www.google.com",
      "browser": "chrome"
    }
  },
  "include": {
    "I": "./steps_file.js"
  },
  "bootstrap": false,
  "mocha": {
    "reporterOptions": {
        "mochaFile": "output/result.xml"
    }
  },
  "name": "codeceptjs"
}


Our Feature contains 3 Scenarios, where the last one was intended to fail on purpose.

My First Test_test.js
Feature('My First Test');

Scenario('google welcome page', (I) => {
  I.amOnPage('/');
  I.see('Google offered in');
});


Scenario('search for Xray should return link to Marketplace', (I) => {
  I.amOnPage('/');
  I.dontSeeElement('#todo-count');
  I.fillField({id: 'lst-ib'}, 'Xray test management');
  I.pressKey('Enter');
  I.see('Xray - Test Management for Jira | Atlassian Marketplace', '#rcnt');
});


Scenario('search for tests should return Xray', (I) => {
  I.fillField({id: 'lst-ib'}, 'tests');
  I.pressKey('Enter');
  I.see('Xray - Test Management for Jira | Atlassian Marketplace', '#rcnt');
});


After running the tests and generating the JUnit XML report (e.g., output/result.xml), it can be imported to Xray (either by the REST API or through the Import Execution Results action within the Test Execution).

 codeceptjs run --reporter mocha-junit-reporter



Each Scenario is mapped to a Generic Test in Jira, and the Generic Test Definition field contains the name of the Scenario concatenated with the name of the Feature.

The Execution Details of the Generic Test contains information about the Test Suite, which in this case corresponds to the name of the Feature.

References