Protractor config file#
What is exports.config in protractor?
Do you say this: exports.config = { ... }
Is used for globals variables and configuation of your test.
For example:
exports.config = {
seleniumAddress: "http://127.0.0.1:4444/wd/hub",
baseUrl: "localhost:8080"
}]
More: https://github.com/angular/protractor/blob/master/docs/tutorial.md
exports.config in Protractor is used to specify configuration in conf.js like below:
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://localhost:4444/wd/hub',
// define browser capabilities
capabilities: {
browserName: 'chrome',
chromeOptions: {
args: [
'--disable-extensions',
]
}
},
// Define which tests should execute.
specs: ['test-spec.js']
// set log level
logLevel: 'verbose',
// Enables colors for log output
coloredLogs: true,
};
Default framework used is Jasmine, to run tests use below command
protractor conf.js
export.config{.....}
It is the place where we define /configure framework details, selenium details, Specs,Script timeouts, Onprepare functions, capabilities,reports and all generic stuffs.
As soon Run starts, protractor looks for this file first and tries to executes the stuffs here.
for eg) initiating selenium web-driver instances,opening Browser etc.
EG)
exports.config = {
framework: 'jasmine2', //framework Used
seleniumPort: 4444, // selenium port address
//seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['./Spec/Master.spec.js'], /*Spec -> consists of test suite/ test cases */
allScriptsTimeout: 50000,
jasmineNodeOpts: { //jasmine framework details
isVerbose: true,
showColors: true,
includeStackTrace: true,
defaultTimeoutInterval: 300000,
print: function() {}
},
capabilities: //Browser details against which test runs
{
'browserName' :'chrome',
'chromeOptions' : {
'args':['incognito','--start-maximized'], /*this line is for maximize the window and incognito view */
prefs: {
'profile:managed_default_content_settings.notifications': 1
}
}
},
//before starting the
actual TC execution, setup the things we define here
onPrepare: function (config_) {
require('./Data/waitReady.js');
//browser.manage().window().maximize();
var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
consolidateAll: false,
savePath: './Reports/JunitXMLprotractor-result/',
filePrefix: 'xmloutput'
}));
jasmine.getEnv().addReporter(
new Jasmine2HtmlReporter({
savePath: './Reports/Screenshots/',
takeScreenshots: true,
takeScreenshotsOnlyOnFailures: true,
consolidateAll: true,
showPassed: true,
// filePrefix: sessionId + 'AutomationReport',
filePrefix: 'AutomationReport',
cleanDestination: true,
})
);
global.isAngularSite = function(flag){
browser.driver.ignoreSynchronization = !flag;
};
Related
I am trying to launch some tests with protractor going to SauceLabs.
I have my SauceConnect up and running. I have my protractor.config.js setup correctly I believe, but when I run the tests on my machine it with ng e2e --suite smoke, it is just running on my local machine and not going through the tunnel. Any suggestions? I have been following this "tutorial" and it has been going pretty well, but I am just not seeing anything going through the tunnel.
Here is my protractor.config.js file:
const baseUrl = '<BASEURL>';
const maxNumberOfInstances = process.env.NUMBER_OF_INSTANCES ? process.env.NUMBER_OF_INSTANCES : 1;
const reportPath = 'protractor/report';
const HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
const screenShotReporter = new HtmlScreenshotReporter({
dest: reportPath,
filename: 'artemis-e2e-report.html'
});
const SAUCELABS_USERNAME = '<SAUCEUSERNAME';
const SAUCELABS_AUTHKEY = '<SAUCEKEY>';
const chromeArgs = process.env.IS_LOCAL ? ['--no-sandbox', '--test-type=browser', '--lang=en', '--window-size=1680,1050'] : ['--disable-gpu', '--no-sandbox', '--test-type=browser', '--lang=en', '--window-size=1680,1050'];
const browserCapabilities = [{
sauceUser: SAUCELABS_USERNAME,
sauceKey: SAUCELABS_AUTHKEY,
browserName: 'chrome',
tunnelIdentifier: '<SAUCETUNNEL>',
shardTestFiles: true,
maxInstances: maxNumberOfInstances,
platform: 'Windows 10',
version: '73.0',
screenResolution: '1280x1024',
chromeOptions: {
args: chromeArgs,
prefs: {
'credentials_enable_service': false,
'profile': {
'password_manager_enabled': false
},
download: {
prompt_for_download: false,
directory_upgrade: true,
default_directory: 'C:\\downloads\\'
},
},
},
loggingPrefs: {
browser: 'SEVERE'
},
}, ];
// Protractor config
exports.config = {
baseUrl: baseUrl,
directConnect: true,
allScriptsTimeout: 2 * 60 * 1000,
jasmineNodeOpts: {
defaultTimeoutInterval: 3 * 60 * 1000
},
getPageTimeout: 2 * 60 * 1000,
suites: {
smoke: 'protractor/smokeTests/*.scenario.ts',
},
multiCapabilities: browserCapabilities,
framework: 'jasmine2',
onPrepare: function () {
browser.waitForAngularEnabled(true);
require('ts-node').register({
project: 'protractor/tsconfig.json',
});
const jasmineReporters = require('jasmine-reporters');
const jUnitXMLReporter = new jasmineReporters.JUnitXmlReporter({
consolidateAll: false,
savePath: reportPath,
filePrefix: 'xmloutput'
});
const JasmineConsoleReporter = require('jasmine-console-reporter');
const consoleReporter = new JasmineConsoleReporter({
colors: 1,
cleanStack: 1,
verbosity: 4,
listStyle: 'indent',
activity: true,
emoji: true,
beep: true,
timeThreshold: {
ok: 10000,
warn: 15000,
ouch: 30000,
}
});
jasmine.getEnv().addReporter(jUnitXMLReporter);
jasmine.getEnv().addReporter(screenShotReporter);
jasmine.getEnv().addReporter(consoleReporter);
browser.get(browser.baseUrl);
},
beforeLaunch: function () {
return new Promise(function (resolve) {
screenShotReporter.beforeLaunch(resolve);
});
},
afterLaunch: function (exitCode) {
return new Promise(function (resolve) {
screenShotReporter.afterLaunch(resolve.bind(this, exitCode));
});
},
};
First of all you are mentioning this
it is just running on my local machine and not going through the tunnel. Any suggestions
This is not related to the tunnel, but related to:
You still have directConnect: true,, remove it from your config
You added the Sauce Labs credentials to your capabilities, but you should use them in your config file at the root level. Here's and example (it's written for TypeScript, but it should give you an idea about how to set up your config file). The tunnel identifier is correct, you only need to be sure that you are getting the correct tunnel id as #fijiaaron mentioned
Hope this helps
Where are you getting your tunnelIdentifier from?
You want to make sure:
The tunnel is running
You can access the tunnel from where you are testing
If you have a named tunnel (e.g. sc -i myTunnel) then "myTunnel" should be the tunnelIdentifier, not the tunnel id that is shown in the console outnot (i.e. not Tunnel ID: cdceac0e33db4d5fa44093e191dfdfb0)
If you have an unnamed tunnel then you should not need to specify a tunnelIdentifier for it to be used.
If you appear to be using the tunnel but cannot access your local environment, try a manual test session in Sauce Labs and select the tunnel to see if it works there.
I have a config file as below:
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
exports.config = {
directConnect: true,
onPrepare: function() {
jasmine.getEnv().addReporter(
new Jasmine2HtmlReporter({
savePath: './test/reports/',
screenshotsFolder: 'images',
})
);
},
multiCapabilities: [
{
'browserName': 'chrome',
}],
framework: 'jasmine',
specs: ['zoo_spec.js'],
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
}
};
My question is is there any code i can write in order to send the report generated from jasmine2-html-reporter as soon as test execution is complete.
Did you try using onComplete instead? Please refer below previous answer
Unable to send Email using nodemailer in protractor.conf.js onComplete: function()
In the config onComplete:function(){} you can write custom code to send email. There are many nodejs email clients that you can reuse to send emails without reinventing. Here is the example client you can use https://nodemailer.com/about/ and also review SMTP section to for configuration;
i have tried to use Jasmine HTML reporter along with the Protractor HTML reporter.
My Config.js code look like this
var HtmlReporter = require('protractor-html-screenshot-reporter');
jasmine.getEnv().addReporter(new HtmlReporter({
baseDirectory: '/protractor-result',
docTitle: 'Protractor Demo Reporter',
docName: 'protractor-demo-tests-report.html'
}));
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
jasmine.getEnv().addReporter(new Jasmine2HtmlReporter({
savePath: '../Report/HTML/',
showSummary: true,
showQuickLinks: true,
showConfiguration: true,
screenshotsFolder: 'images',
takeScreenshots: true,
takeScreenshotsOnlyOnFailures: true,
fixedScreenshotName: true,
ignoreSkippedSpecs: true,
consolidate: true,
consolidateAll: true,
preserveDirectory: true,
reportTitle: 'Protractor-Execution-Report-' + timeStamp
}));
i could able to see HTML reports generated by Jasmine but still unable to see the HTML reports generated by Protractor.Please correct me if i am trying something unreal.
Any help would be appreciated.
protractor-html-screenshot-reporter is not compatible with jasmine 2 and latest version of protractor uses jasmine 2 by default, instead switch to
protractor-jasmine2-html-reporter
You need to put those lines inside your onPrepare section of the protractor.config.js file:
var HtmlReporter = require('protractor-html-screenshot-reporter');
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
exports.config = {
//other stuff
onPrepare: function() {
// Add a screenshot reporter and store screenshots to `/tmp/screnshots`:
jasmine.getEnv().addReporter(new HtmlReporter({
baseDirectory: '/tmp/screenshots'
//...
}));
jasmine.getEnv().addReporter(new Jasmine2HtmlReporter({
savePath: '../Report/HTML/'
//...
}));
}
}
I know this is pretty old, but I ran into an issue trying to get jasmine-reporters and protractor-jasmine2-html-reporter to work and didn't like the idea that I had to downgrade in order for them to work. I found out that protractor-jasmine2-html-reporter wasn't resolving the savePath correctly and was actually putting the reports folder output in protractor-jasmine2-html-reporter directory instead of the root directory of where I ran gulp. In order to make this work correctly I ended up using process.env.INIT_CWD to get the initial Current Working Directory which should be the directory where you ran gulp. Hope this helps someone.
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
var jasmine2HtmlReporter = new Jasmine2HtmlReporter({
savePath: process.env.INIT_CWD + '/report/e2e/',
screenshotsFolder: 'images',
takeScreenshots: true,
takeScreenshotsOnlyOnFailures: true,
fileName: 'index.html'
});
onPrepare: function () {
jasmine.getEnv().addReporter(jasmine2HtmlReporter);
}
I use protractor-jasmine2-screenshot-reporter and protractor-jasmine2-html-reporter to handle that, here is my configuration file protractor.conf.js, hope it can help you.
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var jasmine2HtmlReporter = new Jasmine2HtmlReporter({
savePath: 'report/e2e/protractor-jasmine2-html-reporter/',
filePrefix: 'index',
screenshotsFolder: 'screenshots'
});
var htmlScreenshotReporter = new HtmlScreenshotReporter({
dest: 'report/e2e/protractor-jasmine2-screenshot-reporter/',
filename: 'index.html'
});
exports.config = {
allScriptsTimeout: 11000,
specs: [
'*.e2e.js'
],
capabilities: {
'browserName': 'chrome'
},
baseUrl: 'http://localhost:8080/app/',
framework: 'jasmine',
jasmineNodeOpts: {
defaultTimeoutInterval: 30000,
showColors: true
},
// Setup the report before any tests start
beforeLaunch: function() {
return new Promise(function(resolve){
htmlScreenshotReporter.beforeLaunch(resolve);
});
},
// Assign the test reporter to each running instance
onPrepare: function () {
jasmine.getEnv().addReporter(jasmine2HtmlReporter);
jasmine.getEnv().addReporter(htmlScreenshotReporter);
},
// Close the report after all tests finish
afterLaunch: function(exitCode) {
return new Promise(function(resolve){
htmlScreenshotReporter.afterLaunch(resolve.bind(this, exitCode));
});
}
};
For details you see here
I am running my Protractor Test from command npm run e2e
I want a way so that if I pass npm run e2e firefox then my test will get executed on Firefox browser.
Or if I run npm run e2e chrome then it should run on chrome
if I pass both npm run e2e firefox chrome then my test should run on both the browser in parallel.
Is it possible to parametrize protractor config file?
Similarly if I can pass test suite name via command and it should execute only tests under that particular test suite.
Here is my config file and this is what I want to achieve:
`//var HtmlReporter = require('protractor-html-screenshot-reporter');
exports.config = {
allScriptsTimeout: 30000,
//Add parameters for browser names
params:{
pass: {
browserName : 'chrome',
testSuitName : 'e2e/TestSuites/_BVT/*.js',
}
},
suites: {
//Define here List of Sanity Test Scenarios:
BVT : testSuitName,
},
// configure multiple browsers to run tests
multiCapabilities: [
shardTestFiles: true,
maxInstances: 2
{'browserName': browserName}
],
baseUrl: 'http://mytestUrl/',
framework: 'jasmine2',
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
},
onPrepare: function() {
var jasmineReporters = require('jasmine-reporters');
browser.driver.manage().window().maximize();
return browser.getProcessedConfig().then(function(config) {
var browserName = config.capabilities.browserName;
var junitReporter = new jasmineReporters.JUnitXmlReporter({
consolidateAll: true,
savePath: 'tests/test-results',
filePrefix: browserName + '-xmloutput',
modifySuiteName: function(generatedSuiteName, suite) {
return browserName + '.' + generatedSuiteName;
}
});
jasmine.getEnv().addReporter(junitReporter);
});
},
resultJsonOutputFile: 'tests/test-results/output.json'
};`
Would appreciate any help on this.
Thanks in advance.
I know this post is a bit old now, but this solution may help people having a similar problem.
Use multiple config files, one for each browser type, setup a base config file, and require that into each of the other config files, and then create an npm script for each config file. No need for parameters and everything stacks nicely.
so the base config (called something like "protractor_base.js")would look something like:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
rootElement: '[ng-app]',
allScriptsTimeout: 60000,
framework: 'jasmine',
specs: ['example-spec.js']
};
And then your other configs (ie "protractor_chrome.conf.js") would look something like:
protractor = require('./protractor_base.js');
var config = protractor.config;
config.capabilities: {
'browserName': 'chrome'
};
exports.config = config;
You can then specify a multi browser config, just a chrome one, etc.
--capabilities.chromeOptions.args=headless --capabilities.chromeOptions.args=disable-gpu --capabilities.chromeOptions.args=window-size=1200,600
The above code should work for you.
I was looking for passing parameters from command line to protractor config file and found a way to do this:
npm run e2e -- --baseUrl=http://testurl:8080 --suite=suite_name_defined_in_config --capabilities.browserName=browser_Name
where my npm package.json :
"e2e": "protractor tests/protractor-conf.js",
and config file contains :
suites: {
BVT: 'e2e/TestSuites/_BVT/*.js',
Full: 'e2e/TestSuites/Full/**/*.js',
},
capabilities: {
'browserName': 'chrome'
},
baseUrl: 'http://localhost:8080/',
I've the following config.js file:
var testName = 'Testing';
var HtmlReporter = require('protractor-html-screenshot-reporter');
var reporter = new HtmlReporter({
baseDirectory: './protractor-result', // a location to store screen shots.
docTitle: 'Report Test Summary',
docName: 'protractor-tests-report.html'
});
exports.config = {
seleniumAddress: 'http://hub.browserstack.com/wd/hub',
multiCapabilities: [
{
name: testName,
browserName: 'Chrome',
browser_version: '39.0',
os: 'OS X',
os_version: 'Yosemite',
resolution: '1920x1080',
'browserstack.user': browserstackUser,
'browserstack.key': browserstackKey,
'browserstack.debug': 'true',
'browserstack.selenium_version': '2.45.0'
}
,
{
name: testName,
browserName: 'IE',
browser_version: '11.0',
os: 'Windows',
os_version: '8.1',
resolution: '2048x1536',
'browserstack.user': browserstackUser,
'browserstack.key': browserstackKey,
'browserstack.debug': 'true',
'browserstack.selenium_version': '2.45.0',
'browserstack.ie.driver': '2.44',
//ignoreProtectedModeSettings: true
}
],
// Spec patterns are relative to the current working directly when
// protractor is called.
suites: {
waitlist: './././specs/waitlist_page_spec.js',
press: './././specs/press_page_spec.js',
news: './././specs/news_page_spec.js',
landing: './././specs/landing_page_spec.js'
},
// Maximum number of total browser sessions to run. Tests are queued in
// sequence if number of browser sessions is limited by this parameter.
// Use a number less than 1 to denote unlimited. Default is unlimited.
maxSessions: 2,
// protractor will save the test output in json format at this path.
// The path is relative to the location of this config.
resultJsonOutputFile: null,
framework: 'jasmine2',
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 100000,
realtimeFailure: true,
showTiming: true,
includeStackTrace: true,
isVerbose: true,
onComplete: null
},
onPrepare: function () {
jasmine.getEnv().addReporter(reporter);
browser.driver.manage().window().maximize();
global.dvr = browser.driver; //variable to call selenium directly
global.isAngularSite = function (flag) {
browser.ignoreSynchronization = !flag; //This setup is to configure when testing non-angular pages
};
//browser.manage().timeouts().pageLoadTimeout(90000);
browser.manage().timeouts().implicitlyWait(100000);
}
};
And I would like to find a way to ask on my test that if the capability.browserName is IE do a certain/especial action, so, I would like to do some sort of getConfig(), is that possible? does anyone had implemented something similar?
Thanks all for your time!
The getCapabilities in browser returns a promise with these values:
browser.getCapabilities().then(function (capabilities) {
browser = capabilities.caps_.browserName;
platform = capabilities.caps_.platform;
}).then(function displayEnv() {
console.log('Browser:', browser, 'on platform', platform);
});