nightwatchjs / nightwatch

Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
https://nightwatchjs.org
MIT License
11.79k stars 1.31k forks source link

Regression: globals_path not being used in 1.3.x #2373

Closed joshden closed 3 years ago

joshden commented 4 years ago

Describe the bug

We are finding with 1.3.0 and 1.3.4 that when we specifying a globals_path file in nightwatch.conf.js, values defined in the file are not accessible in the tests with browser.globals.

In the example below, we don't see myTest, testvar, or myGlobal when we console.log(browser.globals) in the test. However, in previous versions of Nightwatch, including 1.2.4 and 1.1.3, we do see these globals outputted.

Here's a complete Gist of the problem: https://gist.github.com/joshden/8c4d312f4b788f10b62f52132741822e

Sample test

test1.js

```js // Please add the sample test here module.exports = { 'First test': function (browser) { browser.url('https://google.com') console.log(browser.globals) } } ```

Run with command

$ npx nightwatch test1.js --verbose
globals.js

```js // Please add the sample test here module.exports = { default: { testvar: 1, myGlobal: function () { return "I'm a method" } }, myTest: 'foo' } ```

Verbose output

debug.log

```txt Starting GeckoDriver on port 4444... GeckoDriver up and running on port 4444 with pid: 30540 (181ms). [Test1] Test Suite ================== - Connecting to localhost on port 4444... Request POST /session { capabilities: { browserName: 'firefox', name: 'Test1' }, desiredCapabilities: { browserName: 'firefox', platform: 'ANY', name: 'Test1' } - Connecting to localhost on port 4444... Response 200 POST /session (5346ms) { value: { sessionId: '638d72df-62cd-4616-ab0c-94e7ff4a54ba', capabilities: { acceptInsecureCerts: false, browserName: 'firefox', browserVersion: '74.0', 'moz:accessibilityChecks': false, 'moz:buildID': '20200309095159', 'moz:geckodriverVersion': '0.26.0', 'moz:headless': false, 'moz:processID': 10580, 'moz:profile': 'C:\\Users\\Joshd\\AppData\\Local\\Temp\\rust_mozprofile19VmOT', 'moz:shutdownTimeout': 60000, 'moz:useNonSpecCompliantPointerOrigin': false, 'moz:webdriverClick': true, pageLoadStrategy: 'normal', platformName: 'windows', platformVersion: '10.0', rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: { implicit: 0, pageLoad: 300000, script: 30000 }, unhandledPromptBehavior: 'dismiss and notify' } } i Connected to localhost on port 4444 (5596ms). Using: firefox (74.0) on windows 10.0 platform. Received session with ID: 638d72df-62cd-4616-ab0c-94e7ff4a54ba → Running [before]: → Completed [before]. Running: First test → Running [beforeEach]: → Completed [beforeEach]. { abortOnAssertionFailure: true, waitForConditionPollInterval: 500, waitForConditionTimeout: 5000, throwOnMultipleElementsReturned: false, suppressWarningsOnMultipleElementsReturned: false, asyncHookTimeout: 10000, unitTestsTimeout: 2000, customReporterCallbackTimeout: 20000, retryAssertionTimeout: 5000, reporter: [Function: reporter] } → Running command: url ('https://google.com') Request POST /session/638d72df-62cd-4616-ab0c-94e7ff4a54ba/url { url: 'https://google.com' } Response 200 POST /session/638d72df-62cd-4616-ab0c-94e7ff4a54ba/url (3050ms) { value: null } → Completed command: url ('https://google.com') (3055ms) → Running [afterEach]: → Completed [afterEach]. No assertions ran. → Running [after]: → Completed [after]. → Running command: end ([Function]) → Running command: session ('delete', [Function]) Request DELETE /session/638d72df-62cd-4616-ab0c-94e7ff4a54ba Response 200 DELETE /session/638d72df-62cd-4616-ab0c-94e7ff4a54ba (1478ms) { value: null } → Completed command: end ([Function]) (1483ms) → Completed command: session ('delete', [Function]) (1482ms) Wrote report file to: tests_output\FIREFOX_74.0_10.0_test1.xml. Wrote log file to: C:\Users\Joshd\Tango\nightwatch_test\geckodriver.log. GeckoDriver process closed. ```

Configuration

nightwatch.conf.js

```js // Autogenerated by Nightwatch // Refer to the online docs for more details: https://nightwatchjs.org/gettingstarted/configuration/ const Services = {}; loadServices(); module.exports = { // An array of folders (excluding subfolders) where your tests are located; // if this is not specified, the test source must be passed as the second argument to the test runner. src_folders: [], // See https://nightwatchjs.org/guide/working-with-page-objects/ page_objects_path: '', // See https://nightwatchjs.org/guide/extending-nightwatch/#writing-custom-commands custom_commands_path: '', // See https://nightwatchjs.org/guide/extending-nightwatch/#writing-custom-assertions custom_assertions_path: '', // See https://nightwatchjs.org/guide/#external-globals globals_path : './globals.js', webdriver: {}, test_settings: { default: { disable_error_log: false, launch_url: 'https://nightwatchjs.org', screenshots: { enabled: false, path: 'screens', on_failure: true }, desiredCapabilities: { browserName : 'firefox' }, webdriver: { start_process: true, server_path: (Services.geckodriver ? Services.geckodriver.path : '') } }, firefox: { desiredCapabilities : { browserName : 'firefox', alwaysMatch: { // Enable this if you encounter unexpected SSL certificate errors in Firefox // acceptInsecureCerts: true, 'moz:firefoxOptions': { args: [ // '-headless', // '-verbose' ], } } }, webdriver: { start_process: true, port: 4444, server_path: (Services.geckodriver ? Services.geckodriver.path : ''), cli_args: [ // very verbose geckodriver logs // '-vv' ] } }, chrome: { desiredCapabilities : { browserName : 'chrome', chromeOptions : { // This tells Chromedriver to run using the legacy JSONWire protocol (not required in Chrome 78) // w3c: false, // More info on Chromedriver: https://sites.google.com/a/chromium.org/chromedriver/ args: [ //'--no-sandbox', //'--ignore-certificate-errors', //'--allow-insecure-localhost', //'--headless' ] } }, webdriver: { start_process: true, port: 9515, server_path: (Services.chromedriver ? Services.chromedriver.path : ''), cli_args: [ // --verbose ] } }, ////////////////////////////////////////////////////////////////////////////////// // Configuration for when using the browserstack.com cloud service | // | // Please set the username and access key by setting the environment variables: | // - BROWSERSTACK_USER | // - BROWSERSTACK_KEY | // .env files are supported | ////////////////////////////////////////////////////////////////////////////////// browserstack: { selenium: { host: 'hub-cloud.browserstack.com', port: 443 }, // More info on configuring capabilities can be found on: // https://www.browserstack.com/automate/capabilities?tag=selenium-4 desiredCapabilities: { 'bstack:options' : { local: 'false', userName: '${BROWSERSTACK_USER}', accessKey: '${BROWSERSTACK_KEY}', } }, disable_error_log: true, webdriver: { keep_alive: true, start_process: false } }, 'browserstack.chrome': { extends: 'browserstack', desiredCapabilities: { browserName: 'chrome', chromeOptions : { // This tells Chromedriver to run using the legacy JSONWire protocol // More info on Chromedriver: https://sites.google.com/a/chromium.org/chromedriver/ w3c: false } } }, 'browserstack.firefox': { extends: 'browserstack', desiredCapabilities: { browserName: 'firefox' } }, 'browserstack.ie': { extends: 'browserstack', desiredCapabilities: { browserName: 'IE', browserVersion: '11.0', 'bstack:options' : { os: 'Windows', osVersion: '10', local: 'false', seleniumVersion: '3.5.2', resolution: '1366x768' } } }, ////////////////////////////////////////////////////////////////////////////////// // Configuration for when using the Selenium service, either locally or remote, | // like Selenium Grid | ////////////////////////////////////////////////////////////////////////////////// selenium: { // Selenium Server is running locally and is managed by Nightwatch selenium: { start_process: true, port: 4444, server_path: (Services.seleniumServer ? Services.seleniumServer.path : ''), cli_args: { 'webdriver.gecko.driver': (Services.geckodriver ? Services.geckodriver.path : ''), 'webdriver.chrome.driver': (Services.chromedriver ? Services.chromedriver.path : '') } } }, 'selenium.chrome': { extends: 'selenium', desiredCapabilities: { browserName: 'chrome', chromeOptions : { w3c: false } } }, 'selenium.firefox': { extends: 'selenium', desiredCapabilities: { browserName: 'firefox', 'moz:firefoxOptions': { args: [ // '-headless', // '-verbose' ] } } } } }; function loadServices() { try { Services.seleniumServer = require('selenium-server'); } catch (err) {} try { Services.chromedriver = require('chromedriver'); } catch (err) {} try { Services.geckodriver = require('geckodriver'); } catch (err) {} } ```

Your Environment

Executable Version
nightwatch --version v1.3.4
npm --version 6.14.3
node --version v12.16.1
Browser driver Version
geckodriver v0.26.0
OS Version
Windows 10 1909
markgmilner commented 4 years ago

I was running into the same issue. I was able to resolve it by naming my external globals file something other than globals.js. To me, it seems like if the file is name globals.js then the module resolution of nightwatch ends up loading the nightwatch globals module rather than your own.

This at least this provides a workaround.

Edit: It appears nightwatch has problems if a globals file does not already exist. So it seems to work if your globals path points to a file not named globals, and you also have a dummy placeholder file named globals.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had any recent activity. If possible, please retry using the latest Nightwatch version and update the issue with any relevant details. If no further activity occurs, it will be closed. Thank you for your contribution.

joshden commented 4 years ago

We were also able to work around this issue by renaming the globals.js file to something else. Thank you for your help, Mark.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had any recent activity. If possible, please retry using the latest Nightwatch version and update the issue with any relevant details. If no further activity occurs, it will be closed. Thank you for your contribution.