How do I keep the browser open on a failed test using Selenium+Nightwatch+Chromedriver? - selenium

I'm using Nightwatch, Selenium, and Chrome Driver to conduct automated UI testing. Sometimes there are test failures on any number of remote environments. To debug the failures locally, I would like to keep the browser open on test failure.
I used to be able to do this using config settings in nightwatch.json and nightwatch.conf.js, but I lost my config settings cheat-sheet in a hard drive failure, and despite my best efforts on google I can't seem to find the right combination again.
If I remember correctly, it wasn't any kind of keepBrowserOpen: true type flag. If memory serves, it was some type of timeout set to a silly high number combined with something else like detachDriver. I've checked the Selenium documentation with specific focus on the Chrome Driver Options, and the list of Chrome Driver options, but I haven't found any working combinations.
What am I missing?
Here is my nightwatch.json
{
"output_folder": "tests/automation/reports",
"end_session_on_fail": false,
"test_settings": {
"default": {
"selenium_port": 4444,
"screenshots": {
"enabled": true,
"path": "tests/automation/reports/screenshots"
},
"javascriptEnabled": true,
"acceptSslCerts": true,
"acceptInsecureCerts": true,
"globals": {
"waitForConditionTimeout": 10000
}
},
"chrome": {
"extends": "default",
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true,
"acceptInsecureCerts": true,
"chromeOptions": {
"args": [
"--no-sandbox",
"--disable-gpu"
]
}
}
}
}
}
And my nightwatch.conf.js
const geckoDriver = require('geckodriver');
const chromeDriver = require('chromedriver');
const seleniumServer = require('selenium-server');
module.exports = (function (settings) {
settings.selenium = {
start_process: true,
server_path: seleniumServer.path,
log_path: 'tests/automation/reports/',
host: 'localhost',
port: 4444,
cli_args: {
'webdriver.chrome.driver': chromeDriver.path,
'webdriver.gecko.driver': geckoDriver.path,
'webdriver.edge.driver': 'C:\\windows\\system32\\MicrosoftWebDriver.exe',
'webdriver.port': '4444'
}
};
return settings;
})(require('./nightwatch.json'));
Selenium Version is 2.35.1

This is not an elegant solution but close to what you are looking for. Basically, we can check the value of browser.currentTest.results.errors in afterEach() and in case if the value is more than 0, then we can just pause the test there, keeping the browser session alive.
afterEach: function(browser) {
if(browser.currentTest.results.errors > 0) {
browser.pause()
}

Related

Getting console log or geckodriver.log for remote firefox browser

Using below configuration as capabilities for the remote firefox browser(saucelabs)-
12:34:32.312 INFO [ActiveSessionFactory.apply] - Capabilities are: {
"browserName": "firefox",
"moz:firefoxOptions": {
"profile": "Test1",
"prefs": {
"devtools.chrome.enabled": false,
"media.webrtc.hw.h264.enabled": true,
"media.peerconnection.ice.tcp": true,
"dom.webnotifications.enabled": false,
"media.gmp-manager.updateEnabled": true,
"devtools.console.stdout.content": true,
"media.getusermedia.screensharing.enabled": true,
"devtools.debugger.remote-enabled": true,
"media.peerconnection.video.h264_enabled": true,
"marionette": true,
"media.navigator.permission.disabled": true,
"devtools.debugger.prompt-connection": false,
"media.navigator.streams.fake": true
},
"log": {
"level": "trace"
},
"args": [
"--use-fake-device-for-media-stream",
"--use-fake-ui-for-media-stream",
"--start-maximized",
"--disable-web-security",
"enable-logging",
"-start-debugger-server",
"9222"
]
},
"selenium:webdriver.remote.quietExceptions": false
}
Yet unable to get the console info into the selenium-driver.log in saucelabs. Even setting the option "devtools.console.stdout.content": true also not yielding any file with geckodriver.log.
With the same settings, chrome console logs are being captured as part of selenium-driver.log. Is there anything to be configured for getting console output for remote firefox?
Any input on this is very much appreciated.

Is There a Problem with my Nightwatch.conf.js file?

I have been attempting to setup an automated testing framework for my job, and nodejs testing has seemed to be the best option for future scalability. Plus, I am attempting to stretch myself into new areas of QA.
I have setup my Nightwatch framework with npm install commands and manually installed the most up-to-date chrome drivers, and selenium standalone executables. I have attempted to ensure that my nightwatch.conf.js file is completely setup to run my first test with google chrome, but I get this error:
⚠ Error connecting to localhost on port 4444.
_________________________________________________
TEST FAILURE: 1 error during execution; 0 tests failed, 0 passed (249ms)
✖ GoogleTest
An error occurred while retrieving a new session
Error: An error occurred while retrieving a new session
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
Error: An error occurred while retrieving a new session
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
SKIPPED:
- Basic Google search
Which seems to be pretty generic. As per normal Nightwatch setup, I created a root folder called 'Automation' where I stuck my bin folder for the drivers and selenium framework. Inside of the 'Automation' folder I also have my nightwatch.conf.js file that I've setup (Let me know if its more helpful if I paste its code in here). In order to run my test, I use the npx nightwatch command, but I continue to get this error.
From what I can tell it may be due to me trying to run the tests locally, but I can't think of a way that I would configure the selenium service to accept local ports other than how it's been setup.
I don't know if this is a well formatted description of the problem, so I apologize in advance if this is super vague. If there's anything else I can do to help clear it up let me know.
Thank you!
Update Conf.js File
// 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: ["tests"],
// 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 : '',
webdriver: {},
test_settings: {
default: {
disable_error_log: false,
launch_url: 'https://nightwatchjs.org',
screenshots: {
enabled: false,
path: 'screens',
on_failure: true
},
desiredCapabilities: {
browserName : 'chrome'
},
webdriver: {
start_process: true,
server_path: (Services.chromedriver ? Services.chromedriver.path : '.Automation/bin/chromedriver.exe')
}
},
safari: {
desiredCapabilities : {
browserName : 'safari',
alwaysMatch: {
acceptInsecureCerts: false
}
},
webdriver: {
port: 4444,
start_process: true,
server_path: '/usr/bin/safaridriver'
}
},
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 : './Automation/bin/geckodriver.exe'),
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: true,
//More info on Chromedriver: https://sites.google.com/a/chromium.org/chromedriver/
args: ['disable-gpu', 'no-sandbox', 'headless']
}
},
webdriver: {
start_process: true,
port: 9515,
server_path: (Services.chromedriver ? Services.chromedriver.path : './Automation/bin/chromedriver.exe'),
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: true,
args: ['disable-gpu', 'no-sandbox', 'headless']
}
}
},
'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 : './Automation/bin/selenium-server-standalone-3.141.59.jar'),
cli_args: {
'webdriver.gecko.driver': (Services.geckodriver ? Services.geckodriver.path : './Automation/bin/geckodriver.exe'),
'webdriver.chrome.driver': (Services.chromedriver ? Services.chromedriver.path : './Automation/bin/chromedriver.exe')
}
}
},
'selenium.chrome': {
extends: 'selenium',
desiredCapabilities: {
browserName: 'chrome',
chromeOptions : {
w3c: true,
args: ['disable-gpu', 'no-sandbox', 'headless']
}
}
},
'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) {}
}
What I will suggest is to create a create a new file for Nightwatch.conf.js and then start adding rest of the configuration from there. Below is an example which works for me:
module.exports = {
src_folders: ["tests"],
skip_testcases_on_fail: false,
page_objects_path: "pageObjects",
custom_commands_path: "./commands",
screenshots: {
enabled: true,
path: "./screenshots",
on_failure: true,
on_error: true
},
test_settings: {
default: {
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
prefs: {
download: {
default_directory: require('path').resolve(__dirname + '/download')
}
}
},
},
webdriver: {
start_process: true,
port: 4444,
server_path: require('chromedriver').path,
}
},
test_workers: {
enabled: true,
workers: 'auto'
},
safari: {
desiredCapabilities: {
browserName: 'safari',
alwaysMatch: {
acceptInsecureCerts: false
}
},
webdriver: {
port: 4445,
start_process: true,
server_path: '/usr/bin/safaridriver'
}
},
firefox: {
desiredCapabilities: {
browserName: 'firefox'
},
webdriver: {
start_process: true,
port: 4446,
server_path: require('geckodriver').path
}
}
}
}
The Nightwatch JS, chromedriver, geckodriver are installed using the npm commands. Since I am using a mac I just needed to enable the safari webdriver.
npm install nightwatch --save-dev
npm install geckodriver --save-dev
npm install chromedriver --save-dev
safaridriver --enable
This is the full repo if you want to have a look: https://github.com/alapanme/NightwatchJS
Also if you need the detailed steps how I setup Nightwatch on my machine you can refer: https://testersdock.com/nightwatch-js-installation/

Protractor tests with Muliple browsers got failed at startup

I am trying to run e2e tests for chrome and firefox and I am unable to load the chrome browser with following configuration ,any comment is much appreciated , thanks
var HtmlReporter = require('protractor-beautiful-reporter');
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: [ **some spec**
],
multiCapabilities: [{
"browserName": "firefox"
},
{
"browserName": "chrome",
}
],
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 180000
},
allScriptsTimeout: 200000,
onPrepare: function () {
browser.manage().timeouts().implicitlyWait(20000);
jasmine.getEnv().addReporter(new HtmlReporter({
baseDirectory: 'test-result',
preserveDirectory: false,
takeScreenShotsOnlyForFailedSpecs: true,
screenshotsSubfolder: 'images'
}).getJasmine2Reporter());
}
};
try directConnect: true instead of using seleniumAddress: http://localhost:4444/wd/hub
or make sure your grid server is up and running and also selenium server is up and running
command to start selenium server webdriver-manager start

WebdriverIO: Not able to access browser capabilities when using multiRemote

I have setup up my wdio.conf.js to use multiple browsers in my tests as described on the WebdriverIO website. (the capabilities are defined as an object, if using multiremote feature)
In my spec file, when I try to perform an action, such as MyFirefoxBrowser.url('https://myUrl') ... (what is happening?)
!Note: When I refer to the browser object directly, both Chrome and Firefox instances are spawned, as expected.
I have tried referring the wdio.conf.js file inside my spec file using require, but it didn't work.
Spec file (.js):
describe('webdriver.io page', () => {
it('should have the right title', () => {
myFirefoxBrowser.url('https://webdriver.io');
const title = myFirefoxBrowser.getTitle()
assert.strictEqual(title, 'WebdriverIO · Next-gen WebDriver test framework for Node.js')
});
});
Capabilities (as defined in the wdio.conf.js):
capabilities: {
myChromeBrowser: {
capabilities: {
browserName1: 'chrome'
}
},
myFirefoxBrowser: {
capabilities: {
browserName: 'firefox'
}
}
},
Error:
ReferenceError: mychromeBrowser is not defined
Expected Results: Only the Firefox browser should navigate to the requested url.
myChromeBrowser != mychromeBrowser. Check this. Also not sure what do you want to do with this. browser is object for every browser and if you want to decide on type of it, you can via browser.capabilities
and you will get object
{ acceptInsecureCerts: false,
browserName: 'chrome',
browserVersion: '78.0.3904.97',
chrome:
{ chromedriverVersion:
'78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904#{#800})',
userDataDir: '/tmp/.com.google.Chrome.Ihm6cA' },
'goog:chromeOptions': { debuggerAddress: 'localhost:45423' },
networkConnectionEnabled: false,
pageLoadStrategy: 'normal',
platformName: 'linux',
proxy: {},
setWindowRect: true,
strictFileInteractability: false,
timeouts: { implicit: 0, pageLoad: 300000, script: 30000 },
unhandledPromptBehavior: 'dismiss and notify' }
You should use browser.
capabilities should be defined as an array
// wdio.conf.js
exports.config = {
// ...
capabilities: [{
browserName: 'firefox'
}, {
browserName: 'chrome',
// ...
proxy: {
proxyType: "manual",
httpProxy: "corporate.proxy:8080",
socksUsername: "codeceptjs",
socksPassword: "secret",
noProxy: "127.0.0.1,localhost"
},
// ...
}],
// ...
}
Late to the party, I think you have a typo. It should be browserName in capabilities.myChromeBrowser and not browserName1. Otherwise the code should do as you want.
capabilities:
{
myChromeBrowser: {
capabilities: {
browserName: 'chrome'
}
},
myFirefoxBrowser: {
capabilities: {
browserName: 'firefox'
}
}
},

goog:chromeOptions being ignored completely using RSpec, Capybara, and Selenium

Is anyone else having this problem? It seems that somewhere goog:chromeOptions are not getting passed to chromedriver properly - which results in Chrome just opening with default options.
I'm using the following Capybara/Selenium configuration:
Capybara.register_driver :chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
'goog:chromeOptions': {
args: %w[ start-maximized ]
}
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: capabilities,
driver_opts: {
log_path: "./tmp/chrome#{Time.now.to_i}.log",
verbose: true
}
)
end
Capybara.javascript_driver = :chrome
However, when I open up the Capybara session log, the goog:chromeOptions have not been set at all. Is this why my Chrome window is not maximized?
Session log:
[1550680994.143][INFO]: COMMAND InitSession {
"capabilities": {
"firstMatch": [ {
"browserName": "chrome",
"goog:chromeOptions": {
//nothing is here??? should have args!
}
} ]
},
"desiredCapabilities": {
"browserName": "chrome",
"cssSelectorsEnabled": true,
"goog:chromeOptions": {
//nothing is here??? should have args!
},
"javascriptEnabled": true,
"nativeEvents": false,
"platform": "ANY",
"rotatable": false,
"takesScreenshot": false,
"version": ""
}
}
Operating System:
Ubuntu 18.04
My environment:
ruby 2.6.1
capybara (2.18.0)
selenium-webdriver (3.13.0)
ChromeDriver 2.37.544315
Everytime I run a Selenium test, the window is not maximized. But this isn't another "the screen isn't maximized" post (there are lots of those already). This appears to be an issue where my options are not being parsed properly perhaps? I don't get whats wrong. I'm following all the README's and guides as best I can. Its just not working :(
Fixed it by upgrading Capybara and Selenium Webdriver!
capybara (3.13.2)
selenium-webdriver (3.141.0)
Now the debug log has what I was expecting to see:
[1550686685.534][INFO]: COMMAND InitSession {
"capabilities": {
"firstMatch": [ {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "start-maximized" ]
}
} ]
},
"desiredCapabilities": {
"browserName": "chrome",
"cssSelectorsEnabled": true,
"goog:chromeOptions": {
"args": [ "start-maximized" ]
},
"javascriptEnabled": true,
"nativeEvents": false,
"platform": "ANY",
"rotatable": false,
"takesScreenshot": false,
"version": ""
}
}