1- const fs = require ( "fs " ) ;
1+ const Nightwatch = require ( "nightwatch " ) ;
22const {
3- setDefaultTimeout,
43 After,
54 AfterAll,
6- BeforeAll,
75 Before,
6+ setDefaultTimeout,
87} = require ( "@cucumber/cucumber" ) ;
9- const {
10- createSession,
11- closeSession,
12- startWebDriver,
13- stopWebDriver,
14- getNewScreenshots,
15- saveScreenshot,
16- } = require ( "nightwatch-api" ) ;
8+ const fs = require ( "fs" ) ;
9+ const fsPromises = fs . promises ;
10+ const path = require ( "path" ) ;
11+ const os = require ( "os" ) ;
1712const reporter = require ( "cucumber-html-reporter" ) ;
18- const { client } = require ( "nightwatch-api" ) ;
19- const run = {
20- browserName : "" ,
21- platform : "" ,
22- version : "" ,
23- BreedingInsight : "" ,
24- } ;
25-
26- setDefaultTimeout ( 600000 ) ;
27- global . __basedir = __dirname ;
28-
29- Before ( async function ( ) {
30- await createSession ( { env : this . parameters . browser } ) ;
31- await client . resizeWindow ( 1900 , 1200 ) ;
32- this . parameters . timeStamp = Date . now ( ) ;
33- if ( process . env . npm_config_url != undefined ) {
34- this . parameters . launch_url = process . env . npm_config_url ;
13+
14+ require ( "events" ) . EventEmitter . defaultMaxListeners = 20 ;
15+ setDefaultTimeout ( 300000 ) ;
16+
17+ Before ( async function ( { pickle } ) {
18+ fs . mkdirSync ( "report" , { recursive : true } ) ;
19+ fs . mkdirSync ( "screenshots" , { recursive : true } ) ;
20+
21+ this . tmpUserDataDir = fs . mkdtempSync (
22+ path . join ( os . tmpdir ( ) , "nw-chrome-profile-" )
23+ ) ;
24+ console . log ( "tmpUserDataDir:" , this . tmpUserDataDir ) ;
25+
26+ const chromeArgs = [
27+ "--no-sandbox" ,
28+ "--disable-dev-shm-usage" ,
29+ "--disable-extensions" ,
30+ "--disable-gpu" ,
31+ "--disable-background-networking" ,
32+ "--disable-sync" ,
33+ "--metrics-recording-only" ,
34+ "--disable-default-apps" ,
35+ "--mute-audio" ,
36+ "--no-first-run" ,
37+ "--ignore-certificate-errors" ,
38+ "--allow-insecure-localhost" ,
39+ "--window-size=1920,1080" ,
40+ "--headless=new" ,
41+ ] ;
42+
43+ const webdriver = { } ;
44+ if ( this . parameters [ "webdriver-host" ] )
45+ webdriver . host = this . parameters [ "webdriver-host" ] ;
46+ if ( this . parameters [ "webdriver-port" ] )
47+ webdriver . port = this . parameters [ "webdriver-port" ] ;
48+ if ( typeof this . parameters [ "start-process" ] !== "undefined" )
49+ webdriver . start_process = this . parameters [ "start-process" ] ;
50+
51+ const globals = { } ;
52+ if ( this . parameters [ "retry-interval" ] ) {
53+ globals . waitForConditionPollInterval = this . parameters [ "retry-interval" ] ;
3554 }
36- } ) ;
3755
38- BeforeAll ( async ( ) => {
39- await startWebDriver ( ) ;
40- } ) ;
56+ this . client = Nightwatch . createClient ( {
57+ headless : this . parameters . headless ,
58+ env : this . parameters . env ,
59+ timeout : this . parameters . timeout ,
60+ parallel : ! ! this . parameters . parallel ,
61+ output : ! this . parameters [ "disable-output" ] ,
62+ enable_global_apis : true ,
63+ silent : ! this . parameters . verbose ,
64+ always_async_commands : true ,
65+ webdriver,
66+ persist_globals : this . parameters [ "persist-globals" ] ,
67+ config : this . parameters . config ,
68+ globals : {
69+ run : { } ,
70+ } ,
71+ desiredCapabilities : {
72+ browserName : "chrome" ,
73+ "goog:chromeOptions" : {
74+ args : chromeArgs ,
75+ } ,
76+ } ,
77+ } ) ;
4178
42- AfterAll ( async ( ) => {
43- run . browserName = client . capabilities . browserName ;
44- switch ( client . capabilities . browserName ) {
45- case "msedge" : //same as chrome
46- case "chrome-headless-shell" :
47- case "chrome" :
48- run . version = client . capabilities . version ;
49- run . platform = client . capabilities . platform ;
50- break ;
51- case "firefox" :
52- run . version = client . capabilities . browserVersion ;
53- run . platform = client . capabilities . platformName ;
54- break ;
55- default :
56- throw new Error ( "Unrecognized browser." ) ;
79+ if ( this . client . settings . sync_test_names ) {
80+ this . client . updateCapabilities ( { name : pickle . name } ) ;
5781 }
58- await stopWebDriver ( ) ;
5982
60- // convert JSON object to string
61- const data = JSON . stringify ( run ) ;
83+ console . log ( "Launching Chrome with args: " , chromeArgs ) ;
84+ console . log ( "Executing test : " + pickle . name ) ;
6285
63- // write JSON string to a file
64- fs . writeFile ( "run.json" , data , ( err ) => {
65- if ( err ) {
66- throw err ;
67- }
68- console . log ( "JSON data is saved." ) ;
69- } ) ;
86+ this . browser = await this . client . launchBrowser ( ) ;
87+ this . browser . globals . timestamp = Date . now ( ) ;
7088} ) ;
7189
72- After ( function ( ) {
73- if ( run . BreedingInsight == "" ) {
74- run . BreedingInsight = client . globals . breedingInsightVersion ;
90+ After ( async function ( testCase ) {
91+ if ( testCase . result . status === "FAILED" && this . browser ) {
92+ const filename = `screenshots/${ testCase . pickle . name } -${ Date . now ( ) } .png` ;
93+ await this . browser . saveScreenshot ( filename ) ;
94+ this . attach ( fs . readFileSync ( filename ) , "image/png" ) ;
7595 }
76-
77- getNewScreenshots ( ) . forEach ( ( file ) =>
78- this . attach ( fs . readFileSync ( file ) , "image/png" )
79- ) ;
80- //Note: The following line can be commented out to keep browsers open for debugging purposes on local
81- closeSession ( ) ;
82- } ) ;
96+
97+ if ( this . browser ) {
98+ await this . browser . quit ( ) ;
99+ }
100+
101+ if ( this . tmpUserDataDir ) {
102+ fs . rmSync ( this . tmpUserDataDir , { recursive : true , force : true } ) ;
103+ }
104+
105+ if ( ! this . browser ?. globals ?. run ?. browserName ) {
106+ const caps = this . browser . capabilities ;
107+ const globalsRun = this . browser . globals . run ;
108+
109+ globalsRun . browserName = caps . browserName ;
110+ globalsRun . version = caps . browserVersion ;
111+ globalsRun . platform = caps . platformName ;
112+
113+ try {
114+ await fsPromises . writeFile ( "report/run.json" , JSON . stringify ( globalsRun ) ) ;
115+ console . log ( "Saved run metadata." ) ;
116+ } catch ( err ) {
117+ console . error ( "Error saving run metadata:" , err ) ;
118+ }
119+ }
120+ } ) ;
0 commit comments