Related
I am trying to get Nightwatch's inbuilt parallel test_workers working with browserstack-local for local url testing.
When running tests without browserstack local, Nightwatch test_workers seem to work just fine. The same is true for running local tests without test_workers enabled.
I have tried the examples found here https://github.com/browserstack/nightwatch-browserstack but none of these uses browserstack-local in combination with Nightwatch test_workers.
When running locally with test_workers I get the following output.
Connecting local
Connected. Now testing...
Process terminated with code 0.
Has anyone else encountered similar issues?
EDIT: I have since resolved this issue and have posted an answer below
I have dumped the relevant configuration files below.
My local.conf.js
nightwatch_config = {
globals_path: 'globals.js',
output_folder: false,
src_folders: ['tests'],
selenium: {
'start_process': false,
'host': 'hub-cloud.browserstack.com',
'port': 80
},
test_workers: {
"enabled": true,
"workers":2
},
test_settings: {
default: {
desiredCapabilities: {
'browserstack.user': process.env.BROWSERSTACK_USER,
'browserstack.key': process.env.BROWSERSTACK_ACCESS_KEY,
'browserstack.local': true,
'browserstack.debug': false,
}
}
}
};
// Code to copy seleniumhost/port into test settings
for (let i in nightwatch_config.test_settings) {
if (nightwatch_config.test_settings.hasOwnProperty(i)) {
let config = nightwatch_config.test_settings[i];
config['selenium_host'] = nightwatch_config.selenium.host;
config['selenium_port'] = nightwatch_config.selenium.port;
}
}
module.exports = nightwatch_config;
local.runner.js
#!/usr/bin/env node
var Nightwatch = require('nightwatch');
var browserstack = require('browserstack-local');
var bs_local;
process.env.BROWSERSTACK_ID = new Date().getTime();
try {
process.mainModule.filename = "./node_modules/.bin/nightwatch";
// Code to start browserstack local before start of test
console.log("Connecting local");
Nightwatch.bs_local = bs_local = new browserstack.Local();
bs_local.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function (error) {
if (error) throw error;
console.log('Connected. Now testing...');
Nightwatch.cli(function (argv) {
Nightwatch.CliRunner(argv)
.setup(null, function () {
// Code to stop browserstack local after end of parallel test
bs_local.stop(function () { });
})
.runTests(function () {
// Code to stop browserstack local after end of single test
bs_local.stop(function () { });
});
});
});
} catch (ex) {
console.log('There was an error while starting the test runner:\n\n');
process.stderr.write(ex.stack + '\n');
process.exit(2);
}
and my package.json script
node ./local.runner.js -c ./local.conf.js
The issue here was due to the module filename being incorrectly defined in the local.runner.js
process.mainModule.filename = "./node_modules/.bin/nightwatch";
should be pointed directly at the Nightwatch file in its directory.
process.mainModule.filename = "./node_modules/nightwatch/bin/nightwatch";
The difference in these files and the exact reason for this solution working are beyond me.
The answer was derived from the "suite" runner in https://github.com/browserstack/nightwatch-browserstack
It seems you are not specifying browsers. Modify your configuration file to be inline with the following configuration file:
var browserstack = require('browserstack-local');
nightwatch_config = {
src_folders : [ "local" ],
selenium : {
"start_process" : false,
"host" : "hub-cloud.browserstack.com",
"port" : 80
},
test_workers: {
"enabled": true,
"workers":2
},
common_capabilities: {
'browserstack.user': process.env.BROWSERSTACK_USERNAME || 'BROWSERSTACK_USERNAME',
'browserstack.key': process.env.BROWSERSTACK_ACCESS_KEY || 'BROWSERSTACK_ACCESS_KEY',
'browserstack.debug': true,
'browserstack.local': true
},
test_settings: {
default: {},
chrome: {
desiredCapabilities: {
browser: "chrome"
}
},
firefox: {
desiredCapabilities: {
browser: "firefox"
}
},
safari: {
desiredCapabilities: {
browser: "safari"
}
}
}
};
// Code to support common capabilites
for(var i in nightwatch_config.test_settings){
var config = nightwatch_config.test_settings[i];
config['selenium_host'] = nightwatch_config.selenium.host;
config['selenium_port'] = nightwatch_config.selenium.port;
config['desiredCapabilities'] = config['desiredCapabilities'] || {};
for(var j in nightwatch_config.common_capabilities){
config['desiredCapabilities'][j] = config['desiredCapabilities'][j] || nightwatch_config.common_capabilities[j];
}
}
module.exports = nightwatch_config;
Im using Protractor for E2E testing. During automation, I need to download files to C:\Automation folder in my system. But below code is not working.
Note:During automation execution,The Save as popup opens(but i have to disable that in future) and I manually click "Save" option. It saves in default location ie Downloads folder.How do I make it save in my given path.
let profile = require('firefox-profile');
let firefoxProfile = new profile();
//_browser = 'chrome';
_browser = 'firefox';
// _browser = 'internet explorer';
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference('browser.download.dir', "C:\\Automation");
exports.config = {
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
capabilities: {
'browserName': _browser,
'shardTestFiles': false,
'maxInstances': 1,
'acceptInsecureCerts': true,
'moz:firefoxOptions': {
'profile': firefoxProfile
}},
beforeLaunch: function () {...}
}
It looks like you may just be missing a couple of preferences for it to work with firefox. Try adding these and see if that helps.
profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference( "browser.helperApps.neverAsk.saveToDisk",
/* A comma-separated list of MIME types to save to disk without asking goes here */ );
this will save to downloads folder inside your project. You can try to tweak it to save to desired folder. You have to specify which types of files are suppose to be downloaded without prompt. JSON and csv are already there.
var q = require('q');
var path = require('path');
var sh = require("shelljs");
var cwd = sh.pwd().toString();
var FirefoxProfile = require('selenium-webdriver/firefox').Profile;
var makeFirefoxProfile = function(preferenceMap) {
var profile = new FirefoxProfile();
for (var key in preferenceMap) {
profile.setPreference(key, preferenceMap[key]);
}
return q.resolve({
browserName: 'firefox',
marionette: true,
firefox_profile: profile
});
};
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
framework: 'jasmine2',
getMultiCapabilities: function() {
return q.all([
makeFirefoxProfile(
{
'browser.download.folderList': 2,
'browser.download.dir': (path.join(cwd, 'downloads')).toString(),
'browser.download.manager.showWhenStarting': false,
'browser.helperApps.alwaysAsk.force': false,
'browser.download.manager.useWindow': false,
'browser.helperApps.neverAsk.saveToDisk': 'application/octet-stream, application/json, text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext, text/plaintext'
}
)
]);
},
allScriptsTimeout: 1000000,
specs: ['./tmp/**/*.spec.js'],
jasmineNodeOpts: {
defaultTimeoutInterval: 1000000,
showColors: true
},
onPrepare: function() {
browser.driver.getCapabilities().then(function(caps) {
browser.browserName = caps.get('browserName');
});
setTimeout(function() {
browser.driver.executeScript(function() {
return {
width: window.screen.availWidth,
height: window.screen.availHeight
};
}).then(function(result) {
browser.driver.manage().window().setPosition(0,0);
browser.driver.manage().window().setSize(result.width, result.height);
});
});
}
};
I was trying to run a test that had worked the day before and got an error message saying
- Error: Error while waiting for Protractor to sync with the page: "window.angular is undefined. This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping. See http://git.io/v4gXM for details"
I added a console.log("hi") in one of my beforeAll() calls and there was no output in the console, so I suspect the error is that beforeAll() is not invoked for some reason.
I'm using the latest jasmine framework in my conf file and some of my tests run when I don't include the beforeAll(), but not all of them. This test was working fine yesterday and I ran it again today without any changes, so I'm not sure what the problem might be. Does anyone else know?
I'll include my conf.js file and part of the test I'm running.
My conf.js file
let SpecReporter = require('jasmine-spec-reporter').SpecReporter;
exports.config = {
framework: 'jasmine',
directConnect: true,
allScriptsTimeout: 50000,
rootElement: 'html',
untrackOutstandingTimeouts: true,
// suites: {
//HealthCheck: './specs/Health Check/**.js',
//ContectCheck: './specs/Content Check/**.js',
// },
specs: [
// 'specs/submitFeedback.js',
'specs/createTeacherAndStudentOld.js',
// 'specs/temp.js',
// 'specs/manipTeste.js',
],
capabilities: {
browserName: 'chrome'
},
params: {
screenWidth: 1920,
screenHeight: 1080,
siteURL: 'https://alpha.khmath.com',
marketingSiteURL: 'https://knowledgehook.com',
portalUsername: '',
portalPassword: '',
classRedemptionCode: 'testcode1',
contentCheckCourse: 'Grade 9 Academic',
homeWorkCourse: 'Grade 9 Applied',
gameShowCourse: 'Grade 9 Applied',
term: '2016-2017 Full Year',
gameShowSiteUrl: 'https://alpha.khmath.com/play'
},
jasmineNodeOpts: {
defaultTimeoutInterval: 7200000,
print: function() {}
},
onPrepare: function () {
global.helper = require('./helper.js');
global.completeQuestionHelper = require('./pages/student/completeQuestionHelper.js');
global.fs = require('fs');
global.https = require('https');
global.LoginPage = require('./pages/loginPage.js');
global.RegistrationPage = require('./pages/registrationPage.js');
global.TeacherGameShowPage = require('./pages/teacher/teacherGameShowPage.js');
global.TeacherHomePage = require('./pages/teacher/teacherHomePage.js');
global.TeacherMissionPage = require('./pages/teacher/teacherMissionPage.js');
global.TeacherPurchasePage = require('./pages/teacher/teacherPurchasePage.js');
global.TeacherStudentsPage = require('./pages/teacher/teacherStudentsPage.js');
global.TeacherStudentSummaryPage = require('./pages/teacher/teacherStudentSummaryPage.js');
global.TeacherReportPage = require('./pages/teacher/teacherReportPage.js');
global.StudentHomePage = require('./pages/student/studentHomePage.js');
global.StudentAllSkillsPage = require('./pages/student/studentAllSkillsPage.js');
global.StudentSkillPage = require('./pages/student/studentSkillPage.js');
global.StudentGameplayPage = require('./pages/student/studentGameplayPage.js');
global.StudentMissionPage = require('./pages/student/studentMissionPage.js');
global.StudentPortfolioPage = require('./pages/student/StudentPortfolioPage.js');
global.GameShowStudentGameplayPage = require('./pages/gameshow/gameShowStudentGameplayPage.js');
global.GameShowStudentRegistrationPage = require('./pages/gameshow/gameShowStudentRegistrationPage.js');
global.GameShowTeacherGameplayPage = require('./pages/gameshow/gameShowTeacherGameplayPage.js');
global.loginPage = new LoginPage(browser);
global.registrationPage = new RegistrationPage(browser);
global.teacherGameShowPage = new TeacherGameShowPage(browser);
global.teacherHomePage = new TeacherHomePage(browser);
global.teacherMissionPage = new TeacherMissionPage(browser);
global.teacherPurchasePage = new TeacherPurchasePage(browser);
global.teacherStudentsPage = new TeacherStudentsPage(browser);
global.teacherStudentSummaryPage = new TeacherStudentSummaryPage(browser);
global.teacherReportPage = new TeacherReportPage(browser);
global.studentHomePage = new StudentHomePage(browser);
global.studentAllSkillsPage = new StudentAllSkillsPage(browser);
global.studentSkillPage = new StudentSkillPage(browser);
global.studentGameplayPage = new StudentGameplayPage(browser);
global.studentMissionPage = new StudentMissionPage(browser);
global.studentPortfolioPage = new StudentPortfolioPage(browser);
global.gameShowStudentGameplayPage = new GameShowStudentGameplayPage(browser);
global.gameShowStudentRegistrationPage = new GameShowStudentRegistrationPage(browser);
global.gameShowTeacherGameplayPage = new GameShowTeacherGameplayPage(browser);
global.EC = protractor.ExpectedConditions;
jasmine.getEnv().addReporter(new SpecReporter({
spec: {
displayStacktrace: true
},
colors: {
enabled: false,
},
prefixes: {
successful: "O ",
failed: "X ",
},
}));
},
};
My spec file
beforeAll(function () {
console.log('hi');
helper.setBrowserParams(browser);
browser.get(browser.params.siteURL);
});
it('should register a teacher and create a premium class', function () {
//Click Register
loginPage.registerBtn.click();
...
`
I'm new to both Durandal.js and Javascript testing with Jasmine, and need some help. I have managed to get a testing environment up and running with Karma and Jasmine, but I struggle to figure out how to test my viewmodels that contains i18next dependencies.
Here's a stripped down example of my setup:
main.js
requirejs.config({
paths: {
'models': 'models',
'text': '../lib/require/text',
'durandal': '../lib/durandal/js',
'plugins': '../lib/durandal/js/plugins',
'transitions': '../lib/durandal/js/transitions',
'knockout': '../lib/knockout/knockout-2.3.0',
'bootstrap': '../lib/bootstrap/js/bootstrap.min',
'jquery': '../lib/jquery/jquery-1.9.1',
'i18next': '../lib/i18next/i18next.amd.withJQuery-1.7.1.min'
}
});
define(['plugins/router', 'durandal/system', 'durandal/app', 'durandal/binder', 'durandal/viewLocator', 'knockout', 'jquery', 'i18next'], function (router, system, app, binder, viewLocator, ko, $, i18n) {
app.configurePlugins({
router: true
});
app.start().then(function () {
// Setup of i18n
i18n.init({
detectFromHeaders: false,
fallbackLng: 'en',
preload: ['nb', 'en'],
supportedLngs: ['nb', 'en'],
resGetPath: 'locales/__lng__.json',
useCookie: true,
}, function () {
binder.binding = function (obj, view) {
$(view).i18n();
};
viewLocator.useConvention();
app.setRoot('viewmodels/shell');
});
});
});
shell.js
define(['plugins/router', 'durandal/app', 'i18next'], function (router, app, i18n) {
return {
router: router,
activate: function () {
return router.map([
{ route: '', title: i18n.t('pageTitle'), moduleId: 'viewmodels/ViewModel', nav: true }
]).buildNavigationModel().mapUnknownRoutes('viewmodels/ViewModel', 'unknown').activate({
pushState: false,
hashChange: true
});
}
};
});
test.js (own require.js config)
var tests = [];
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
if (/.*\.spec\.js$/.test(file)) {
tests.push(file);
}
}
}
//Workaround for the timestamp issue
for (var file in window.__karma__.files) {
window.__karma__.files[file.replace(/^\//, '')] = window.__karma__.files[file];
}
require.config({
baseUrl: 'base',
paths: {
'models': 'app/models',
'viewModels': 'app/viewmodels',
'specs': 'tests/specs/',
'text': 'lib/require/text',
'durandal': 'lib/durandal/js',
'plugins': 'lib/durandal/js/plugins',
'transitions': 'lib/durandal/js/transitions',
'knockout': 'lib/knockout/knockout-2.3.0',
'jquery': 'lib/jquery/jquery-1.9.1',
'i18next': 'lib/i18next/i18next.amd.withJQuery-1.7.1.min',
},
// ask Require.js to load these files (all our tests)
deps: tests,
// start test run, once Require.js is done
callback: window.__karma__.start
});
karma.conf.js
// Karma configuration
module.exports = function (config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// frameworks to use
frameworks: ['jasmine', 'requirejs'],
// list of files / patterns to load in the browser
files: [
{ pattern: 'lib/jquery/jquery-1.9.1.js', watched: false, included: true, served: true },
{ pattern: 'tests/jasmine/jasmine-jquery.js', watched: false, served: true, included: true },
{ pattern: 'tests/specs/**/*.js', included: false },
{ pattern: 'tests/sinon.js', included: false },
{ pattern: 'lib/**/*.js', included: false, served: true },
{ pattern: 'app/**/*.js', included: false, served: true },
'tests/test.js',
//Serve the fixtures
{ pattern: 'app/**/*.html', watched: true, served: true, included: false },
{ pattern: 'app/**/*.json', watched: true, served: true, included: false },
{ pattern: 'tests/**/*.json', watched: true, served: true, included: false }
],
preprocessors: [
{ '**/*.html': '' }
],
// list of files to exclude
exclude: [
'foundation/*.js',
'require.js',
'init.js',
'jasmine/jasmine.js',
'jasmine/jasmine-html.js',
'node_modules/**/*.html'
],
// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['PhantomJS'],
//browsers: ['Chrome', 'Firefox', 'IE'],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
ViewModel (i18next):
define(["i18next"], function (i18n) {
var pageTitle = i18n.t("pageTitle");
return {
pageTitle: pageTitle
};
});
Spec (i18next)
define(['viewModels/ViewModel'], function (ViewModel) {
describe("Viewmodel test", function () {
it("should be testable", function () {
expect(true).toEqual(true);
});
it("should have title", function () {
expect(ViewModel.pageTitle).not.toBeEmpty();
});
});
});
Test runner output
PhantomJS 1.9.2 (Mac OS X) ERROR
ReferenceError: Can't find variable: initialized
at /Users/xxx/dev/projectXYZ/lib/i18next/i18next.amd.withJQuery-1.7.1.min.js:5
PhantomJS 1.9.2 (Mac OS X): Executed 0 of 0 ERROR (0.312 secs / 0 secs)
ViewModel (wo/i18next):
define([], function () {
var pageTitle = "This is the page title";
return {
pageTitle: pageTitle
};
});
Spec (wo/i18next)
define(['viewModels/ViewModel'], function (ViewModel) {
describe("Viewmodel test", function () {
it("should be testable", function () {
expect(true).toEqual(true);
});
it("should have title", function () {
expect(ViewModel.pageTitle).toEqual("This is the page title");
});
});
});
Test runner output (wo/i18next)
PhantomJS 1.9.2 (Mac OS X): Executed 2 of 2 SUCCESS (0.312 secs / 0.164 secs)
I guess I have to somehow initialize i18next, but I dont know how.
Any help would be much appreciated.
Regards,
Remi
UPDATE
This is what I ended up doing:
test.js:
'i18next-original': 'lib/i18next/i18next.amd.withJQuery-1.7.1.min',
'i18next': 'tests/i18n-test'
i18n-test.js:
define(['i18next-original'], function (i18noriginal) {
'use strict';
i18noriginal.init({
lng: 'nb',
detectFromHeaders: false,
fallbackLng: 'en',
preload: ['nb', 'en'],
supportedLngs: ['nb', 'en'],
resGetPath: 'base/locales/__lng__.json',
useCookie: false,
debug: false,
getAsync: false
});
return i18noriginal;
});
karma.conf.js:
{ pattern: 'tests/i18n-test.js', included: false, served: true },
Hallais Remi!
You can initialize i18next in beforeEach like this:
define(['viewModels/ViewModel', 'i18next'], function (ViewModel, i18n) {
describe("Viewmodel test", function () {
beforeEach(function() {
var initialized = false;
runs(function() {
i18n.init({
detectFromHeaders: false,
fallbackLng: 'en',
preload: ['nb', 'en'],
supportedLngs: ['nb', 'en'],
resGetPath: 'locales/__lng__.json',
useCookie: true
}, function () { initialized = true; });
});
waitsFor(function() {
return initialized;
}, 'i18n to initialize', 100);
});
it("should be testable", function () {
expect(true).toEqual(true);
});
it("should have title", function () {
expect(ViewModel.pageTitle).not.toBeEmpty();
});
});
});
For focusing the test on code inside the viewModel itself you can consider instead to mock the i18n object used in your viewModel.
testr.js might help you mock require.js dependencies: https://github.com/medallia/testr.js
I have been using Jasmine2-HTML-Reporter and it's been fine. Produces a multi-spec report with screenshots - just as I need.
However, something seems to have gone awry! Now I will only get a report for the first Spec and nothing for any other spec. Also, the system used to delete the previous report/screenshot but now this has to be done manually.
I really cannot think what's changed in the rest of the package to cause this!!
Here's the entry in the conf.js files...identical for each conf file. I've tried adding the various switches - no effect at all!
Thanks
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
exports.config = {
//seleniumAddress: 'http://localhost:444/wd/hub',
params: require ('Models/Baths.js'),
directConnect: true,
capabilities: {
cssSelectorsEnabled: true,
'browserName': 'chrome',
specs:['Specs/001-First.js',
'Specs/003-Exp.js'
],
allScriptsTimeout: 120000,
getPageTimetout: 30000,
framework: 'jasmine2',
showColors: true,
isVerbose: true,
onPrepare: function() {
Jasmine.getEnv().addReporter(
new Jasmine2HtmlReporter({
takeScreenshots: true,
savePath: './Reports',
fileName: 'B&Q'
})
);
var SpecReporter = require ( 'jasmine-spec-reporter').SpecReporter;
jasmine.getEnv().addReporter( new SpecReporter( {
displayStacktrace: true,
displayFailureSummary: true,
displayPendingSummary: true,
displaySuccessfulSpec: true,
displayFailedSpec: true,
displaySpecDuration: true,
displaySuiteNumber: false,
colors: {
success: 'green',
failure: 'red',
pending: 'yellow'
},
customProcessors: []
} ));
}
}
};
Try with below conf file, it should work for you.
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
var log4js = require('log4js');
var params = process.argv;
var args = process.argv.slice(3);
exports.config = {
//seleniumServerJar: './node_modules/gulp-protractor/node_modules/protractor/selenium/selenium-server-standalone-2.48.2.jar',
seleniumAddress: 'http://localhost:4444/wd/hub',
allScriptsTimeout: 100000,
framework: 'jasmine2',
onPrepare: function () {
browser.manage().timeouts().implicitlyWait(11000);
var width = 768;
var height = 1366;
browser.driver.manage().window().setSize(768, 1366);
browser.ignoreSynchronization = false;
jasmine.getEnv().addReporter(
new Jasmine2HtmlReporter({
savePath: __dirname+'/qualityreports/testresults/e2e',
takeScreenshots: false,
filePrefix: 'automationReport',
consolidate: true,
cleanDestination: false,
consolidateAll: true
})
);
},
suites:{
example:['./test/e2e/specs/**/*Spec.js',]
},
capabilities: {
'browserName': 'chrome'
},
resultJsonOutputFile:'./results.json',
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 100000
}
};