Skip to content

Commit

Permalink
[DEVX-1154] Set default value for failure related fields in junit (#112)
Browse files Browse the repository at this point in the history
* [DEVX-1154] Set default value for failure related fields in junit

* Add unit test for generating junit file

* remove debug code
  • Loading branch information
tianfeng92 authored Aug 25, 2021
1 parent 190f02d commit 38d76c6
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ run.yaml
tests/fixtures/basic-js/test-results/
tests/fixtures/basic-js/__assets__/
bundle/
__assets__/
__assets__/

!tests/unit/src/__assets__/
13 changes: 12 additions & 1 deletion src/playwright-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ function generateJunitfile (cwd, suiteName, browserName, platformName) {
totalSkipped += +testsuite._attributes.skipped || 0;
totalTime += +testsuite._attributes.time || 0;

if (!Array.isArray(testsuite.testcase)) {
testsuite.testcase = [testsuite.testcase];
}
for (let j = 0; j < testsuite.testcase.length; j++) {
const testcase = testsuite.testcase[j];
if (testcase.failure) {
testsuite.testcase[j].failure._cdata = testcase.failure._text || '';
delete testsuite.testcase[j].failure._text;
}
}

testsuite._attributes.id = i;
let timestamp = new Date(+testsuite._attributes.timestamp);
testsuite._attributes.timestamp = timestamp.toISOString();
Expand Down Expand Up @@ -334,4 +345,4 @@ if (require.main === module) {
});
}

module.exports = { run };
module.exports = { run, generateJunitfile };
30 changes: 30 additions & 0 deletions tests/unit/src/__assets__/expected_junit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<testsuites name="Firefox using global mode" tests="1" failures="1" skipped="0" errors="0" time="4.143">
<testsuite name="tests/example.test.js" timestamp="2021-08-25T23:14:56.785Z" hostname="" tests="1" failures="1" skipped="0" time="4.143" errors="0" id="0">
<testcase name="should verify title of the page" classname="tests/example.test.js:3:1 › [firefox] should verify title of the page" time="4.143">
<failure message="example.test.js:3:1 should verify title of the page" type="FAILURE"><![CDATA[
tests/example.test.js:3:1 › [firefox] should verify title of the page ============================
Error: expect(received).toBe(expected) // Object.is equality
Expected: "Swag Labs111"
Received: "Swag Labs"
3 | test('should verify title of the page', async ({ page }) => {
4 | await page.goto('https://www.saucedemo.com/');
> 5 | expect(await page.title()).toBe('Swag Labs111');
| ^
6 | });
7 |
at /home/seluser/__project__/tests/example.test.js:5:32
at WorkerRunner._runTestWithBeforeHooks (/home/seluser/node_modules/@playwright/test/lib/test/workerRunner.js:290:13)
]]></failure>
</testcase>
<properties>
<property name="platformName" value="Linux"/>
<property name="browserName" value="firefox"/>
</properties>
</testsuite>
</testsuites>
25 changes: 25 additions & 0 deletions tests/unit/src/__assets__/junit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<testsuites id="" name="" tests="1" failures="1" skipped="0" errors="0" time="5.535">
<testsuite name="tests/example.test.js" timestamp="1629933296785" hostname="" tests="1" failures="1" skipped="0" time="4.143" errors="0">
<testcase name="should verify title of the page" classname="tests/example.test.js:3:1 › [firefox] should verify title of the page" time="4.143">
<failure message="example.test.js:3:1 should verify title of the page" type="FAILURE">
tests/example.test.js:3:1 › [firefox] should verify title of the page ============================

Error: expect(received).toBe(expected) // Object.is equality

Expected: "Swag Labs111"
Received: "Swag Labs"

3 | test('should verify title of the page', async ({ page }) => {
4 | await page.goto('https://www.saucedemo.com/');
> 5 | expect(await page.title()).toBe('Swag Labs111');
| ^
6 | });
7 |

at /home/seluser/__project__/tests/example.test.js:5:32
at WorkerRunner._runTestWithBeforeHooks (/home/seluser/node_modules/@playwright/test/lib/test/workerRunner.js:290:13)

</failure>
</testcase>
</testsuite>
</testsuites>
12 changes: 11 additions & 1 deletion tests/unit/src/playwright-runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock('glob');
jest.mock('sauce-testrunner-utils');
jest.mock('../../../src/reporter');
const path = require('path');
const { run } = require('../../../src/playwright-runner');
const { run, generateJunitfile } = require('../../../src/playwright-runner');
const childProcess = require('child_process');
const { EventEmitter } = require('events');
const SauceLabs = require('saucelabs').default;
Expand Down Expand Up @@ -97,4 +97,14 @@ describe('playwright-runner', function () {
});
});
});
describe('.generateJunit', function () {
const junitPath = 'tests/unit/src/__assets__/junit.xml';
const backupContent = fs.readFileSync(junitPath, 'utf8');

generateJunitfile('tests/unit/src/', 'Firefox using global mode', 'firefox');

expect(fs.readFileSync(junitPath)).toEqual(fs.readFileSync('tests/unit/src/__assets__/expected_junit.xml'));

fs.writeFileSync(junitPath, backupContent);
});
});

0 comments on commit 38d76c6

Please sign in to comment.