Nightwatch tests do not find elements in Headless chrome mode - selenium

I am trying to get my tests (written in Nightwatch.js) to run locally in headless chrome.
However, the tests fail since they are not able to find the elements in headless mode (they work without headless mode though).
If I check the failure screenshots I only get a white screen.
But if test checks for the "body" element, it actually pass. So I think the page is loaded, but maybe headless chrome, for some reason, cannot load the javascript? Later I wait for divs and buttons etc to be visible for several seconds, but it does not find them.
Do you have any ideas what could be wrong?
I have added the --headless and --disable-gpu flags in desiredCapabilities in the nightwatch config file.

So, as I said in my reply I had the same issue yesterday, I was just trying to do a dummy test on google homepage. This morning with a fresh brain I tried to tackle that. I had the brilliant idea to take a screenshot just before nightwatch fails to find an element.
It turns out the "normal" chrome had the home page in English, and the "headless" chrome had the homepage in french, for some reason.
I found this: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1925 may be explaining why.
The workaround I found to always have the correct language is:
"chromeOptions" : {
"args" : ["--lang=fr", "--headless"]
}
I still have a hard time setting it in english (weirdly) but I hope this will save someone a couple of hours in the future

I think you should declare binary path in Nightwatch.js
If you are on linux please try this, it works perfectly for me :
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true,
"chromeOptions": {
"args": [
"headless", "disable-gpu"
],
"binary": "/usr/bin/google-chrome"
}
}
If you are on Mac, replace your binary path, eg /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome

Related

Running Chrome from PortableApps with Selenium WebDriver fails

I've got C# Selenium project and want to run portable Chrome downloaded from here.
My chromeOptions are:
chromeOptions.BinaryLocation = Path.GetFullPath("C:\", "tests.browsers", "windows", "chrome", "GoogleChromePortable.exe"));
chromeOptions.AddArgument("--enable-logging");
chromeOptions.AddArgument("--v=1");
chromeOptions.AddExtension(Path.Combine("path", "to", "kontur-plugin.crx"));
But when I run the test, Chrome opens and shows this:
(notifications say "Failed to login %EXTENSIONNAME%, click to load it once again)
chrome_debug.log contains no errors.
What am I doing wrong? How can I figure out what is the problem with?
this might help you - Stackoverflow post
also, your CRX file should add as a file, not just the path.

Test VueJs localization using Nightwatch

I have an application that I started in English from scratch. I had nightwatch tests and everything is working perfectly fine... but after adding 2 more languages, I want the main tests to run as they used to in English, then change the browser language (since that's the criteria on which I choose the language) so I can run the other tests in German or French... etc. Is there a way to start a test suite by changing the browser's language?
I looked into the documentation and found nothing in this area
In order to set the language and run the tests locally, I did the following:
chrome: {
desiredCapabilities: {
chromeOptions: {
prefs: {
intl: { accept_languages: "ss-ZA" }
}, args: []
}
}
}
this allows you to run the tests locally, on only that language. or you can manage setting that locale code to a variable in a way and try setting it at the beginning of your test suite.
I did not get that far, because in my case, I have to run my tests headless because they will run later on a remote git server that runs only headless tests. According to this issue on Nightwatch's github page, setting a browser language parameter for a headless test is not possible!

Webdriver.io: what's the difference between the capabilities and desiredCapabilities keywords?

I'm using Webdriver.io to run UI tests in a Node environment.
I'd like to run Headless Chrome and came up with the following working configuration:
{
capabilities: [{
browserName: 'chrome',
args: ['--headless', '--disable-gpu']
}
}],
services : ['selenium-standalone'],
execArgv : ['--inspect']
}
However, I can't figure out whether capabilities is deprecated or not; I'm wondering because all the examples I see reference desiredCapabilities instead.
If I use desiredCapabilities though, then Chrome runs normally, not in headless mode.
I feel like I'm missing something, but I don't know what. Is there a significant difference between the two, and is one going away?
Thanks!
If you run webdriverio through the test runner (using a wdio.conf.js file), it uses capabilities.
If you run it in standalone mode (e.g. node myTest.js), it uses desiredCapabilities

Disable file downloads Selenium Firefox

I'm building a webscraping app and I'm using Selenium with a Firefox driver to open my pages. Whenever it opens a link that leads to a download, my app stalls and hangs on the link for eternity.
I've tried looking for a solution, but they never mention disabling downloads. They only talk about enabling them, changing the download directory,...
Would it be possible to detect that the link is a download link and just skip it, or maybe to skip the link whenever it opens?
I had a slightly different problem and I solved it by changing Firefox capabilities.
Open about:config in Firefox to see list of them. Then you can pass them to webdriver through desired_capabilities.
caps = {'acceptInsecureCerts': True,
'browserName': 'firefox',
'marionette': True,
'moz:firefoxOptions': {
'args': ['--no-remote'],
'prefs': {
'browser.safebrowsing.downloads.enabled': False
}
},
driver = webdriver.Remote([...]
desired_capabilities=caps)

Ghostdriver with PHPUnit

I'm attempting to use PhantomJS as a browser for PHPUnit Selenium tests.
I've set Selenium running in grid mode, and started phantomjs with webdriver, and registered it to the grid, as in the GhostDriver Readme.
When I run a selenium test, it fails with an unknown command error - GhostDriver just doesn't understand what PHPUnit is saying.
[ERROR - 2013-05-12T16:23:06.326Z] RouterReqHand - _handle - Thrown => {
"message": "Request => {\"headers\":{\"Accept\":\"*/*\",\"Connection\":\"Keep-Alive\",\"Content-Length\":\"85\",\"Content-Type\":\"application/x-www-form-urlencoded; charset=utf-8\",\"Host\":\"127.0.0.1:4444\"},\"httpVersion\":\"1.1\",\"method\":\"POST\",\"post\":\"cmd=getNewBrowserSession&1=phantomjs&2=https%3A%2F%2Ftest.testurl.com%2F&\",\"url\":\"/\",\"urlParsed\":{\"anchor\":\"\",\"query\":\"\",\"file\":\"\",\"directory\":\"/\",\"path\":\"/\",\"relative\":\"/\",\"port\":\"\",\"host\":\"\",\"password\":\"\",\"user\":\"\",\"userInfo\":\"\",\"authority\":\"\",\"protocol\":\"\",\"source\":\"/\",\"queryKey\":{},\"chunks\":[\"\"]}}",
"name": "Unknown Command",
"line": 87,
"sourceId": 139810136032448,
"sourceURL": ":/ghostdriver/request_handlers/router_request_handler.js",
"stack": "Unknown Command: Request => {\"headers\":{\"Accept\":\"*/*\",\"Connection\":\"Keep-Alive\",\"Content-Length\":\"85\",\"Content-Type\":\"application/x-www-form-urlencoded; charset=utf-8\",\"Host\":\"127.0.0.1:4444\"},\"httpVersion\":\"1.1\",\"method\":\"POST\",\"post\":\"cmd=getNewBrowserSession&1=phantomjs&2=https%3A%2F%2FFtest.testurl.com%2F&\",\"url\":\"/\",\"urlParsed\":{\"anchor\":\"\",\"query\":\"\",\"file\":\"\",\"directory\":\"/\",\"path\":\"/\",\"relative\":\"/\",\"port\":\"\",\"host\":\"\",\"password\":\"\",\"user\":\"\",\"userInfo\":\"\",\"authority\":\"\",\"protocol\":\"\",\"source\":\"/\",\"queryKey\":{},\"chunks\":[\"\"]}}\n at :/ghostdriver/request_handlers/router_request_handler.js:87",
"stackArray": [
{
"sourceURL": ":/ghostdriver/request_handlers/router_request_handler.js",
"line": 87
}
]
}
This same question was asked and closed unanswered on the GhostDriver site with the suggestion that it's PHPUnit to blame. That may be the case, but I'm still no nearer to making this work. Does anyone have any idea how to fix it?
It looks like you are using a test class extending PHPUnit_Extensions_SeleniumTestCase. Use PHPUnit_Extensions_Selenium2TestCase instead.
Unfortunately, that's not the end of the story. The syntax of the Selenium-related methods changes when you swap out the base class.
The dated PHPUnit_Extensions_SeleniumTestCase class
uses the Selenium RC API
does not support Phantom.js as a browser
requires the "traditional" syntax as documented here
works fine with code which is generated in Selenium IDE and exported with the help of the "Selenium IDE: PHP Formatters" plugin.
By contrast, PHPUnit_Extensions_Selenium2TestCase
uses the WebDriver API
supports Phantom.js
requires a different set of commands which is not well documented - this test case demonstrates it by example, and that's about it
does not work with code exported from Selenium IDE without an extensive rewrite.
So it is possible to run PHPUnit-driven Selenium tests faster with PhantomJS, but it does come at a cost.