Protractor e2e test case for downloading csv file without prompt is not working with the following code. Please help me on this. I have gone through all the stuff already available in stackoverflow. Nothing worked for me...
var q = require("q");
var FirefoxProfile = require("firefox-profile");
var makeFirefoxProfile = function(preferenceMap, specs) {
var deferred = q.defer();
var firefoxProfile = new FirefoxProfile();
for (var key in preferenceMap) {
firefoxProfile.setPreference(key, preferenceMap[key]);
}
firefoxProfile.encoded(function (encodedProfile) {
var capabilities = {
browserName: "firefox",
acceptInsecureCerts: true,
"moz:webdriverClick": false,
firefox_profile: encodedProfile,
};
deferred.resolve(capabilities);
});
return deferred.promise;
};
exports.config = {
getMultiCapabilities: function() {
return q.all([
makeFirefoxProfile(
{
"browser.download.dir": "./",
"browser.helperApps.neverAsk.saveToDisk": "text/commaseparated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext"
},
)
]);
},
// NOTE: this will need changing if a new version of selenium
// standalone server is released --->
seleniumServerJar: 'node_modules/protractor/node_modules/webdriver-manager/selenium/selenium-server-standalone-3.5.0.jar',
specs: [
'test/e2e/app.e2e_spec.js',
'test/e2e/**/*.e2e_spec.js',
],
capabilities: {
browserName: 'firefox',
acceptInsecureCerts: true,
firefox_profile: makeFirefoxProfile,
"moz:webdriverClick": false
},
jasmineNodeOpts: {
defaultTimeoutInterval: 240000
},
baseUrl: 'https://localhost:3030/',
seleniumAddress: 'http://localhost:4444/wd/hub',
framework: 'jasmine'
};
With the above is my protractor.conf file not working, Prompt pop-up is coming for file download.
Do you have any ideas? Thanks.
Related
As Angular team plans to stop development of Protractor soon, I am looking for alternatives for our protractor test suite. As we don't really use a lot of protractor specific stuff, i just looking to remove the wrapper around webdriver, and just use webdriverjs instead. Any suggestions for a test runner that i can use to run the protractor config file, which looks something like this.
var { SpecReporter } = require('jasmine-spec-reporter');
exports.config = {
allScriptsTimeout: 40000,
specs: [
'./src/tests/*/*.e2e-spec.ts',
],
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
args: ['--headless', '--disable-gpu', '--window-size=2880,2000', '--no-sandbox', '--test-type=browser', '--disable-dev-shm-usage'],
// args: ['--no-sandbox', '--test-type=browser', '--window-size=2880,1800'],
// Set download path and avoid prompting for download even though
// this is already the default on Chrome but for completeness
prefs: {
'download': {
'prompt_for_download': false,
'directory_upgrade': true,
'default_directory': 'C:\\downloadtemp'
}
}
}
},
directConnect: true,
// Used for running against development server baseUrl: 'http://localhost:4200/',
// Used for running against an env - replaced now as jenkins job specifies the baseUrl as part of protractor command
framework: 'jasmine',
//to run the e2e tests,from the app root run the command ng e2e
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 240000,
print: function () {
}
},
onPrepare: function () {
//browser.driver.manage().window().maximize();
require('ts-node').register({
project: require('path').join(__dirname, './tsconfig.e2e.json')
});
//clear down the previous test run downloads
var fs = require('fs');
function rmDir(dirPath) {
try {
var files = fs.readdirSync(dirPath);
} catch (e) {
return;
}
if (files.length > 0)
for (var i = 0; i < files.length; i++) {
var filePath = dirPath + '/' + files[i];
if (fs.statSync(filePath).isFile())
fs.unlinkSync(filePath);
else rmDir(filePath);
}
fs.rmdirSync(dirPath);
}
rmDir('C:\\downloadtemp');
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: 'none' } }));
var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
consolidateAll: true,
savePath: 'testresults/xmltestresults',
filePrefix: 'xmloutput'
}));
var HtmlReporter = require('protractor-beautiful-reporter');
jasmine.getEnv().addReporter(new HtmlReporter({
baseDirectory: 'testresults/htmltestreport',
takeScreenShotsOnlyForFailedSpecs: true,
gatherBrowserLogs: true,
docTitle: 'Freecon Test Report',
preserveDirectory: false,
}).getJasmine2Reporter());
}
};
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://';
}
};
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.
I'm trying to mock the backend so that I can write some e2e tests.
I searched online but I can't figure out what i'm doing wrong.
I get this error on the first line of my code:
angular is not defined.
My protractor config file is:
exports.config = {
specs: ['tests/e2e/*.js'],
baseUrl: "http://localhost:8100/#/",
framework: 'jasmine',
allScriptsTimeout: 5000000,
capabilities: {
'browserName': 'chrome'
},
onPrepare: function () {
browser.driver.get(browser.baseUrl);
var SpecReporter = require('jasmine-spec-reporter');
jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'all'}));
},
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 300000,
includeStackTrace: true,
print: function () {
}
}
};
and the very start of my e2e test is:
'use strict';
describe('Page', function() {
var BY, test;
BY = by;
test = angular.module('hgApp', ['ionic', 'ngMockE2E']);
return test.run(function($httpBackend) {
return console.log($httpBackend);
});
});
What am I doing wrong?
Thanks for any help
EDIT
I also added browser.waitForAngular() before trying to do beforeEach module but still nothing, same error as before
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