Activating chrome language flags when activating from protractor (selenium) - angularjs

I'm writing end to end tests with Protractor for an angular website.
We have to support certain languages so I would like to init chrome using the --lang flag and start it with some other language. I searched the web and couldn't find any example to how it can be done.
My only lead was some article I saw and understood that I need to add to Protractor config file the "capabilities" section and there I can define the "args" property.
Then tried to tinker with it but no luck.
Any help will be most welcome.
Thanks,
Alon

How to set Browser language and/or Accept-Language header
exports.config = {
capabilities: {
browserName: 'chrome',
chromeOptions: {
// How to set browser language (menus & so on)
args: [ 'lang=fr-FR' ],
// How to set Accept-Language header
prefs: {
intl: { accept_languages: "fr-FR" },
},
},
},
};
More examples:
intl: { accept_languages: "es-AR" }
intl: { accept_languages: "de-DE,de" }

As it turns out - the answer was in the docs all along.
This is how you do it for chrome and i guess it similar for other browsers:
inside the protractor.conf.js (for Spanish):
capabilities: {
browserName: 'chrome',
version: '',
platform: 'ANY',
'chromeOptions': {
'args': ['lang=es-ES']}
}

Related

Using headless chrome displays empty part of the page

When using chromedriver in a protractor script, to test an angular page, I obtain different results using "Headless" or "Normal" browser.
Actually, if I use a "repeater" locator, to display the items in the "empty" list, it returns that there are 5 items, but the "headless" chrome driver fails to render them. Look at the screenshots.
I'm using ChromeDriver 2.45, which supports Chrome Version 70 to 72, I have version 71.
My OS is Windows 10.
Protractor version 5.1.1
Angularjs version 1.5
Here's the configuration file:
exports.config = {
directConnect: true,
rootElement: 'html',
chromeDriver: 'C:\\srv\\build\\applications\\chromedriver\\chromedriver_win32\\chromedriver.exe',
getPageTimeout: 60000,
allScriptsTimeout: 60000,
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
capabilities: {
'browserName': 'chrome',
chromeOptions:{
args:["--headless"]
}
},
specs: [ 'features/*.feature' ],
baseUrl: '',
cucumberOpts: {
tags: '',
require: [ 'steps/*.spec.js' ],
monochrome: true,
strict: true,
plugin: "json"
},
};
Seems to problem is in the window size. By default, headless mode is not a fullscreen, so some elements are automatically moved and hidden (as when you try manually to resize the window)
Just add to your conf:
driver.manage().window().maximize();
Can you update args to
capabilities: {
...,
chromeOptions: {
args: ["--headless", "--disable-gpu", "--window-size=800x600"]
}
}
For the exact configuration mentioned i am able to automate headless with no issues.
Reference https://github.com/angular/protractor/blob/master/docs/browser-setup.md#using-headless-chrome

Electron: Run end to end tests with protractor

We are making a desktop app with Electron using AngularJS, we want to make end-to-end tests using Protractor, the problem is that we got issues on running it, we don't really know what is the problem but looks like there is some synchronization problems, like it's not waiting for the page to load, not closing the app after the test is finished.
Here is the protractor configuration file:
const ELECTRON_PATH = '/usr/bin/electron';
const APP_PATH = '/home/me/project/myapp';
exports.config = {
directConnect: true,
framework: 'mocha',
specs: [
'test.js'
],
capabilities: {
browserName: 'chrome',
platform: 'LINUX',
chromeOptions: {
binary: ELECTRON_PATH,
args: ['app=' + APP_PATH, '--e2e-testing']
}
},
onPrepare: function() {
browser.ignoreSynchronization = true;
browser.resetUrl = 'file://';
}
};

Protractor e2e test case for download file work fine on chrome but not with firefox and vice versa?

I have a scenario where I need to click on a link which will trigger .CSV file download to a default location(/tmp)and it is working fine on both chrome and firefox browsers but based on multiCapabilities configuration in conf.js, at times it work only on single browser(means one set of configuration helps in chrome working fine but not the firefox and the other set results in firefox to work but not the chrome).And I used the following stackoverflow post as the reference : Protractor e2e test case for downloading pdf file. And my attempt worked fine someway but then script hits only on chrome or firefox based on multiCapabilities configuration I used.
Please note that chrome will work with following config and in this I haven't added the firefox profile setup . So file download part in firefox wont work with the following configuration.
multiCapabilities: [
{
'browserName': 'chrome',
'platform': 'ANY',
'chromeOptions': {
args: ['--no-sandbox', '--test-type=browser'],
prefs: {
download: {
'prompt_for_download': false,
'directory_upgrade': true,
'default_directory': '/tmp'
}
}
}
},
{
'browserName': 'firefox',
}
],
Based on the above mentioned url(Protractor e2e test case for downloading pdf file) I added the function getFirefoxProfile() in my util file : common.js
var getFirefoxProfile = function() {
var deferred = q.defer();
var firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", '/tmp');
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext");
firefoxProfile.encoded(function(encodedProfile) {
var multiCapabilities = [{
browserName: 'firefox',
firefox_profile : encodedProfile
}];
deferred.resolve(multiCapabilities);
});
return deferred.promise;
}
exports.getFirefoxProfile = getFirefoxProfile;
And then I updated the conf.js as follows:
getMultiCapabilities: com.getFirefoxProfile,
multiCapabilities: [
{
'browserName': 'chrome',
'platform': 'ANY',
'chromeOptions': {
args: ['--no-sandbox', '--test-type=browser'],
prefs: {
download: {
'prompt_for_download': false,
'directory_upgrade': true,
'default_directory': '/tmp'
}
}
}
},
{
'browserName': 'firefox',
}
],
And getMultiCapabilities: com.getFirefoxProfile when used in conf.js will override both capabilities and multiCapabilities mentioned in the conf.js and when I run my script, it executes the script only on firefox and not in chrome.
Any idea on how to fix this? And my requirement is to login to chrome, perform the csv download, logout from chrome, then login to firefox and do the same.
Any help would be greatly appreciated..
For adding capabilities to multiple browser(chrome and firefox), we need to use multiCapabilities, and add capabilities of each browser(firefox and chrome) as follows.
Note : Here I configured multi capabilities with promises.
var q = require("q");
var FirefoxProfile = require("firefox-profile");
exports.config = {
directConnect: true,
onPrepare: function () {
browser.driver.getCapabilities().then(function(caps){
browser.browserName = caps.get('browserName');
});
},
maxSessions: 1,
getPageTimeout: 150000,
allScriptsTimeout: 150000,
params: require('../testdata/data.json'),
framework: 'jasmine2',
specs: ['../unit_test/*_spec.js'],
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 150000
},
//seleniumAddress: "http://127.0.0.1:4444/wd/hub",
getMultiCapabilities: function() {
var deferred = q.defer();
var multiCapabilities = [
{
'browserName': 'chrome',
'platform': 'ANY',
'chromeOptions': {
args: ['--no-sandbox', '--test-type=browser'],
prefs: {
download: {
'prompt_for_download': false,
'directory_upgrade': true,
'default_directory': '/tmp'
}
}
}
},
];
// Wait for a server to be ready or get capabilities asynchronously.
setTimeout(function() {
var firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("javascript.enabled", false);
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", '/tmp');
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext");
firefoxProfile.encoded(function (encodedProfile) {
var capabilities = {
"browserName": "firefox",
"firefox_profile": encodedProfile
};
multiCapabilities.push(capabilities);
deferred.resolve(multiCapabilities);
});
}, 1000);
return deferred.promise;
}
};
The default download location is set as /tmp for both browsers and for firefox to set capabilities, we need create a firefox profile and set preference.
Note :
"browser.download.folderList", 2 ==> set the download location as user defined. passing value 0 set download to desktop and 1 will download to default download location.
Also "browser.helperApps.neverAsk.saveToDisk", "text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext" ==> mime type used is csv mime type.
If your download file is pdf or some other, replace the csv mime type with your files mime type.

How to loop in Protractor config file?

I am new to JS and Protractor for testing functionalities. I need to include some conditions or loops inside 'Configuration' file of protractor.
Such as, if I need to check my specs running on 'Windows / Mac' platform and a variable provides these details.
I am expecting something like :
exports.config = {
seleniumAddress : 'http://localhost:4444/wd/hub',
getPageTimeout : 30000,
allScriptsTimeout : 30000,
specs : [ ],
framework : 'jasmine2',
***don't know the syntax, am expecting below line and condition need to work for protractor***
***var platform = 'Windows',
if(platform ==='Windows'){***
multiCapabilities: [{
'browserName': 'chrome',
'specs': ['spec1.js']
},
***else {***
'browserName': 'chrome',
'specs': ['spec2.js']
}],
};
Is it possible to validate in Configuration file?
You need to use the getMultiCapabilities function:
getMultiCapabilities: function() {
// TODO: check platform and return list of capability objects
},

Why is Protractor complaining about target browser config when I'm not targeting a browser?

I'm writing an app using Ionic/Angular and testing it with Protractor. I'd like to test it using Sauselabs Appium so I can test the app running natively on the various target platforms. I'm launching Protractor with:
SAUCE_USERNAME=$(SAUCE_USERNAME) SAUCE_ACCESS_KEY=$(SAUCE_ACCESS_KEY) ./node_modules/.bin/protractor protractorConfig.js
But I get this error message:
[launcher] Running 1 instances of WebDriver
[launcher] Error: TypeError: Target browser must be a string, but is <object>; did you forget to call forBrowser()?
My protractorConfig.js looks like:
/* global exports */
/* global process */
exports.config = {
sauceUser: process.env.SAUCE_USERNAME,
sauceKey: process.env.SAUCE_ACCESS_KEY,
capabilities: {
appiumVersion: "1.0",
app: "sauce-storage:app.zip",
platformName: "iOS",
platformVersion: "7.1",
deviceName: "iPhone Simulator"
},
allScriptsTimeout: 30000,
specs: [
"spec/feature/*.js"
]
};
I should have read the docs more carefully. Appium's documentation for Desired Capabilities says that 'browserName' should be an empty string if you're not targeting a browser.
Updating my protractorConfig accordingly fixes the issue.
/* global exports */
/* global process */
exports.config = {
sauceUser: process.env.SAUCE_USERNAME,
sauceKey: process.env.SAUCE_ACCESS_KEY,
capabilities: {
appiumVersion: "1.0",
app: "sauce-storage:app.zip",
platformName: "iOS",
platformVersion: "7.1",
deviceName: "iPhone Simulator",
browserName: ""
},
allScriptsTimeout: 30000,
specs: [
"spec/feature/*.js"
]
};
Try adding of 'browserName': 'chrome' into your capabilities.

Resources