'Allow' Chrome Geolocation popup in WebdriverIO - webdriver-io

I want to enable geolocation(using service: dev-tools) when webdriverIO spins up the chrome browser by default. Any idea where can I make the change to achieve this? I am faking my geolocation via browser.cdp but when the browser spins up it asks me to 'Allow' the location first before the test proceeds and I don't want this manual intervention.
I have tried a lot of options in wdio.config file, latest one being-
browserName: 'chrome',
'goog:chromeOptions': {
prefs: { 'credentials_enable_service': false, 'profile': { 'password_manager_enabled': false, 'default_content_setting_values': { 'geolocation': 1 } } }
},
services: ['devtools'],
But the above configuration is also not working.
My code in test file is as follows -
it('TC070_Geolocations', () => {
browser.url('https://www.where-am-i.co/')
browser.cdp('Emulation', 'setGeolocationOverride', {
latitude:51.507351,
longitude:-0.127758,
accuracy: 1
})
browser.cdp('Emulation', 'setTimezoneOverride', {
timezoneId: 'Europe/London'
})
browser.pause(5000)
browser.saveScreenshot('Screenshots/a.png')
})

Related

Cypress 12 Component Tests Wont Load

I am trying to use Cypress 12 to run compnent tests in a Vue.js 2 app. Below is my cypress.config.ts file:
import { defineConfig } from "cypress";
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
baseUrl: "http://localhost:9090/.......",
defaultCommandTimeout: 60000,
},
component: {
devServer(cypressConfig: CypressConfiguration) {
// return devServer instance or a promise that resolves to
// a dev server here
return {
port: 9090,
close: () => {},
};
},
},
});
I setup a custom devServer in vue.config.js (otherwise Cypress starts uses its own localhost):
module.exports = {
devServer: {
port: 9090,
proxy: 'http://localhost:8080'
}
}
However, the tests wont load
When I run e2e tests, all is fine: tests appears, calls localhost:9090. However, if I want to run only component tests, it just gets stuck trying to load the tests.
It is not a DevTools problem as I have looked into that. All other configuration settings are standard.

How to run the UI script in Headless mode for edge browser

I have an scenario where I want to trigger the UI script in Microsoft Edge with headless mode. How to achieve this?
* configure driver = { type: 'msedgedriver', webDriverSession: { capabilities: { browserName: 'edge' } }, executable: '#(executable)' }
Have you tried passing the headless flag inside the capabilities? Something like this
* configure driver = { type: 'msedgedriver', webDriverSession: { capabilities: { browserName: 'edge', headless: true } }, executable: '#(executable)'}
Taken from the documentation mentioned here.

What is the correct config settings to automate test in parallel using Protractor?

Here is my settings. Is there a correct setting for angular/non-angular application test in parallel? Sometimes, either my firefox or chrome hangs while the other one is running. Is it the ignoreSynchronization suppose to be set to true and waitForAngular to be false? I feel like there is too much time syncing problem that is causing one of the browsers to hang?
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
getPageTimeout: 600000,
allScriptsTimeout: 500000,
defaultTimeoutInterval: 30000,
framework: 'custom',
// path relative to the current config file
frameworkPath: require.resolve('protractor-cucumber-framework'),
multiCapabilities:
[{
'browserName': 'firefox',
specs: 'features/firefox/*.feature',
},
{
'browserName': 'chrome',
specs: 'features/chrome/*.feature',
}],
maxSessions: 2,
baseUrl: 'https://localhost:8080',
cucumberOpts: {
strict: true,
require: [
'hooks/hooks.js',
'specs/*Spec.js'
],
tags: [
"#runThis",
"~#ignoreThis"
],
profile: false,
format: 'json:./e2e/reports/cucumber-report.json',
resultJsonOutputFile: './e2e/reports/cucumber-report.json'
},
beforeLaunch: function() {
const fs = require('fs');
const path = require('path');
const directory = './e2e/reports';
//cleans up the json results from the previous build when using node flake
fs.readdir(directory, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(directory, file), err => {
if (err) throw err;
});
}
});
},
onPrepare: function() {
var chai = require('chai');
chai.use(require('chai-as-promised'));
global.expect = chai.expect;
browser.ignoreSynchronization = true;
browser.manage().window().maximize();
browser.waitForAngular(false);
browser.manage().timeouts().implicitlyWait(30000);
},
ghostMode:false
}
browser.ignoreSynchronization is deprecated so you do not need to set that. However you do need to set browser.waitForAngularEnabled(false) instead of browser.waitForAngular(false) like you have in your conf. waitForAngular is what is called before every action when waitForAngularEnabled is true.
Setup your onPrepare like
onPrepare: function() {
var chai = require('chai');
chai.use(require('chai-as-promised'));
global.expect = chai.expect;
browser.manage().window().maximize();
browser.waitForAngularEnabled(false);
browser.manage().timeouts().implicitlyWait(30000);
},
Your specific situation will depend on what you are trying to achieve from your parallel execution.
This setup will divide your tests across 2 browser types, chrome and firefox, and execute your tests in parallel. It will support 3 chrome and 2 firefox browsers running at any one time. Tests are divided based on whichever finishing first
multiCapabilities: [
{ browserName: "chrome", shardTestFiles: true, maxInstances: 3 },
{ browserName: "firefox", shardTestFiles: true, maxInstances: 2 },
],
maxSessions: 10, //controls the total number of instances, won't be relevant in this case
This setup will execute all your tests on BOTH a chrome and firefox browser. If you are running 5 specs you will have 10 results.
multiCapabilities: [
{ browserName: "firefox" },
{ browserName: "chrome" },
],
Above given answers with adding shardTestFiles: true, and number of maxInstances should work. However I would still want to highlight that if you are using webdriver-manager start to start selenium server (which I think you are based on the selenium address given in config file) then do not expect that it will work as smooth as selenium grid solution to run tests in parallel on different browsers.
Webdriver-manager is designed as a quickest solution to start selenium server and is not a replacement of selenium-grid.

Nightwatch, How to call 'Setup' and 'Teardown' API globally

I am just getting started learning nightwatchjs to test my webapp. I need to call a 'setup' and a 'teardown' script before and after everything else, respectively. These scripts create the necessary conditions in the database for the tests to run (creating test licenses, users, etc), and remove them afterward. I have an API call in my app that triggers these.
In globals.js, you can set before and after methods that should execute before and after everything else, and also a beforeEach and afterEach that should execute before and after each test suite, if I'm not mistaken. It seems the beforeEach and afterEach methods accept as arguments a browser object and a done callback. The before and after methods, however, only get the done callback.
In a particular test suite, the same four methods can be added, but in this case, the before and after run before everything and after everything, respectively, and the beforeEach and afterEach run before and after each individual test in the suite.
Ideally, I would call my setup and teardown scripts in the global before and after methods, but those do not get an instance of nightwatch (browser), so I don't know how I'd do that.
I am able to fire off the 'setup' call just fine, in my globals beforeEach method. This isn't ideal for me, but it would still work, it would just be a but superfluous (calling it before each test suite rather than just once before everything).
However, I run into issues when I try to call the teardown script from my globals afterEach method. When I run my test, the output hangs at the end, until I hit CTRL+C.
Here is my nightwatch.conf.js:
const seleniumServer = require("selenium-server");
const chromedriver = require("chromedriver");
// we use a nightwatch.conf.js file so we can include comments and helper functions
module.exports = {
"src_folders": [
"tests",
],
"page_objects_path": './pages',
"globals_path": "./globals.js",
"output_folder": "./reports", // reports (test outcome) output by nightwatch
"custom_commands_path" : "./commands",
"selenium": {
"start_process": true, // tells nightwatch to start/stop the selenium process
"server_path": seleniumServer.path,
"host": "127.0.0.1",
"port": 4444, // standard selenium port
"cli_args": {
"webdriver.chrome.driver" : chromedriver.path
}
},
"test_settings": {
"default": {
"silent": true,
"launchUrl": 'http://local.mytestwebsite.com',
"screenshots": {
"enabled": true, // if you want to keep screenshots
"path": "./screenshots/" // save screenshots here
},
"globals": {
"waitForConditionTimeout": 5000 // sometimes internet is slow so wait.
},
"desiredCapabilities": { // use Chrome as the default browser for tests
"browserName": "chrome",
"chromeOptions": {
"args": [
"window-size=1366,768",
"--incognito"
]
}
}
}
}
}
Here is my globals.js:
var chromedriver = require('chromedriver');
module.exports = {
beforeEach: function(browser, done) {
console.log('Executing the global `beforeEach`');
browser.url('http://www.vulfpeck.com');
browser.expect.element('body').to.be.present;
browser.end();
// getting the session info
browser.status(function(result) {
console.log("Session Info: ", result.value);
done();
});
},
afterEach: function(browser, done){
console.log('Executing the global `afterEach`');
browser.url('http://www.vulfpeck.com');
browser.expect.element('body').to.be.present;
browser.end();
// getting the session info
browser.status(function(result) {
console.log("Session Info: ", result.value);
done();
});
},
before: function(done) {
console.log('Executing the global `before`');
console.log('starting the chromedriver');
chromedriver.start();
done();
},
after: function(done) {
console.log('Executing the global `after`');
console.log('stoppin the chromedriver');
chromedriver.stop();
done();
}
};
And here is my test suite:
module.exports = {
'Test 1': function(browser){
browser.url('http://www.google.com');
browser.expect.element('body').to.be.present;
browser.end();
},
'Test 2': function(browser){
browser.url('http://www.vulfpeck.com');
browser.expect.element('body').to.be.present;
browser.end();
},
before: function(browser, done){
console.log('test before');
done();
},
beforeEach: function(browser, done){
console.log('test beforeEach');
done();
},
after: function(browser, done){
console.log('test after');
done();
},
afterEach: function(browser, done){
console.log('test afterEach');
done();
}
};
Here is the output.
$ nightwatch
Executing the global `before`
starting the chromedriver
Starting selenium server... started - PID: 11772
[Login] Test Suite
======================
Executing the global `beforeEach`
√ Expected element <body> to be present - element was present in 31ms
Session Info: { ready: true,
message: 'Server is running',
build:
{ revision: '63f7b50',
time: '2018-02-07T22:42:28.403Z',
version: '3.9.1' },
os: { arch: 'amd64', name: 'Windows 10', version: '10.0' },
java: { version: '9' } }
test before
Running: Test 1
test beforeEach
√ Expected element <body> to be present - element was present in 30ms
test afterEach
OK. 1 assertions passed. (3.56s)
Running: Test 2
test beforeEach
√ Expected element <body> to be present - element was present in 20ms
test afterEach
OK. 1 assertions passed. (2.503s)
test after
Executing the global `afterEach`
Session Info: { ready: true,
message: 'Server is running',
build:
{ revision: '63f7b50',
time: '2018-02-07T22:42:28.403Z',
version: '3.9.1' },
os: { arch: 'amd64', name: 'Windows 10', version: '10.0' },
java: { version: '9' } }
× Expected element <body> to be present - element was not found - expected "present" but got: "not present"
at Object.afterEach (C:\sites\mytestwebsite.com\selenium\globals.js:29:21)
So, am I missing something? Why can I not hit a URL in the global afterEach method without it hanging? (I just figured out that if I remove the browser.end(); from my last test in the suite, my global afterEach will work just fine. Why is this?). Is there some other recommended way of hitting a URL before and after running all tests?
Thanks! Any help is appreciated!
While I haven't completely answered my own question, I have come up with an alternate way to call my setup and teardown scripts in the globals before and after hooks.
For this, I have installed the node libarary child_process (by typing npm install child_process), which allows me to execute commands on the command line.
In my globals.js, I am requiring that library at the top:
const { exec } = require('child_process');
And in my before and after hooks, I am using it like so:
exec('php ../run.php api selenium setup', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.log("node couldn't execute the command",err);
done();
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
done();
});
Hope this helps someone.

Pass parameters for each browser using Protractor

Just got started with Protractor for E2E testing.
I want to pass parameters (login and password) for each instance of chrome selenium server.
I want test the same spec file with different user account in parallel.
This is my conf.js :
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'args': ['--disable-web-security']
},
count: 10
},
You can use the onPrepare-method of Protractor for that. If multiple capabilities are being run, this will run once per capability. You can add data to the browser-object that you can use during your execution.
What you can do is something like this
// A JSON file or something
var login = {
"chrome": {
"user": "usernameChrome",
"pass": "passwordChrome"
},
"firefox": {
"user": "usernameFirefox",
"pass": "passwordFirefox"
}
};
// in your config
// An example configuration file.
exports.config = {
directConnect: true,
// Capabilities to be passed to the webdriver instance.
multiCapabilities: [{
'browserName': 'chrome'
},
{
'browserName': 'firefox'
}
],
// Framework to use. Jasmine is recommended.
framework: 'jasmine',
// Spec patterns are relative to the current working directory when
// protractor is called.
specs: ['example_spec.js'],
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
},
onPrepare: function() {
return browser.getCapabilities()
.then((capabilities) => {
// Get the current browser you are using
browser.browserName = capabilities.get('browserName').toLowerCase();
// Add the user and pass to the browser-object
browser.user = login[browser.browserName].user;
browser.pass = login[browser.browserName] pass;
});
}
};
// In your spec
describe('logon', function() {
it('should logon', function() {
browser.get('http://www.example.com');
element(by.model('user')).sendKeys(browser.user);
element(by.model('pass')).sendKeys(browser.pass);
element(by.tagName('button')).click();
});
});
You can handle this with Protractor's params on the command line. For example, you can start each test with a different username/password like this:
protractor conf.js --params.username user1 --params.password password1
Then, in your test, you would use them something like this:
logIntoMyApp(browser.params.username, browser.params.password);
You can also set defaults in your config file (see the docs for details).