Custom args for each instance of Chrome when running parallel tests with Protractor - selenium

Is it possible to pass custom args for each instance of Chrome when running parallel tests in Protractor? I need to know the Remote debugging port for each instance so I can connect with the Dev Tools protocol.
As I see it there's two options. Set the port to a specific unique value for each instance, or let it be set automatically and fetch it somehow when preparing the tests. Would it be possible with any of these options?
exports.config = {
framework: 'jasmine',
chromeDriver: chromeDriverPath,
multiCapabilities: [{
browserName: 'chrome',
chromeOptions: {
args: process.env.HEADLESS && puppeteer ? ['--headless', `--remote-debugging-port=${DEV_TOOLS_PORT}`] : [`--remote-debugging-port=${DEV_TOOLS_PORT}`],
binary: puppeteer.executablePath()
},
shardTestFiles: true,
maxInstances: 1
}]
}

If I got the problem right, what you could do is to pass parameters as env variables to protractor on start. So your config would look like this:
exports.config = {
framework: 'jasmine',
chromeDriver: chromeDriverPath,
multiCapabilities: [{
'browserName': 'chrome',
'chromeOptions': {
args: [`--remote-debugging-port=${process.env.PORT_ONE}`]
}
}, {
'browserName': 'chrome',
'chromeOptions': {
args: [`--remote-debugging-port=${process.env.PORT_TWO}`]
}
}]
}
And then start your protractor process with env variables like so:
PORT_ONE=90 PORT_TWO=80 protractor protractor.conf.js

One of the two options are solved. But I would still like to know if there's a possibility to use custom args for each instance.
// Get Remote debugging port for chrome
let chromeRemoteDebuggingPort;
browser.getCapabilities().then((capabilities) => {
const chromeOptions = capabilities.get('goog:chromeOptions');
if( chromeOptions && chromeOptions.debuggerAddress) {
chromeRemoteDebuggingPort = chromeOptions.debuggerAddress.split(':')[1];
}
});

Related

Can I run Nightwatch js in paralell with a different env per worker?

I'm currently using Nightwatch js to run my E2E tests in paralell.
The issue I'm having is that my tests share the same database, which is causing me problems with shared data being rewritten across different tests/workers, resulting in flaky tests.
I thought about running each test worker with its own database, but I'm not sure how to do it in practice. My starting point would be having different settings for the test databases, each on a night-watch env which can be accessed by the individual workers, but so far I've not found if this would be possible.
Any ideas?
Yes you can run different tests on different workers, Nightwatch provides a way to do that. I have done something very similar where I ran the same test on three different browsers parallely.
You have to add the test_Worker configuration, which allows the tests to be run in parallel. When this is enabled the test runner will launch a configurable number of child processes and then distribute the loaded tests over to run in parallel.
The workers option configures how many child processes can run concurrently.
a) “auto” – determined by the number of CPUs e.g. 4 CPUs means 4 workers
b) {number} – specifies an exact number of workers
test_workers: {
enabled: true,
workers: 'auto'
}
This is how my Nightwatch.conf.js file looks like:
{
module.exports = {
src_folders: ["tests"],
test_settings: {
default: {
desiredCapabilities: {
browserName: 'chrome'
},
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
}
}
}
}
Now to execute the test I use the command:
npx nightwatch tests/TC001_WikiSearch.js -e default,firefox,safari
Referenced from: https://testersdock.com/execute-parallel-tests-nightwatchjs/

wdio chrome headless is not running headlessly

Headless chrome doesn't seem to be headless for me. I'm using wdio and have this as my configuration:
capabilities: [
{
// maxInstances can get overwritten per capability. So if you have an in-house Selenium
// grid with only 5 firefox instances available you can make sure that not more than
// 5 instances get started at a time.
maxInstances: 5,
//
browserName: 'chrome',
args: ['--headless', '--disable-gpu', '--window-size=1280,800'],
binary: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
}
]
I'm also outputting what the capabilities are prior to the browser starting:
{
"maxInstances": 5,
"browserName": "chrome",
"args": [
"--headless",
"--disable-gpu",
"--window-size=1280,800"
],
"binary": "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
}
My chrome browser is launching and I can see webdriver driving the test. Everything I have many posts doing it in this manner and it's supposed to just work. What am I missing?
UPDATE
I've modified capabilities to be read in from an environment variable. If I use BROWSER=chrome, I see the proper capabilities go through and the browser starts in chrome. If I use BROWSER=firefox, firefox opens up and I see the proper capabilities. If I don't use anything I see the proper capabilities, chrome opens up, but it's not headless.
const CHROME = {
browserName: 'chrome',
};
const FIREFOX = {
browserName: 'firefox',
};
const CHROME_HEADLESS = {
browserName: 'chrome',
args: ['headless', 'disable-gpu']
};
function getCapabilities() {
let browser;
switch(process.env.BROWSER && process.env.BROWSER.toLowerCase()) {
case 'chrome':
browser = CHROME;
break;
case 'firefox':
browser = FIREFOX;
break;
default:
browser = CHROME_HEADLESS;
break;
}
return [Object.assign({maxInstances: 5}, browser)];
}
To go along with the accepted answer, in newer version of Selenium (3.8 and higher) you may have to specify chromeOptions as "goog:chromeOptions"
https://gist.github.com/disintegrator/ff6e9341860e9b121099c71bc9381bd6
Have the capabilities inside your chrome options.
It works fine for me.
capabilities: [
{
      browserName: 'chrome',
      chromeOptions: {
        args: ['headless', 'disable-gpu'],
      },
    },
  ],

How to skip protractor+jasmine tests specific to browsers

Assume that I've automated 25 tests and executing in multiple browsers like chrome, firefox, IE, Edge & Safari. All tests (25) are executing well on Chrome. In Firefox, Only 20 tests are running fine due to few protractor APIs are not supported. Similarly IE can execute only 23 tests.
I would like to skip the test only for browsers, which are not supported for particular test? Is there any way available?
You can create protracotr.conf file for each browser with specific suites where will be specified what tests should run. And execute in one time all protractor.conf files.
//protractor.chrome.conf
export let config: Config = {
...
capabilities: {
browserName: 'chrome',
shardTestFiles: true,
maxInstances: 1
},
SELENIUM_PROMISE_MANAGER: false,
specs: [
'../test/chrome/**/*.js'
]
};
and
//protractor.ie.conf
export let config: Config = {
...
capabilities: {
browserName: 'internet explorer',
shardTestFiles: true,
maxInstances: 1
},
SELENIUM_PROMISE_MANAGER: false,
specs: [
'../test/ie/**/*.js'
]
};
in your package.json:
{
...
"scripts": {
"test:all": "npm run test:chrome && test:ie",
"test:chrome": "protractor ./config/protractor.chrome.conf.js",
"test:ie": "protractor ./config/protractor.ie.conf.js",
...
},
...
}
With jasmine2 you can filter tests using a regular expression. Maybe you can add something like #chrome, #ie to your tests and then only run those ones by passing the grep flag:
it('should do stuff #ie #chrome', function() {
...
});
Then run protractor passing the grep flag:
protractor conf.js --grep='#ie'

Webdriver.io - visual regression Service compare not recognized with multi remote

I got visual regression working in webdriver.io and it is very wonderful. I would, however, like to also use it with multi remote. When I add the necessary configuration to my wdio.conf.js file for multiremote, I get the following error
Error: Please provide a visualRegression configuration with a compare method in your wdio-conf.js!
when running my tests and the regression service no longer works.
My relevent wdio.conf.js settings are configured as such:
//...
capabilities: {
browserA: {
desiredCapabilities: {
browserName: 'chrome'
}
},
browserB: {
desiredCapabilities: {
browserName: 'chrome'
}
}
},
// ...
services: ['visual-regression'],
visualRegression: {
compare: new VisualRegressionCompare.LocalCompare({
referenceName: getScreenshotName(path.join(process.cwd(), 'screenshots/reference')),
screenshotName: getScreenshotName(path.join(process.cwd(), 'screenshots/taken')),
diffName: getScreenshotName(path.join(process.cwd(), 'screenshots/diff')),
}),
viewportChangePause: 500,
widths: [1280, 1600],
orientations: ['landscape', 'portrait'],
},
Any help would be much appreciated!

How to setup Selenium grid remotely in protractor

I have my conf.js file which runs fine locally. But now as per my requirement I need to run it in a bamboo task. Since my code is going to run on server there is a remote selenium webdriver that i need to add to the conf.js.
Can you please help me out how it is done? My conf.js looks like this :
exports.config = {
params: {
url: "URL",
testroadname:"Testing123",
sleeptime:1000
},
directConnect: true,
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},
// Framework to use. Jasmine is recommended.
framework: 'jasmine',
// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['mapfeedback.js'],
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
}
You have to remove directConnect = true and just give the seleniumAddress in your config or setup an environment.js file- you can simply add your remote selenium's address and fire up your protractor tests, following will be your environment.js-
// Common configuration files with defaults plus overrides from environment vars
var webServerDefaultPort = 8081;
module.exports = {
// The address of a running selenium server.You can add your remote address here
seleniumAddress:
(process.env.SELENIUM_URL || 'http://localhost:4444/wd/hub'),
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName':
(process.env.TEST_BROWSER_NAME || 'chrome'),
'version':
(process.env.TEST_BROWSER_VERSION || 'ANY')
},
// Default http port to host the web server
webServerDefaultPort: webServerDefaultPort,
// Protractor interactive tests
interactiveTestPort: 6969,
// A base URL for your application under test.
baseUrl:
'http://' + (process.env.HTTP_HOST || 'localhost') +
':' + (process.env.HTTP_PORT || webServerDefaultPort)
};
your config file will look like -
var env = require('./environment.js');
exports.config = {
params: {
url: "URL",
testroadname:"Testing123",
sleeptime:1000
},
seleniumAddress: env.seleniumAddress,
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},
// Framework to use. Jasmine is recommended.
framework: 'jasmine',
// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['mapfeedback.js'],
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
}
To setup selenium-grid remotely for Protractor :-
start hub with command :-
java -jar selenium-server-standalone-3.3.1.jar -role hub -port 4444
Now start your remote node with command where you want execute remote test cases :-
java -jar selenium-server-standalone-3.3.1.jar -role webdriver -hub http://HubDomain-IP:4444/grid/register -port 5556 -browser browserName=internet explorer,maxInstances=1,platform=WINDOWS,applicationName=remoteNode -Dwebdriver.ie.driver=path to IEDriverServer.exe
Once your selenium-grid setup is done and node get connected with Hub, then take config file with configuratiion set as this conf.js file:-
exports.config = {
//The address of your running selenium Hub server.
seleniumAddress: 'http://HubDomain-IP:4444/wd/hub',
//Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'internet explorer',
// add node-name as applicationName where you have started node and wanted to remotely execute test cases
'applicationName': 'remoteNode'
},
//Specify the name of the specs files.
specs: ['grid_spec.js'],
//Options to be passed to Jasmine-node.
jasmineNodeOpts: {
onComplete: null,
isVerbose: false,
showColors: true,
includeStackTrace: true
}
};
With above mentioned configuration and setup commands you could run selenium-grid setup for Protractor.Thanks !