Protractor: how to make the configuration file more flexible? - angularjs

I have an idea to make my configs more flexible. For example I have 10000 config files with same parameters:
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['C:/Users/Lilia.Sapurina/Desktop/Protractor Tests/Scenarios/ps-grid-column-filter-range_spec.js'],
params: {'url_filter': 'http://wks-15103:8010/ps/ng-components/examples/ps-grid-column-filter-range.html'}
And once I want to change path to specs, html or change selenium address. Can I do this in another file for all my configs?
For example write in my config:
seleniumAddress: '../Variables/seleniumAdress.txt'
Or maybe exist another interesting ways to solve this problem?

You can export your common config rules as node.js module:
// globalProtractor.conf.js
module.exports = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['C:/Users/Lilia.Sapurina/Desktop/Protractor Tests/Scenarios/ps-grid-column-filter-range_spec.js'],
params: {
'url_filter': 'http://wks-15103:8010/ps/ng-components/examples/ps-grid-column-filter-range.html'
}
And use in another file
// protractor.conf.js
var globalConf = require('/path/to/globalProtractor.conf.js');
globalConf.specs.push('path/new.spec.js');
exports.config = globalConf;

With #driver_by help I found nice solution for my problem. Now my files isolated. If I want to change url or path to folder I should change only global config.
// globalProtractor.conf.js
module.exports = {
seleniumAddress: 'http://localhost:4444/wd/hub',
baseUrl: 'http://wks-15103:8010/ps/ng-components/examples/',
specs: [],
path_to_scenario: '../Scenarios/',
path_to_spec: '../Specs/',
path_to_lib: '../Lib/'
}
And another file:
// protractor.conf.js
var globalConf = require('../Global_configs/globalProtractor_conf.js');
globalConf.specs.push(path_to_spec + 'ps-grid-column-filter-range_spec.js');
globalConf.params = {'url_filter': 'ps-grid-column-filter-range.html',
'column_number_filter': 5,
'req_lib_filter': globalConf.path_to_lib + 'jasmine-expect'}
exports.config = globalConf;

Related

ngMockE2E does not work with remote Selenium server

Setup
We use protractor, protractor-cucumber, and angular-mocks to handle our e2e tests. Builds are managed by grunt.
We have two build configs; pool and local. pool is the configuration that is picked up by our CI environment, local is for debugging purposes and is setup to run on a local machine. The primary difference between the two setups is that local runs everything locally, whereas pool uses a remote Selenium server (outside the CI server itself).
protractor.conf.js:
exports.config = {
params: {
widgetUrl: 'http://localhost:9980',
vudUrl: 'xxx',
testDelay: 3000,
},
// The timeout for each script run on the browser. This should be longer
// than the maximum time your application needs to stabilize between tasks.
allScriptsTimeout: 150000,
// A base URL for your application under test. Calls to protractor.get()
// with relative paths will be prepended with this.
baseUrl: 'http://localhost:' + (process.env.PORT || '9082'),
// list of files / patterns to load in the browser
specs: ['./e2e/features/*.feature'],
cucumberOpts: {
require: ['./e2e/steps/*.js', './e2e/pageObjects/*.js', './e2e/support/*.js'],
format: 'pretty',
tags: ['#context-test']
},
// Patterns to exclude.
exclude: [],
// ----- Capabilities to be passed to the webdriver instance ----
//
// For a full list of available capabilities, see
// https://code.google.com/p/selenium/wiki/DesiredCapabilities
// and
// https://code.google.com/p/selenium/source/browse/javascript/webdriver/capabilities.js
capabilities: {
browserName: 'chrome',
loggingPrefs: {
'driver': 'INFO',
'server': 'INFO',
'browser': 'INFO'
}
},
// ----- The test framework -----
//
// Jasmine and Cucumber are fully supported as a test and assertion framework.
// Mocha has limited beta support. You will need to include your own
// assertion framework if working with mocha.
framework: 'custom',
frameworkPath: 'node_modules/protractor-cucumber-framework',
mocks: {
dir: "mocks", // path to directory with mocks
default: []
},
onPrepare: function() {
// Chai config
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
global.expect = chai.expect;
console.log('params: ' + JSON.stringify(browser.params));
//browser.driver.manage().window().maximize();
}
};
We configure our mocks in a Before hook (snipped here for brevity):
this.Before(function(scenario, callback) {
// ...
let data = require(`${TEST_DATA_DIR}/default.js`);
let httpMocker = function() {
angular.module('httpMocker', ['ngMockE2E'])
.run(function($httpBackend) {
$httpBackend.whenPOST(...);
};
browser.addMockModule('httpMocker', httpMocker, {data})
// ...
callback();
});
Problem
Despite identical test setups, ngMockE2E is not being called when run in the CI environment. This can be demonstrated by the following test:
test.feature:
#Test
Feature: Test and debug
#context-test
Scenario: Get console output
Given I access the page
Then I get the log output
test.steps.js
module.exports = function() {
this.Given(/^I access the page$/, () => {
util.waitAndDo(() => true);
});
this.Then(/^I get the log output$/, () => {
util.waitAndDo(() => {
browser.manage().logs().get('browser').then(function(browserLog) {
console.log('log: ' + require('util').inspect(browserLog));
});
})
});
};
This test will dump the browser log rather than actually testing anything. When it is run locally, the logs are empty. However, when the same test is run in the CI environment, the logs show errors for failed calls to the URLs being mocked.
We have verified that the URLs used in the CI environment correctly match the regex in our mocks, so it is not a match-miss. The module is simply not being called.
To reiterate - the only major difference in configuration is that the pool config makes use of a remote Selenium hub. How could this affect how our tests are run, and why would it prevent our mocks from working?

AngularJs Protractor Html Reporter Fails Config Parser

I am trying to use protractor html screenshot reporter but it's failing to require in the the config file. Here's my config:
//Configuration for Protractor
var HtmlReporter = require('protractor-html-screenshot-reporter');
var reporter = new HtmlReporter({
baseDirectory: '/screenshots',
takeScreeenShotOnlyForFailedSpecs: true
});
exports.config = {
//Address of Selenium server
seleniumAddress: 'http://localhost:4444/wd/hub',
//framework: 'jasmine2',
chromeOnly: false,
multiCapabilities: [
{'browserName': 'chrome'}
],
//URL of the application
baseUrl: 'http://localhost:80',
cache: false,
//Spec pattern
specs: ['**/specs/*-spec.js'],
onPrepare: function () {
//browser.driver.manage().window().maximize();
browser.driver.manage().window().setSize(480, 800);
browser.driver.manage().window().setPosition(400, 100);
jasmine.getEnv().addReporter(reporter);
},
//Options passed to Jasmine-node
jasmineNodeOpts: {
includeStackTrace: true,
showColors: true,
defaultTimeoutInterval: 30000,
isVerbose: true,
}
};
I have both protractor and the protractor html screenshot report installed globally . Here's the error protractor is throwing:
[13:48:23] E/configParser - error code: 105
[13:48:23] E/configParser - description: failed loading configuration file protractor-config.js
Any help would be most helpful!!
UPDATE:
I moved to the require call inside the onPrepare in the config and I getting the proper error:
[14:27:15] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
[14:27:15] I/launcher - Running 1 instances of WebDriver
[14:27:18] E/launcher - Error: Error: Cannot find module 'protractor-html-screenshot-reporter'
Seems like it can't find the node module. Anyone else seeing this issue?
UPDATE2:
So, I think I found my problem. I was installing like this:
npm install -g protractor-html-screenshot-reporter
Which seems to be the cause the fail. Installing with this command:
npm install protractor-html-screenshot-reporter --save-dev
Works! For whatever reason installing globally doesn't work but installing locally does, which is a bit of a downer :( If anyone knows how to get this to work with the screenshot reporter installed globally let me know how :)

Protractor not working in google chrome

I am new to protractor testing tool in angular js, I installed CORS extension
in chrome browser, I wrote my first test-case, I try to run this using
'protractor config.js' chrome browser is getting open, actually it is login form, i entered correct username and password but it is not connecting to server, I understood that CORS extension was disabled, how to enable cors extension when i am running test case through cmd.
my config.js
exports.config = {
capabilities: {
// You can use other browsers
// like firefox, phantoms, safari, IE (-_-)
'browserName': 'chrome'
},
specs: [
// We are going to make this file in a minute
'../www/modules/user/login.spec.js',
//'../www/modules/lead/list.spec.js',
],
jasmineNodeOpts: {
showColors: true,
//defaultTimeoutInterval: 30000,
isVerbose: true,
},
allScriptsTimeout: 20000,
onPrepare: function(){
browser.driver.get('http://localhost:8100/');
}
};
By default, a completely clean browser is fired up with no extensions installed.
In order to start Chrome with an extension, you need to set the chromeOptions and the extensions appropriately. Here you can find working samples:
Is it possible to add a plugin to chromedriver under a protractor test?
just change capabilities section. or u can follow alexe answer.
capabilities: {
// You can use other browsers
// like firefox, phantoms, safari, IE (-_-)
'browserName': 'chrome',
chromeOptions': { 'args': ['--disable-web-security'] }
},

protractor "ERROR - Unable to start a WebDriver session"

I have some it tests on my single-page-app written in angular.
the tests are in protractor.
they ran during the build before, but now that I moved all of them to a branch something got broken and when I run the tests I'm getting:
Running "protractor:normal" (protractor) task
Starting selenium standalone server...
[launcher] Running 1 instances of WebDriver
Selenium standalone server started at http://172.31.9.226:23730/wd/hub
ERROR - Unable to start a WebDriver session.
c:\projects\blog-manager\node_modules\protractor\node_modules\selenium- webdriver\lib\atoms\error.js:113
var template = new Error(this.message);
^
Warning: Protractor test(s) failed. Exit code: 1 Use --force to continue.
Aborted due to warnings.
Process finished with exit code 6
the conf file is as follows:
'use strict';
module.exports.config = {
allScriptsTimeout: 60000,
baseUrl: 'http://localhost:9000/',
specs: [
process.cwd() + '/test/spec/e2e/**/*.js'//,
// process.cwd() + '/test/e2e/spec/**/*.js'
],
framework: 'jasmine',
capabilities: {
browserName: 'chrome',
"chromeOptions": {
binary: "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe",
args: [],
extensions: [],
}
},
onPrepare: function () {
// Disable animations so e2e tests run more quickly
var disableNgAnimate = function () {
angular.module('disableNgAnimate', []).run(function ($animate) {
$animate.enabled(false);
});
};
browser.addMockModule('disableNgAnimate', disableNgAnimate);
// Store the name of the browser that's currently being used.
browser.getCapabilities().then(function (caps) {
browser.params.browser = caps.get('browserName');
});
},
jasmineNodeOpts: {
defaultTimeoutInterval: 300000
}
};
any suggestions on how to solve this would be much appreciated.
Thanks!
According to alecxe's answer it's chrome binary issue which can be solved by installing chrome in same place as chromedriver expects to be or specifying the executable path to the binary settings.
capabilities: {
"browserName": "chrome",
"chromeOptions": {
binary: "D:/Program Files/Chrome/chrome.exe",
args: [],
extensions: [],
}
},
See his answer here
Even though you are setting the executable path correctly it seems little off to me binary: "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe",
Is this correct?
Alternatively, I would try this

Setting Protractor baseUrl dynamically

In our end-to-end tests, we dynamically generate the URL endpoint for us to test against. In our Protractor configuration, I was hoping to use beforeLaunch or onPrepare to retrieve the URL endpoint and set it to baseUrl before running our tests. However it seems no matter what I try, Protractor runs tests with the wrong baseUrl, not the one I set in beforeLaunch or onPrepare.
We're using Protractor version 1.4.0. This is a simple configuration file demonstrating the issue:
exports.config =
directConnect: true
framework: 'jasmine'
jasmineNodeOpts:
isVerbose: true
showColors: true
includeStackTrace: true
suites:
login: 'login/**/*.coffee'
full: '**/*.coffee'
capabilities:
browserName: 'chrome'
beforeLaunch: ->
setBaseUrl 'https://test-url.com'
onPrepare: ->
setBaseUrl 'https://test-url.com'
setBaseUrl = (baseUrl) ->
exports.config.baseUrl = baseUrl
Thanks for the help!
It looks like calling browser.baseUrl = "https://test-url.com" does the trick in onPrepare

Resources