forked from wet-boew/wet-boew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-runner.js
137 lines (112 loc) · 3.73 KB
/
test-runner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Web Experience Toolkit (WET) / Boîte à outils de l'expérience Web (BOEW)
* @title Test runner
* @overview Test runner through puppeteer and chromium headless with mocha
* @license wet-boew.github.io/wet-boew/License-en.html / wet-boew.github.io/wet-boew/Licence-fr.html
* @author @duboisp
*/
console.log( "Running unit testing" );
// Test runner setting
const baseFolder = "dist";
const webServerPort = 8000;
const webPageUrlToTest = "http://localhost:8000/unmin/test/test.html?txthl=just%20some%7Ctest";
// Import/required
const puppeteer = require( "puppeteer" );
const connect = require('connect');
const path = require('path');
const connectServer = connect();
const serveIndex = require('serve-index');
const serveStatic = require('serve-static');
const basePath = path.resolve( baseFolder );
// Start the web server
connectServer.use( serveStatic( basePath ) );
connectServer.use( serveIndex( basePath ) );
webServer = connectServer.listen( webServerPort );
console.log( "Web server started" );
// Sleep utility function
function sleep( ms ) {
ms = ms || 1000;
return new Promise( resolve => setTimeout( resolve, ms) );
}
// Run the test into the headless chromiumn browser
puppeteer.launch( {
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox'
],
slowMo: 50
} ).then( async browser => {
// Wait a little to help concurent test
await sleep( 1000 );
console.log( "Puppeteer chrominium started, navigating to:\n" + webPageUrlToTest );
const page = await browser.newPage();
page.on( 'console', msg => {
console.log( "LOG" );
console.log( msg );
} );
page.on( 'pageerror', msg => {
console.log( "ERR" );
console.log( msg );
} );
await page.goto( webPageUrlToTest, { waitUntil: "domcontentloaded" } );
// Wait for the result
console.log( "Waiting for test completion" );
const limit = 60;
let counter = 0,
mocha;
while ( limit !== counter ) {
await counter++;
mocha = await page.evaluate( () => {
// Get the test result from the global variable and use the mocha div container to test if the test are running or not
return window.mochaResults || { body: document.getElementById( "mocha" ).innerHTML.trim() };
} );
if ( !mocha || !mocha.reports ) {
console.log( "wait..." + counter );
if ( mocha.body ) {
console.log( "Test ongoing" );
} else {
console.log( "Nothing happening currently" );
// Reload the page if nothing happen
if ( !( counter % 3 ) ) {
console.log( "Reload the page ... " + counter );
await page.reload( { waitUntil: "domcontentloaded", timeout: 60000 } );
}
}
await sleep( 1000 );
console.log( "++++++++++++++++++++++++++++++++++++\n" );
} else {
break;
}
}
if ( !mocha || !mocha.reports ) {
console.log( "Error - Timeout or unable to retrieve the mocha test results" );
console.log( mocha );
process.exit( 1 );
}
// Close the browser and webserver
await browser.close();
await webServer.close( () => { console.log( "Web server terminated" ); } );
// Print the stats
console.log( "\n=== Test results ===\n" );
console.log( "Test suites: " + mocha.suites );
console.log( "Number of tests: " + mocha.tests );
console.log( "Passes: " + mocha.passes );
console.log( "Failures: " + mocha.failures );
console.log( "Duration: " + mocha.duration + " ms" );
console.log( "===\n" );
// Print test error details if any
mocha.reports.forEach( function( details ) {
console.log( details.titles[ 0 ] );
console.log( " -> " + details.name );
console.log( " -> Error message: " + details.message + "\n" );
} );
// Test result summary
if ( !mocha.reports.length ) {
console.log( "Test PASS\n" );
} else {
console.log( "\n*******\nTest FAIL !!!!!\n*******\n" );
}
// Exit
process.exit( mocha.reports.length );
} );