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.8k stars 1.31k forks source link

waitForElementVisible fails with Safari in master, 1.0.16, works in 1.0.14 #1945

Closed phsultan closed 5 years ago

phsultan commented 5 years ago

Hi,

I'm running a simple test (against standalone selenium server at version 3.14.0) that fails at waitForElementVisible():

$ cat sites/www.google.com.js 
module.exports = {
  'Google\'s Search Functionality' : function (browser) {
    browser
      .url('https://www.google.com/ncr')
      .waitForElementVisible('body', 5000)
      .setValue('input[type=text]', 'BrowserStack\n')
      .pause(10000)
      .assert.title('BrowserStack - Google Search')
      .end();
  }
};

Command:

$ ./node_modules/nightwatch/bin/nightwatch --test sites/www.google.com.js --env safari --verbose
...
 → Running command: waitForElementVisible ('body', 5000)
   Request POST  /wd/hub/session/BC6424EF-4491-4D55-9A38-FCC88B14A3AF/elements  
   { using: 'css selector', value: 'body' }
   Response 200 POST /wd/hub/session/BC6424EF-4491-4D55-9A38-FCC88B14A3AF/elements (25ms)
   { state: 'success',
     sessionId: null,
     value: 
      [ { 'element-6066-11e4-a52e-4f735466cecf': 'node-BA416B98-C3B3-4156-B768-0D4E98B40B84' } ],
     status: 0 }
 Error while running .isElementDisplayed() protocol action: TypeError: Error while trying to create HTTP request for "/wd/hub/session/BC6424EF-4491-4D55-9A38-FCC88B14A3AF/element/[object Object]/displayed": Request path contains unescaped characters
    at new ClientRequest (_http_client.js:127:13)
    at Object.request (http.js:38:10)
    at HttpRequest.createHttpRequest (/tmp/git_repositories/nightwatch.git/lib/http/request.js:112:55)
    at HttpRequest.send (/tmp/git_repositories/nightwatch.git/lib/http/request.js:191:29)
    at Promise (/tmp/git_repositories/nightwatch.git/lib/transport/transport.js:208:15)
    at new Promise (<anonymous>)
    at JsonWireProtocol.sendProtocolAction (/tmp/git_repositories/nightwatch.git/lib/transport/transport.js:206:12)
    at JsonWireProtocol.runProtocolAction (/tmp/git_repositories/nightwatch.git/lib/transport/jsonwire.js:55:17)
    at Object.target.(anonymous function).definition [as isElementDisplayed] (/tmp/git_repositories/nightwatch.git/lib/transport/actions.js:46:33)
    at JsonWireProtocol.executeProtocolAction (/tmp/git_repositories/nightwatch.git/lib/transport/transport.js:451:48)
...

Checking out tags/v1.0.14 and running the same command results in success:

$ ./node_modules/nightwatch/bin/nightwatch --test sites/www.google.com.js --env safari --verbose
...
 → Running command: waitForElementVisible ('body', 5000)
   Request POST  /wd/hub/session/7B7C099A-739C-4A90-9555-954B7F2DC56E/elements  
   { using: 'css selector', value: 'body' }
   Response 200 POST /wd/hub/session/7B7C099A-739C-4A90-9555-954B7F2DC56E/elements (35ms)
   { state: 'success',
     sessionId: null,
     value: 
      [ { 'element-6066-11e4-a52e-4f735466cecf': 'node-2857AF86-E73A-46AC-A8BB-C2ED502667DE' } ],
     status: 0 }
   Request GET  /wd/hub/session/7B7C099A-739C-4A90-9555-954B7F2DC56E/element/node-2857AF86-E73A-46AC-A8BB-C2ED502667DE/displayed  
   Response 200 GET /wd/hub/session/7B7C099A-739C-4A90-9555-954B7F2DC56E/element/node-2857AF86-E73A-46AC-A8BB-C2ED502667DE/displayed (114ms)
   { state: 'success', sessionId: null, value: true, status: 0 }
...

Configuration:

$ cat nightwatch.json 
{
  "src_folders": [
    "sites"
  ],
  "output_folder": "reports",
  "selenium": {
    "start_process": false
  },
  "webdriver": {
    "default_path_prefix": "/wd/hub"
  },
  "test_settings": {
    "safari": {
      "desiredCapabilities": {
        "browserName": "safari",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    },
    "chrome": {
      "desiredCapabilities": {
        "browserName": "chrome",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "nativeEvents": true,
        "chromeOptions": {
          "args": [
            "headless",
            "no-sandbox",
            "allow-file-access-from-files",
            "use-fake-device-for-media-stream",
            "use-fake-ui-for-media-stream",
            "disable-translate",
            "no-process-singleton-dialog",
            "mute-audio"
          ]
        }
      }
    },
    "firefox": {
      "desiredCapabilities": {
        "browserName": "firefox",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "nativeEvents": true,
        "moz:firefoxOptions": {
          "log": {
            "level": "trace"
          },
          "args": [
            "-headless",
            "-no-remote"
          ],
          "prefs": {
            "browser.cache.disk.enable": false,
            "browser.cache.disk.capacity": 0,
            "browser.cache.disk.smart_size.enabled": false,
            "browser.cache.disk.smart_size.first_run": false,
            "browser.sessionstore.resume_from_crash": false,
            "browser.startup.page": 0,
            "media.navigator.streams.fake": true,
            "media.navigator.permission.disabled": true,
            "device.storage.enabled": false,
            "media.gstreamer.enabled": false,
            "browser.startup.homepage": "about:blank",
            "browser.startup.firstrunSkipsHomepage": false,
            "extensions.update.enabled": false,
            "app.update.enabled": false,
            "network.http.use-cache": false,
            "browser.shell.checkDefaultBrowser": false
          }
        }
      }
    }
  }
}

Let me know if you need more information!

beatfactor commented 5 years ago

Where is Safari running?

phsultan commented 5 years ago

A Safari instance is popped on the same computer I'm running the test command.

beatfactor commented 5 years ago

then you can remove "default_path_prefix": "/wd/hub" and also the selenium block just for testing in Safari.

phsultan commented 5 years ago

The thing is that if I remove "default_path_prefix": "/wd/hub", then I hit https://github.com/nightwatchjs/nightwatch/issues/1943 (which applies both locally in my case and for BrowserStack).

beatfactor commented 5 years ago

So maybe you can post your config on #1943 so I can reproduce it? From your config posted here, there is no information about the selenium server.

In your safari settings you can overwrite default_path_prefix:

"safari": {
  "webdriver": {
      "default_path_prefix" : ""
   },
    "desiredCapabilities": {
        "browserName": "safari",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
},
phsultan commented 5 years ago

Sure, Selenium is started with:

$ java -Dwebdriver.chrome.driver='./node_modules/chromedriver/lib/chromedriver/chromedriver' -Dwebdriver.gecko.driver='./node_modules/geckodriver/geckodriver' -jar ./node_modulesium-server/lib/runner/selenium-server-standalone-3.14.0.jar -role standalone --debug

I had indeed post my environment details there (config, test command, selenium server command) : https://github.com/nightwatchjs/nightwatch/issues/1943#issuecomment-447849951

The difference is that I added the "default_path_prefix": "/wd/hub" to work around https://github.com/nightwatchjs/nightwatch/issues/1943.

Thanks a lot for your help so far!

beatfactor commented 5 years ago

@phsultan I'm also wondering why you're starting Selenium separately and not through Nightwatch, since it is on the same machine?

phsultan commented 5 years ago

That's because for some reason, when I started using Nightwatch + Selenium few months ago, I would randomly encounter this issue:

$ ./node_modules/nightwatch/bin/nightwatch --verbose
 Starting Selenium Server on port 4444...
   connect ECONNREFUSED 127.0.0.1:4444

   socket hang up
       at createHangUpError (_http_client.js:331:15)
       at Socket.socketCloseListener (_http_client.js:363:23)

And having Selenium running in a separate process had fixed this totally.

beatfactor commented 5 years ago

Hmm, ok, was that in v1.0?

phsultan commented 5 years ago

Most probably, yes. Launching Selenium from Nightwatch fixes this issue:

$ ./node_modules/nightwatch/bin/nightwatch --test sites/www.google.com.js --env safari --verbose
 Starting Selenium Server on port 4444...
 Selenium Server up and running on port 4444 with pid: 9739 (1025ms).

[Sites/Www Google Com] Test Suite
=================================
   Request POST  /wd/hub/session  
   { desiredCapabilities: 
      { browserName: 'safari',
        acceptSslCerts: true,
        platform: 'ANY',
        javascriptEnabled: true,
        name: 'Sites/Www Google Com' } }
   Response 200 POST /wd/hub/session (2509ms)
   { status: 0,
     sessionId: '0DCBA705-0D28-4868-BA46-DD3C24E3E2FB',
     value: 
      { browserVersion: '12.0.1',
        platformName: 'macOS',
        acceptInsecureCerts: false,
        setWindowRect: true,
        browserName: 'Safari',
        'webdriver.remote.sessionid': '0DCBA705-0D28-4868-BA46-DD3C24E3E2FB' } }
 Received session with ID: 0DCBA705-0D28-4868-BA46-DD3C24E3E2FB

 → Running [before]:
 → Completed [before].
Running:  Google's Search Functionality

 → Running [beforeEach]:
 → Completed [beforeEach].

 → Running command: url ('https://www.google.com/ncr')
   Request POST  /wd/hub/session/0DCBA705-0D28-4868-BA46-DD3C24E3E2FB/url  
   { url: 'https://www.google.com/ncr' }
   Response 200 POST /wd/hub/session/0DCBA705-0D28-4868-BA46-DD3C24E3E2FB/url (1812ms)
   { state: 'success', sessionId: null, value: null, status: 0 }
 → Completed command url ('https://www.google.com/ncr') (1814ms)

 → Running command: waitForElementVisible ('body', 5000)
   Request POST  /wd/hub/session/0DCBA705-0D28-4868-BA46-DD3C24E3E2FB/elements  
   { using: 'css selector', value: 'body' }
   Response 200 POST /wd/hub/session/0DCBA705-0D28-4868-BA46-DD3C24E3E2FB/elements (15ms)
   { state: 'success',
     sessionId: null,
     value: 
      [ { 'element-6066-11e4-a52e-4f735466cecf': 'node-3596395C-AC34-4E7E-A8B7-EEAF8169D2A9' } ],
     status: 0 }
   Request GET  /wd/hub/session/0DCBA705-0D28-4868-BA46-DD3C24E3E2FB/element/node-3596395C-AC34-4E7E-A8B7-EEAF8169D2A9/displayed  
   Response 200 GET /wd/hub/session/0DCBA705-0D28-4868-BA46-DD3C24E3E2FB/element/node-3596395C-AC34-4E7E-A8B7-EEAF8169D2A9/displayed (50ms)
   { state: 'success', sessionId: null, value: true, status: 0 }
✔ Element <body> was visible after 66 milliseconds.
 → Completed command waitForElementVisible ('body', 5000) (68ms)

Configuration is now:

{
  "src_folders": [
    "sites"
  ],
  "output_folder": "reports",
  "selenium" : {
    "start_process": true,
    "host": "localhost",
    "server_path": "./node_modules/selenium-server/lib/runner/selenium-server-standalone-3.14.0.jar",
    "cli_args": {
      "webdriver.gecko.driver": "./node_modules/geckodriver/geckodriver",
      "webdriver.chrome.driver": "./node_modules/chromedriver/lib/chromedriver/chromedriver"
    }
  },
  "test_settings": {
    "safari": {
      "desiredCapabilities": {
        "browserName": "safari",
        "javascriptEnabled": true,
        "acceptSslCerts": true
      }
    },
    "chrome": {
      "desiredCapabilities": {
        "browserName": "chrome",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "nativeEvents": true,
        "chromeOptions": {
          "args": [
            "headless",
            "no-sandbox",
            "allow-file-access-from-files",
            "use-fake-device-for-media-stream",
            "use-fake-ui-for-media-stream",
            "disable-translate",
            "no-process-singleton-dialog",
            "mute-audio"
          ]
        }
      }
    },
    "firefox": {
      "desiredCapabilities": {
        "browserName": "firefox",
        "javascriptEnabled": true,
        "acceptSslCerts": true,
        "nativeEvents": true,
        "moz:firefoxOptions": {
          "log": {
            "level": "trace"
          },
          "args": [
            "-headless",
            "-no-remote"
          ],
          "prefs": {
            "browser.cache.disk.enable": false,
            "browser.cache.disk.capacity": 0,
            "browser.cache.disk.smart_size.enabled": false,
            "browser.cache.disk.smart_size.first_run": false,
            "browser.sessionstore.resume_from_crash": false,
            "browser.startup.page": 0,
            "media.navigator.streams.fake": true,
            "media.navigator.permission.disabled": true,
            "device.storage.enabled": false,
            "media.gstreamer.enabled": false,
            "browser.startup.homepage": "about:blank",
            "browser.startup.firstrunSkipsHomepage": false,
            "extensions.update.enabled": false,
            "app.update.enabled": false,
            "network.http.use-cache": false,
            "browser.shell.checkDefaultBrowser": false
          }
        }
      }
    }
  }
}

Thanks a lot for your valuable help!

beatfactor commented 5 years ago

No problem, just so you know you can use ChromeDriver directly, without Selenium. I'm not entirely sure about GeckoDriver compatibility, but it is also possible to be used directly.

phsultan commented 5 years ago

Yes, this discussion made me realize this too. Thank you.