Protractor: Multiple browser instance tests fail where Single Instance tests pass - angularjs

I'm testing an Ionic application. When the app is run in the browser, the console does show that cordova.js is missing - which is right, but it doesn't break the application.
As long as I run my tests with a single browser instance, the missing files does not cause my tests to fail. Here is my configuration and spec files:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
framework: 'jasmine2',
suites: {
test: 'e2e/test.spec.js'
},
allScriptsTimeout: 60000,
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
binary: '/Applications/Google\ Chrome\ Stable.app/Contents/MacOS/Google\ Chrome',
args: [],
extensions: [],
}
},
onPrepare: function() {
browser.driver.get('http://localhost:8100');
}
};
Here is the test file:
describe('First test', function(){
it('just verifies that everything loads correctly', function() {
expect(1).toEqual(1);
});
});
The above works without errors. But opening a second browser instance causes a fail:
describe('First test', function(){
var browser2 = browser.forkNewDriverInstance();
browser2.get('http://localhost:8100');
var element2 = browser2.element;
it('just verifies that everything loads correctly', function() {
expect(1).toEqual(1);
});
});
With the error message:
/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/atoms/error.js:113
var template = new Error(this.message);
^
UnknownError: unknown error: cordova is not defined

Related

protractor instance does not do anything

from one to anothet day i cannot interact with the angular page i am testing.
The only result i get:
Started
A Jasmine spec timed out. Resetting the WebDriver Control Flow.
F
the page has this:
<html lang="sv" class="no-js" data-ng-app="app">
So it should work, or not?
I tried checking on iFrames and also enhanced the allScriptTimeout: 360000, but nothing helped.
here is my config:
var TIMEOUT = 360000;
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var reporter = new HtmlScreenshotReporter({
dest: 'report/screenshots',
filename: 'my-report.html'
});
exports.config = {
seleniumAddress: 'http://dmc-l01-ap08.dmc.de:4448/wd/hub',
baseUrl: 'https://int.system.com/',
specs: [
'./UseCases/protractorTestcaseNightly.js',
],
Capabilities: [
{
'browserName': 'chrome',
shardTestFiles: false,
maxInstances: 1,
maxSessions: -1,
restartBrowserBetweenTests: true,
},
],
getPageTimeout: TIMEOUT,
allScriptsTimeout: TIMEOUT,
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: TIMEOUT,
includeStackTrace : true,
isVerbose : true,
},
framework: "jasmine2",
onPrepare: function () {
browser.driver.manage().window().maximize();
browser.driver.manage().deleteAllCookies();
jasmine.getEnv().addReporter(reporter);
var reporters = require('jasmine-reporters');
var junitReporter = new reporters.JUnitXmlReporter({
savePath: 'report/xml',
consolidateAll: false,
filePrefix: 'xmloutput'
});
jasmine.getEnv().addReporter(junitReporter);
}
};
UPDATE:
I added
rootElement: 'html',
to my config. But nothing changed. I also took a screenshot from the page before clicking the first element. In the screenshot i can see the element i want to click at.
this is my Testspec:
describe('Start testspec | ', function () {
it('opens the start', function () {
browser.get("/de-de/connect/").then(function () {
console.log("1"); //works
browser.executeScript('localStorage.clear();sessionStorage.clear();'); //works (?)
console.log("2"); //works
expect(element(by.xpath('//*[#class="btn-link"]'))).toBeTruthy(); //works
console.log("3"); //works
browser.sleep(3000); //works
element(by.css('.ms-linklist-typ-1 a')).click(); //DOES NOT WORK :(
browser.sleep(3000); //works
});
});
});
I do have to click on the "btn-link" / css element, because this is the cookie layer, which has to be clicked first before i can reach any other element...
any ideas?
Looks like you are using the timeout configuration incorrectly. allScriptTimeout is only for the backend application scripts is not for the overall Jasmine spec
Please take a look at the three timeouts explained below
allScriptsTimeout - This timeout config indicates the time
application needs to finish running back-end scripts
getPageTimeout - This indicates the time to wait for Page
Load
jasmineNodeOpts.defaultTimeoutInterval - This indicates the
time that Jasmine waits for overall spec
So in your case you increase the jasmineNodeOpts.defaultTimeoutInterval to increase the timeout for overall spec
Try making the below change and see if this resolves the issue
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 120000,
includeStackTrace : true,
isVerbose : true,
},
Also, Protractor assumes your angular-app root element to be body. Please see the below extract from the documentation below. Since your ng-app resides on a different element , may be update the config.js accordingly
/** * CSS Selector for the element housing the angular app - this
defaults to * 'body', but is necessary if ng-app is on a descendant
of . */
rootElement?: string;
it looks like i found the fix. My tests are running again by changing the versions of my installation. I looks like there is incompatibility between some version. It now works with:
Node LTS (6.10.0)
Selenium Server Standalone 3.0.1
protractor#5.1.1
npm#4.1.2

protractor gulp run a single spec file

Here is my protractor conf file
exports.config = {
framework: 'jasmine',
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['../../e2e/smoke-test/*.spec.js'],
ignoreSynchronization: 'true',
jasmineNodeOpts: {
defaultTimeoutInterval: 2500000,
allScriptsTimeout: 25000000
}
};
Here is my gulp conf file
'use strict';
var path = require('path');
var gulp = require('gulp');
// Protractor configurations to open browser
var protractor = require("gulp-protractor").protractor;
var spawn = require('child_process').spawn;
var browserSync = require('browser-sync');
var $ = require('gulp-load-plugins')();
// Downloads the selenium webdriver
gulp.task('webdriver-update', $.protractor.webdriver_update);
gulp.task('webdriver-standalone', $.protractor.webdriver_standalone);
// Protractor with selenium configuration to open browser
//run webdriver method
function runWebdriver(callback) {
spawn('webdriver-manager', ['start'], {
stdio: 'inherit'
}).once('close', callback);
}
//run protractor configurations method
function runProtractorSeleniumConfig() {
gulp.src('./**/*-page.spec.js')
.pipe(protractor({
configFile: './e2e/conf/smoke-test-conf.js'
}))
.on('error', function (e) {
throw e;
});
}
//execute protractor.config after webdriver is executed
function runWebdriverProtractor(){
// runWebdriver(runWebdriver);
runWebdriver(runProtractorSeleniumConfig);
}
//put them into
gulp.task('e2e:smoke-test', runWebdriverProtractor);
// run on dist
//gulp.task('e2e:dist', ['serve:e2e-dist', 'webdriver-update'], runProtractor);
right now I defined one task for gulp which is
gulp e2e:smoke-test
I runs all specs under smoke-test what can I do if I want to run a single smoke spec file or if I want to run a single spec/test
I solved this by passing an individual file through gulp configuration.
function runProtractorSeleniumConfig(path) {
gulp.src(getFile(path))
.pipe(protractor({
configFile: 'ProtractorConfigFile'
}))
.on('error', function (e) {
throw e;
});
}
function getFile(ext){
if(ext == '--regression'){
return 'RegressionTestCaseFilePath'
}
else{
return 'TestCaseFilePath'
}
}
//execute protractor.config after webdriver is executed
function runWebdriverProtractor(str){
// runWebdriver(runWebdriver);
runWebdriver(runProtractorSeleniumConfig(str));
}
//run smoke test commands configurations
gulp.task('e2e:smoke-test', function(){
runWebdriverProtractor(process.argv[3])
});
now I can run regression/smokde file by simply using this
gulp e2e:smoke-test --smoke
cheers!!
With jasmine2 you can filter tests using a regular expression. Maybe you can add something like #smoke, #regressions to your tests and then only run those ones by passing the grep flag:
describe('Test Spec #smoke', function () {
...
})
and in protractor.conf.js:
exports.config = {
jasmineNodeOpts: {
grep: '#smoke',
}
};

Protractor: how to Force secuential execution of describe?

I'm using protractor to test an webapp, there is a lot of test_.js and it looks like all test are running at the same time. if I set just one test in the export.config.specs it works flawlessly but if I use wildcard or put 2 or more spects it open the browser and try to open all the routes at the same time and fails all test...
So is there a flag or something that I miss to force execute all describes one by one?
An excerpt of my conf file:
exports.config = {
multiCapabilities: [
{'browserName': 'chrome'}
],
seleniumAddress: 'http://localhost:4444/wd/hub',
params: {
domain: 'http://0.0.0.0:3000/'
},
specs: [
'specs/test_login.js',
//'specs/test_*.js'
]
};
an example of one of the many specs:
describe('homepage test', function() {
browser.get(browser.params.domain);
it('should check page title', function() {
return expect(browser.getTitle()).toEqual('The title');
});
});
The browser actions must be allwais within a Jasmin action like
it
before(All/Each)
after(All/Each)
Solution based on example:
describe('homepage test', function() {
beforeAll(function() {
browser.get(browser.params.domain);
});
it('should check page title', function() {
return expect(browser.getTitle()).toEqual('The title');
});
});
source: http://jasmine.github.io/2.4/introduction.html#section-Setup_and_Teardown
If you want all the describes to run sequentially in the same browser window, in exports.config, under multiCapabilities (or capabilities) add shardTestFiles: false
If you want all the describes to run one at a time in separate browser windows, in exports.config, add maxSessions: 1

Protractor-trx-reporter not creating trx file

I'm working with Protractor for the first time and I've installed some modules with NPM for protractor, protractor-trx-reporter, jasmine-trx-reporter, and webdriver-manager. Selenium server is running at the default 4444 port, and the tests seem to run fine when I run it locally through command line (opens a browser, test passes).
Everything seems to not give any errors, but I can't find the trx file published by the protractor-trx-reporter. When I run protractor conf.js, the test starts, and the command line output says that it's exporting the trx reporter and setting the output file to ProtractorTestResults.trx but a .trx file doesn't show up anywhere so I suspect it's not publishing a file but not throwing errors.
Any ideas if protractor-trx-reporter hasn't exported a trx file?
Here's what my config and spec files look like (both taken as samples from Protractor and protractor-trx-reporter sites)
//conf.js
exports.config = {
framework: 'jasmine',
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js'],
onPrepare: function () {
console.log('Adding TRX reporter');
require('protractor-trx-reporter');
jasmine.getEnv().addReporter(new jasmine.TrxReporter('ProtractorTestResults.trx'));
}
}
//spec.js
describe('angularjs homepage todo list', function() {
it('should add a todo', function() {
browser.get('https://angularjs.org');
element(by.model('todoList.todoText')).sendKeys('write first protractor test');
element(by.css('[value="add"]')).click();
var todoList = element.all(by.repeater('todo in todoList.todos'));
expect(todoList.count()).toEqual(3);
expect(todoList.get(2).getText()).toEqual('write first protractor test');
// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click();
var completedAmount = element.all(by.css('.done-true'));
expect(completedAmount.count()).toEqual(2);
});
});
I ended up using jasmine-trx-reporter, here's what the conf.js looked like:
// conf.js
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
directConnect: true,
specs: ['spec.js'],
capabilities: {
'browserName': 'chrome'
},
onPrepare: function () {
var trx = require('jasmine-trx-reporter');
return browser.getCapabilities().then(function (caps) {
var browserName = caps.get('browserName').toUpperCase();
var jasmineTrxConfig = {
reportName: 'Protractor Test Results',
folder: 'testResults',
outputFile: 'Test.trx',
browser: browserName,
groupSuitesIntoSingleFile: false
};
jasmine.getEnv().addReporter(new trx(jasmineTrxConfig));
});
}
};
I think this may be the answer:
"...short term we will not add support for Jasmine 2.0. Later on maybe
yes, but I can't promise you a date."
So that's upsetting. I'm having the same problem. And jasmine-trx-reporter isn't working for me either.
Source: [https://github.com/hatchteam/protractor-trx-reporter/issues/2]

Protractor - 'Async callback was not invoked' errors on Jenkins while passing locally

I'm consistently getting the following error when running Protractor end to end UI tests on Jenkins. (OS on the boxes are CentOS 6.7). Firefox browser is used. Xvfb server is started before the build.
Error: Timeout - Async callback was not invoked within timeout by jasmine.DEFAULT_TIMEOUT_INTERVAL.
The same tests consistently pass locally on Windows 10 machine with Firefox 38. I deliberately chose 10-15 already stabilized tests to fix this but no luck. From the logs it seems like the tests go through with every assertion successfully and hang after that.
I use 'done' param in every test and call it after assertion to finish the test. Here is an example of a single test in one of my 2 specs:
page.getWaitForRows(function () {
expect(browser.getCurrentUrl()).toContain(HISOTRY_PATH);
commons.verifyElementDisplayed(commonPage.getNewNumberMenuItem());
commonPage.clickNumberMenuItem().then(function () {
commonPage.clickOnOrderMenuLabel();
commons.waitForUrlToChangeTo(new RegExp(ORDER_MENU)).then(function () {
done();
});
});
});
The time of running is 100 seconds for most of them.
Here is most of my config file (ignoreSynchronization=true is used since synchronization fails for our Angular app because it uses $timeout):
exports.config = {
allScriptsTimeout: 30000,
getPageTimeout: 30000,
troubleshoot: true,
capabilities: {
'browserName': 'firefox'
},
baseUrl: 'http://admin.localhost:9002',
framework: 'jasmine2',
jasmineNodeOpts: {
defaultTimeoutInterval: 50000,
showColors: true
},
onPrepare: function () {
browser.ignoreSynchronization = true;
browser.driver.manage().window().maximize();
var disableNgAnimate = function () {
angular.module('disableNgAnimate', []).run(['$animate', function ($animate) {
$animate.enabled(false);
}]);
};
browser.addMockModule('disableNgAnimate', disableNgAnimate);
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
jasmine.getEnv().addReporter(new HtmlScreenshotReporter({
captureOnlyFailedSpecs: true,
filename: 'failure-report.html',
dest: '.test-results/html',
pathBuilder: function (currentSpec, suites, browserCapabilities) {
return 'chrome/' + currentSpec.fullName;
}
}));
var jasmineReporters = require('jasmine-reporters');
return browser.getProcessedConfig().then(function (config) {
var cap = config.capabilities;
var prePendStr = cap.browserName.toUpperCase() + '-';
if (cap.version) {
prePendStr += 'v' + cap.version + '-';
}
var junitReporter = new jasmineReporters.JUnitXmlReporter({
consolidateAll: false,
savePath: '.test-results/protractor',
modifySuiteName: function (generatedSuiteName, suite) {
return generatedSuiteName;
}
});
jasmine.getEnv().addReporter(junitReporter);
});
Please note that synchronization is enabled for some time later in the tests to do the log in, but after that it is once again set to false. Here is the beforeAll method for my tests:
beforeAll(function (done) {
commons.loginAndChooseAccount().then(function () {
browser.ignoreSynchronization = true;
done();
});
});
What I tried:
Connecting directly to Firefox using directConnect: true,
Updating protractor version to 3.1.1
Specifically setting
browser.ignoreSynchronization to "true" in each 'it' test or in
'beforeEach'
and a lot other stuff.
Nothing worked. Each 'it' in the 2 specs I run fail with this error. And there are consistently 0 failures locally for the same tests. I tried switching to Chrome but is does not officially support CentOS 6.7 so I'm stuck with Firefox and have been battling with this issue for more than a week now. Will appreciate any help greatly!

Resources