How to loop in Protractor config file? - angularjs

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
},

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

Running both Chrome and IE 11 Protractor tests

Running Protractor E2E tests against both Chrome and IE is difficult.
I can run them separately, however I need to start/stop the respective Chrome/IE webdriver server before running each tests.
In my conf.js file, I export the config options like this :
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: [
'spec/my-spec.js',
],
directConnect: false, // false when targeting IE, and selen addr is used
multiCapabilities: [
{
browserName: 'chrome'
}
,
{
browserName: 'internet explorer',
'version': '11'
}
]
}
For the standard Chrome tests, I can kick off the Webdriver server:
> webdriver-manager start
But for IE, I discovered a way to run Webdriver IE as follows (yeah, pretty ugly):
java -Dwebdriver.ie.driver=C:\Projects\GSDashboard-E2ETests\node_modules\protractor\node_modules\webdriver-manager\selenium\IEDriverServer_x64_2.53.1.exe -jar C:\Projects\GSDashboard-E2ETests\node_modules\protractor\node_modules\webdriver-manager\selenium\selenium-server-standalone-2.53.1.jar
Then I just launch the Protractor tests:
protractor protractor.conf.js
I'm searching around for a cleaner and smoother way of running both IE/Chrome e2e tests in one shot.
Is there a solution to this ?
Any advice/guidance is appreciated....
****** UPDATE ******
As per answer below, trying to use seleniumArgs as follows (where I can specify the jar file OR the IEDriverServer_x64_2.53.1.exe file :
exports.config = {
//seleniumAddress: 'http://localhost:4444/wd/hub', // comment out
seleniumArgs: ['-Dwebdriver.ie.driver=C:\Projects\Dashb-E2ETests\node_modules\protractor\node_modules\webdriver-manager\selenium\IEDriverServer_x64_2.53.1.exe'],
allScriptsTimeout: 50000,
specs: [
'spec/MY-spec.js',
],
directConnect: false, // false when targeting IE, and selen addr is used
multiCapabilities: [
//{
// browserName: 'chrome',
,
{
browserName: 'internet explorer',
'version': '11'
}
]
}
BUT running the test throws this error in a windows cmd prompt:
E/launcher - The path to the driver executable must be set by the web driver.ie.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/InternetExplorer‌​Driver.
So, I still haven't figured out how to run both IE and Chrome tests (sequentially).
regards,
Bob
I had a similar problem and the solution was running this:
webdriver-manager update --ie
After that, both IE11 and Chrome instances run simultaneously with:
multiCapabilities: [
{
browserName: 'chrome'
}
,
{
browserName: 'internet explorer',
'version': '11'
}
]
include seleniumArgs: ['-Dwebdriver.ie.driver=pathtoIEdriver/IEDriverServer.exe'] property in the conf.js and remove seleniumAddress: 'http://localhost:4444/wd/hub'.If no seleniumAddress is mentioned ,then protractor will automatically start selenium server. So following will be your conf.js for running your protractor test against chrome and ie.
exports.config = {
seleniumArgs: ['-Dwebdriver.ie.driver=node_modules/protractor/selenium/IEDriverServer.exe'],
specs: [
'spec/my-spec.js',
],
directConnect: false, // false when targeting IE, and selen addr is used
multiCapabilities: [
{
browserName: 'chrome'
}
,
{
browserName: 'internet explorer',
'version': '11'
}
]
}

how to run end to end test in Protractor?

I am new to angularjs ,trying to test my app with karma,jasmine and protractor.
here is my conf.js:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
'browserName': 'chrome'
},
specs: ['spec.js'],
jasmineNodeOpts: {
showColors: true,
}
};
here is my spec.js
describe('Protractor Demo App', function() {
it('should have a title', function() {
browser.get('http://localhost/First-angular-App/source/');
expect(browser.getTitle()).toEqual('Firstangular app');
});
});
after running protractor conf.js I get this:
but can't run my test how can i do this??
As noted above:
The error message tells exactly why your tests are not running. This option specifies a path to your specs: ['spec.js']. Modify conf.js and set a path to a folder with your specs relative to conf.js: specs: ['tests/spec.js'] or even better specs: ['tests/**/*.js'] (will take all files in tests/ directory as test files).
Upgrading my node version to LTS(Node Website), I fixed this problem.

Activating chrome language flags when activating from protractor (selenium)

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']}
}

how to get E2E Testing in Protractor results

I setup some tests in protractor for my angular app and i wanted to run a nightly batch and see next day some reports of the tests passed or failed .. . I tried without success protractor-html-screenshot-reporter so now i want to see how to save a log . Is this save a log file?
How i could see second day the result of the tests runner ?
My current protractor conf file :
exports.config = {
seleniumAddress: 'http://imptest2:80/wd/hub',
specs: [
'../E2E/**/*.spec.js'
],
multiCapabilities: [
{
'browserName' : 'chrome',
'ensureCleanSession': 'false',
'shardTestFiles': 'true',
maxInstances:8
}
],
params: {
global: {
url: 'http://impiis/TestsServices/Client',
concern: '01'
}
},
baseUrl: 'http://impiis/TestsServices/Client',
allScriptsTimeout: 500000
};
Thank you
With this simple protractor config file, you should have protractor-html-screenshot-reporter working
var HtmlReporter = require('protractor-html-screenshot-reporter');
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['Exova.LIMS.UI.Testing/e2e/pages/**/*_spec.js'],
baseUrl: 'http://localhost:51494',
onPrepare: function() {
// Add a screenshot reporter and store screenshots to `/tmp/screnshots`:
jasmine.getEnv().addReporter(new HtmlReporter({
baseDirectory: 'tmp/screnshots'
}));
}
};
It was a reporter bug it was solved : https://github.com/jintoppy/protractor-html-screenshot-reporter/pull/38

Resources