AngularJS with ngTable, testing with Intern - angularjs

I am trying to setup intern testing for my angularjs app that has ngTable dependency.
So my test file for AccountController:
define([
'intern/chai!expect',
'intern!bdd',
'intern/order!nonNgDependencies/non-angular-dependencies.min.js',
'intern/order!angular/angular',
'intern/order!angular-mocks/angular-mocks',
'intern/order!ui.router/release/angular-ui-router',
'intern/order!ngResource/angular-resource',
'intern/order!bower_components/ng-table/dist/ng-table.js',
'intern/order!im/app',
'intern/order!im/account/AccountService',
'intern/order!im/account/AccountController'
], function (expect, bdd) {
function inject (fn) {
return function () {
angular.injector(['ng', 'ngMock', 'ui.router', 'ngTable', 'IntegrationManager']).invoke(fn);
}
}
bdd.describe('AccountController', function () {
var ctrl, scope;
bdd.beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
ctrl = $controller('AccountController', { $scope: scope });
}));
bdd.it('should return true', function () {
expect(scope.isTrue()).to.true;
});
bdd.it('should not have Accounts to start', function () {
expect(scope.accounts).to.be.undefined;
});
});
});
My internjs config file:
// Learn more about configuring this file at <https://github.com/theintern/intern/wiki/Configuring-Intern>.
// These default settings work OK for most people. The options that *must* be changed below are the
// packages, suites, excludeInstrumentation, and (if you want functional tests) functionalSuites.
define({
// The port on which the instrumenting proxy will listen
proxyPort: 9000,
// A fully qualified URL to the Intern proxy
proxyUrl: 'http://localhost:9000/',
// Default desired capabilities for all environments. Individual capabilities can be overridden by any of the
// specified browser environments in the `environments` array below as well. See
// https://code.google.com/p/selenium/wiki/DesiredCapabilities for standard Selenium capabilities and
// https://saucelabs.com/docs/additional-config#desired-capabilities for Sauce Labs capabilities.
// Note that the `build` capability will be filled in with the current commit ID from the Travis CI environment
// automatically
capabilities: {
'selenium-version': '2.41.0'
},
// Browsers to run integration testing against. Note that version numbers must be strings if used with Sauce
// OnDemand. Options that will be permutated are browserName, version, platform, and platformVersion; any other
// capabilities options specified for an environment will be copied as-is
environments: [
// { browserName: 'internet explorer', version: '11', platform: 'Windows 8.1' },
// { browserName: 'internet explorer', version: '10', platform: 'Windows 8' },
// { browserName: 'internet explorer', version: '9', platform: 'Windows 7' },
// { browserName: 'firefox', version: '28', platform: [ 'OS X 10.9', 'Windows 7', 'Linux' ] },
{ browserName: 'chrome', version: '34', platform: [ 'OS X 10.9', 'Windows 7', 'Linux' ] },
// { browserName: 'safari', version: '6', platform: 'OS X 10.8' },
// { browserName: 'safari', version: '7', platform: 'OS X 10.9' }
],
// Maximum number of simultaneous integration tests that should be executed on the remote WebDriver service
maxConcurrency: 3,
// Name of the tunnel class to use for WebDriver tests
tunnel: 'SauceLabsTunnel',
tunnelOptions: {
'username': '*****',
'accessKey': '**********************'
},
// The desired AMD loader to use when running unit tests (client.html/client.js). Omit to use the default Dojo
// loader
useLoader: {
'host-node': 'dojo/dojo',
'host-browser': 'node_modules/dojo/dojo.js'
},
// Configuration options for the module loader; any AMD configuration options supported by the specified AMD loader
// can be used here
loader: {
// Packages that should be registered with the loader in each testing environment
packages: [
{ name: 'im', location: 'js/src' },
{ name: 'angular', location: 'bower_components/angular' },
{ name: 'angular-mocks', location: 'bower_components/angular-mocks' },
{ name: 'ui.router', location: 'bower_components/angular-ui-router' },
{ name: 'ngTable', location: 'bower_components/ng-table/dist' },
{ name: 'ngResource', location: 'bower_components/angular-resource' },
{ name: 'nonNgDependencies', location: 'tests'}
]
},
// Non-functional test suite(s) to run in each browser
suites: [ 'tests/all' ],
// Functional test suite(s) to run in each browser once non-functional tests are completed
functionalSuites: [ /* 'myPackage/tests/functional' */ ],
// A regular expression matching URLs to files that should not be included in code coverage analysis
excludeInstrumentation: /^(?:tests|bower_components|node_modules)\//
});
The error I get when trying to run intern is:
undefined: Failed to load module angular/main from /bower_components/angular/main.js (parent: bower_components/ng-table/dist/ng-table)
Error: Failed to load module angular/main from /bower_components/angular/main.js (parent: bower_components/ng-table/dist/ng-table)
at HTMLScriptElement.handler <__intern/node_modules/dojo/dojo.js:731:13>
No unit test coverage for chrome 34.0.1847.116 on Windows NT
From the error, my guess is that it isn't able to load ngTable, however I am not sure where to start.
Any advice on how to troubleshoot this?

Related

Electron: Run end to end tests with protractor

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://';
}
};

Protractor e2e test case for download file work fine on chrome but not with firefox and vice versa?

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.

protractor not stopping selenium server

We are getting a timeout error after our protractor tests have completed.
Error updating Sauce pass/fail status: 'Could not send request: connect ETIMEDOUT
We have tried adding these to the onComplete function in our conf.js:
browser.driver.quit(), browser.driver.stop(), and different forms like this. None seem to be stopping the web driver. for the stop() function we are obviously receiving the error that the function DNE. For the browser.driver.quit(), we are receiving the timeout still. How do we stop the webdriver?
My protractor version: Version 3.3.0
My Complete conf.js:
exports.config = {
sauceUser: process.env.SAUCE_USERNAME,
sauceKey: process.env.SAUCE_ACCESS_KEY,
tunnelIdentifier: process.env.SAUCE_TUNNELL_IDENTIFIER,
webDriverProxy: 'my-companies-proxy',
specs: ['e2e/*spec.js'],
framework: 'jasmine2',
onPrepare: function(){
var caps = browser.getCapabilities()
},
multiCapabilities: [{
browserName: 'chrome',
version: '41',
platform: 'Windows 7',
name: "chrome-tests",
shardTestFiles: true,
maxInstances: 25
}],
onComplete: function() {
var printSessionId = function(jobName){
browser.getSession().then(function(session) {
console.log('SauceOnDemandSessionID=' + session.getId() + ' job-name=' + jobName);
});
}
printSessionId("Protractor Tests");
browser.driver.quit();
}
}

Protractor - run specific test as mobile device

I have a feature that acts differently for mobile web / desktop. My media query is running on device-width, for example:
#media only screen and (max-device-width: 1024px) {
...
}
This means that I cannot just change browser size (it will not work):
browser.manage().window().setSize(320, 480);//will not be affected by 'device-size' media query
I found an option to change chromeOptions on device capabilities. I tried using this with multiCapabilities:
'use strict';
var config = require('infra-gruntfile/protractor-conf').config;
// old capabilities
// config.capabilities = {
// browserName: 'chrome'
// };
config.multiCapabilities =
[
{
browserName: 'chrome',
name: 'Unnamed Job',
count: 1,
shardTestFiles: false,
maxInstances: 10,
chromeOptions: {
'mobileEmulation': {
'deviceName': 'Apple iPhone 4'
},
args: [
'incognito',
'disable-extensions'
]
},
loggingPrefs: {
browser: 'ALL'
},
specs: ['app/**/*.mobile.spec.js']
},
{
browserName: 'chrome',
specs: ['!app/**/*.mobile.spec.js']
}
]
module.exports.config = config;
It looks like specs doesn't work (I was trying different spec options) but all of them will run all test on both mobile/non mobile browser. I need the test mobile test to run only on mobile, while all others on regular browser.
Do I have a programatically way of doing this during my test (and not using config)?
What am I doing wrong with my current config? Why isn't specs working for me?
After reading this post I figured out that the problem is that the capability-specific specs are in addition to the main config.specs.
This means I have to remove the defaults arrived from my infrastructure config file, using specs: []
'use strict';
var config = require('infra-gruntfile/protractor-conf').config;
// old capabilities
// config.capabilities = {
// browserName: 'chrome'
// };
config.specs = []; //This line solved my problem!
config.multiCapabilities =
[
{
browserName: 'chrome',
name: 'Unnamed Job',
count: 1,
shardTestFiles: false,
maxInstances: 10,
chromeOptions: {
'mobileEmulation': {
'deviceName': 'Apple iPhone 4'
},
args: [
'incognito',
'disable-extensions'
]
},
loggingPrefs: {
browser: 'ALL'
},
specs: ['app/**/*.mobile.spec.js']
},
{
browserName: 'chrome',
specs: ['!app/**/*.mobile.spec.js']
}
]
module.exports.config = config;

Internet Explorer Selenium protractor e2e tests

I would like to add to our CI build process some e2e tests. I have already added them against chrome + firefox (as the simplest ones). But I really want to do it for several IE versions. How is it possible to inject it in build process on linux/mac?
I found such article:
http://elgalu.github.io/2014/run-protractor-against-internet-explorer-vm/
But looks like it is not 100% what I need. Could some one provide a simple configuration sample?
You would need a selenium server, either your own, or at browserstack/SauceLabs. If you are planning to do it on your own, in short, you would need to setup a selenium grid and register nodes, one of the nodes should be a windows machine where you would run tests against IE.
Personally, I've been successfully running protractor e2e tests on multiple browsers including different Chrome, Firefox and IE versions on browserstack. Here's the configuration I use (it also includes jasmine junit reporter, needed this for the CI):
'use strict';
var browserstackUser = 'user';
var browserstackKey = 'key';
exports.config = {
multiCapabilities: [
{
'browserstack.user': browserstackUser,
'browserstack.key': browserstackKey,
'browserstack.local': 'true',
'browserstack.debug': 'true',
'browserName': 'Chrome',
'os': 'Windows',
'os_version': '8',
'resolution': '1024x768',
specs: [
'*.spec.js'
],
exclude: [
'footer.disabledCookies.spec.js'
]
},
{
'browserstack.user': browserstackUser,
'browserstack.key': browserstackKey,
'browserstack.local': 'true',
'browserstack.debug': 'true',
'browser': 'Internet Explorer',
'browser_version': '8.0',
'os': 'Windows',
'os_version': '7',
'resolution': '1024x768',
specs: [
'*.spec.js'
]
},
{
'browserstack.user': browserstackUser,
'browserstack.key': browserstackKey,
'browserstack.local': 'true',
'browserstack.debug': 'true',
'browserName': 'Internet Explorer',
'browser_version': '9.0',
'os': 'Windows',
'os_version': '7',
'resolution': '1024x768',
specs: [
'*.spec.js'
],
exclude: [
'footer.disabledCookies.spec.js'
]
}
],
// Browserstack's selenium server address
seleniumAddress: 'http://hub.browserstack.com/wd/hub',
framework: 'jasmine',
allScriptsTimeout: 300000,
baseUrl: 'http://localhost:9001',
onPrepare: function () {
require('jasmine-reporters');
var capsPromise = browser.getCapabilities();
capsPromise.then(function (caps) {
var browserName = caps.caps_.browserName.toUpperCase();
var browserVersion = caps.caps_.version;
var prePendStr = browserName + "-" + browserVersion + "-";
jasmine.getEnv().addReporter(new
jasmine.JUnitXmlReporter("test-results", true, true, prePendStr));
});
},
jasmineNodeOpts: {
showColors: true,
isVerbose: true,
includeStackTrace: true,
defaultTimeoutInterval: 3600000
}
};

Resources