I'm getting the following error:
Failed: unknown error: angular is not defined
This only happens when using angular specific selectors like "by.model". But selectors such as "by.css" work correctly . This is an Angular 2 app...
Test
it('should set focus', () => {
//This works
//var input = element(by.css('myclass'));
//This fails
var input = element(by.model('config.value'));
input.clear();
input.sendKeys('test');
input.sendKeys(Key.TAB);
input.click();
var highlightedText = browser.executeScript(function getSelectionText()
{
return window.getSelection().toString();
});
expect(highlightedText).toEqual('test');
});
exports.config = {
baseUrl: 'http://localhost:5555',
specs: [
'dist/dev/**/*.e2e.js'
],
exclude: [],
framework: 'jasmine2',
allScriptsTimeout: 110000,
jasmineNodeOpts: {
showTiming: true,
showColors: true,
isVerbose: false,
includeStackTrace: false,
defaultTimeoutInterval: 400000
},
directConnect: true,
capabilities: {
'browserName': 'chrome'
},
onPrepare: function() {
var SpecReporter = require('jasmine-spec-reporter');
jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: true}));
browser.ignoreSynchronization = false;
},
useAllAngular2AppRoots: true
};
chrome=49.0.2623.87
chromedriver=2.9.248315
platform=Windows NT 6.1 SP1 x86_64
node=5.9.1
I found the answer on the angular.io site under developer docs.
Upgrading from 1.x
Right now angular selectors by.model and by.binding are not supported. Its not clear if these are features that are possible or that they are working on for angular 2.
Related
I have one file of config which runs tests in one browser using capabilities.
Now I have created one more separate config file which contains multiCapabilites and will run same tests in multiple browsers.
I want to optimize configs so I second config file I write multiCapabilities for first config and used
delete firstConfig['capabilities'];
to ignore the capabilities from first config and use all other params from firstConfig and use multiCapabilities from 2nd config and run.
Expected result:
params in configs should not be duplicated in both configs, only multiCapabilities is the change, rest of config is same.
Use a base configuration file
Having a base configuration file and another file that extends from it might be a better approach. For this example, we will look at my base configuration file:
var env = require('./environment');
// This is the configuration for a smoke test for an Angular TypeScript application.
exports.config = {
seleniumAddress: env.seleniumAddress,
framework: 'jasmine',
specs: [
'ng2/async_spec.js'
],
capabilities: env.capabilities,
baseUrl: env.baseUrl,
allScriptsTimeout: 120000,
getPageTimeout: 120000,
jasmineNodeOpts: {
defaultTimeoutInterval: 120000
}
};
Create a second config from the base config
From there, we did something similar to your question where we removed the capabilities and added multicapabilities. (https://github.com/angular/protractor/blob/master/spec/ciNg2Conf.js). In addition, since we were running on Sauce Labs, we also decided to increase our timeouts.
exports.config = require('./angular2Conf.js').config;
exports.config.sauceUser = process.env.SAUCE_USERNAME;
exports.config.sauceKey = process.env.SAUCE_ACCESS_KEY;
exports.config.seleniumAddress = undefined;
// TODO: add in firefox when issue #2784 is fixed
exports.config.multiCapabilities = [{
'browserName': 'chrome',
'tunnel-identifier': process.env.TRAVIS_JOB_NUMBER,
'build': process.env.TRAVIS_BUILD_NUMBER,
'name': 'Protractor suite tests',
'version': '54',
'selenium-version': '2.53.1',
'chromedriver-version': '2.26',
'platform': 'OS X 10.11'
}];
exports.config.capabilities = undefined;
exports.config.allScriptsTimeout = 120000;
exports.config.getPageTimeout = 120000;
exports.config.jasmineNodeOpts.defaultTimeoutInterval = 120000;
I hope that helps.
Update:
Per comments below, setting the config.capabilities to undefined did not work; however, setting config.capabilities to false did work.
Prepare a capabilities provider to define vary capabilities, and exports a function to return a capabilities array according to the cmd line params.
// capabilities.provider.js
var capabilities = {
chrome: {
browserName: 'chrome'
},
chrome-headless {
browserName: 'chrome',
},
firefox: {
browsername: 'firefox'
},
...
};
exports.evaluate=function(){
var caps = 'chrome';
process.argv.slice(3).forEach(function(kvp){
if(kvp.includes('--caps=')) {
caps = kvp.split('=')[1] || caps;
}
})
var _caps = [];
caps.split(',').forEach(function(cap){
if(Object.keys(capabilities).includes(cap)) {
_caps.push(capabilities[cap])
}
})
return _caps;
};
protractor config.js
var capsProvider = require('./capabilities.provider');
exports.config = {
seleniumAddress: '',
framework: 'jasmine',
specs: [
'ng2/async_spec.js'
],
params: {
},
multiCapabilities: capsProvider.evaluate(),
baseUrl: env.baseUrl,
allScriptsTimeout: 120000,
getPageTimeout: 120000,
jasmineNodeOpts: {
defaultTimeoutInterval: 120000
}
};
Specify caps from cmd line:
protractor config.js --caps=chrome,firefox,ie,safari
I'm new to frontend world, I would like to write some test using protractor-image-comparison. I followed installation instructions from https://github.com/wswebcreation/protractor-image-comparison. Also I make configuration according to this page.
When I try to use functions form this lib I get following error: "TypeError: Cannot read property 'checkFullPageScreen' of undefined". I'm getting a warrning in protractor.conf.js in
const protractorImageComparison = require('protractor-image-comparison');
"Could not find a declaration file for module
'protractor-image-comparison'.
'/home/rafa/repos/example/src/example/node_modules/protractor-image-comparison/index.js'
implicitly has an 'any' type. Try npm install
#types/protractor-image-comparison if it exists or add a new
declaration (.d.ts) file containing declare module
'protractor-image-comparison';"
So I did, I made simple *.d.ts file with `declare module protractor-image-comparison' in it, but it didn't solve the problem just the warning disappear. It's propably the config issue, but I can't handle it or maybe I made wrong declaration. This is my config file :
// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts
const reporter = require("cucumber-html-reporter");
const path = require("path");
const jsonReports = path.join(process.cwd(), "/reports/json");
const htmlReports = path.join(process.cwd(), "/reports/html");
const targetJson = jsonReports + "/cucumber_report.json";
const cucumberReporterOptions = {
jsonFile: targetJson,
output: htmlReports + "/cucumber_reporter.html",
reportSuiteAsScenarios: true,
theme: "bootstrap",
};
exports.config = {
allScriptsTimeout: 110000,
restartBrowserBetweenTests: true,
//SELENIUM_PROMISE_MANAGER: false,
specs: [
'./e2e/**/login.feature'
],
capabilities: {
'browserName': 'chrome'
},
directConnect: true,
baseUrl: 'http://localhost:4200/',
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
cucumberOpts: {
format: "json:" + targetJson,
require: ['./e2e/steps/*.ts', "./e2e/timeout.ts"],
},
useAllAngular2AppRoots: true,
onPrepare: () => {
browser.ignoreSynchronization = true;
const protractorImageComparison = require('protractor-image-comparison');
browser.protractorImageComparison = new protractorImageComparison(
{
baselineFolder: "report/screens/baseline",
screenshotPath: "report/screens/actual"
}
);
},
beforeLaunch: function() {
require('ts-node').register({
project: 'e2e'
});
},
onComplete: () => {
reporter.generate(cucumberReporterOptions);
}
};
Ok, I solved it. The reason why I was getting this TypeError is that I lunched few test scenarios and onPrepare was lunched only in the begining. I move config of protractor-image-comparison to cucumber befor hook and everything works fine now.
I have a setup of my angular-app and a separate selenium server as docker-compose setup
my protractor setting looks like this
const { SpecReporter } = require('jasmine-spec-reporter');
exports.config = {
allScriptsTimeout: 11000,
specs: [
'./e2e/**/*.e2e-spec.ts'
],
capabilities: {
'browserName': 'chrome'
},
directConnect: false,
baseUrl: 'http://user-frontend:4200/',
seleniumAddress: 'http://selenium:4444/wd/hub',
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
print: function() {}
},
onPrepare() {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
}
};
now I run the command to run the end2end tests using protractor:
ng e2e --no-serve
All my tests are failing, and when I debug the selenium server and take screenshots, I see it is trying to connect to itself (localhost) instead of back to the application host baseUrl: 'http://user-frontend:4200/',
any Idea what I am doing wrong here?
Are you sure that browser.baseUrl (or browser.get()) is not set elsewhere?
My tests are throwing the following error. It used to run fine before. Please advise.
My Config file:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
allScriptsTimeout: 20000,
baseUrl: 'https://mylink/#/',
// frameworks to use
frameworks: ['jasmine'],
// Capabilities to be passed to the webdriver instance.
multiCapabilities: [{
'browserName': 'chrome'
//}, {
// 'browserName': 'firefox'
//
}],
// Spec patterns are relative to the current working directory.
specs: [
'tests/HomePage.js'
],
onPrepare: function() {
browser.driver.manage().window().maximize();
require('jasmine-reporters');
jasmine.getEnv().addReporter(
new jasmine.JUnitXmlReporter('xmloutput', true. true));
},
jasmineNodeOpts: {
showColors: true
}
}
My Spec file:
// spec.js
describe('MyTest homepage', function() {
var ptor;
require('protractor-linkuisref-locator')(protractor);
beforeEach(function() {
browser.ignoreSynchronization = true;
ptor = protractor.getInstance();
browser.rootEl = 'div';
//browser.driver.manage().window().setSize(1280, 1024);
browser.debugger();
});
it('Should enter homepage url and credentials', function() {
browser.get('/#/');
element(by.name('userName')).sendKeys('');
element(by.name('password')).sendKeys('');
element(by.xpath("//button[#class=' btn orange small wide']")).click();
});
it('Should the title contain', function() {
expect(browser.getTitle()).toEqual('');
});
it('Should click on the PowerGuide', function() {
element(by.linkUiSref('locator')).click();
ptor.sleep(100);
//expect(ptor.getCurrentUrl(locator)).toEqual('');
});
it('Should click on Month', function() {
element(by.id('month-tab')).click();
ptor.sleep(100);
});
//it('Should load the images', function() {
// element(by.css('.pure-u-1-2')).click();
//var icons = element.all(by.css('.pure-u-1-2')).click();
//icons.first().click();
//});
});
This is the error I am getting.
seleniumProcess.pid: 1380
14:43:45.163 INFO - Launching a standalone server
Setting system property webdriver.chrome.driver to C:\Users\sramamoorthy\AppData\Roaming\npm\node_modules\protractor\sel
enium\chromedriver.exe
14:43:45.342 INFO - Java: Oracle Corporation 25.40-b25
14:43:45.342 INFO - OS: Windows 7 6.1 amd64
14:43:45.373 INFO - v2.45.0, with Core v2.45.0. Built from revision 5017cb8
14:43:45.597 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
14:43:45.597 INFO - Version Jetty/5.1.x
14:43:45.597 INFO - Started HttpContext[/selenium-server,/selenium-server]
14:43:45.779 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler#23223dd8
14:43:45.781 INFO - Started HttpContext[/wd,/wd]
14:43:45.781 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
14:43:45.781 INFO - Started HttpContext[/,/]
14:43:45.789 INFO - Started SocketListener on 0.0.0.0:4444
14:43:45.789 INFO - Started org.openqa.jetty.jetty.Server#133314b
14:44:41.384 INFO - Executing: [new session: Capabilities [{count=1, browserName=chrome}]])
14:44:41.399 INFO - Creating a new session for Capabilities [{count=1, browserName=chrome}]
Starting ChromeDriver 2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf) on port 44385
Only local connections are allowed.
14:44:43.309 INFO - Done: [new session: Capabilities [{count=1, browserName=chrome}]]
14:44:43.567 INFO - Executing: [set script timeoutt: 20000])
14:44:43.584 INFO - Executing: [delete session: 64c8dac4-559f-416d-b77b-78c5184b6538])
14:44:43.590 INFO - Done: [set script timeoutt: 20000]
14:44:45.303 INFO - Done: [delete session: 64c8dac4-559f-416d-b77b-78c5184b6538]
Test Error: This is the error I am getting where i am calling the Conf.js file from
Using the selenium server at http://localhost:4444/wd/hub
[launcher] Running 1 instances of WebDriver
[launcher] Error: TypeError: undefined is not a function
at
at Object.exports.run (C:\Users\AppData\Roaming\npm\node_modules\protractor\lib\frameworks\jasmine.js:6
2:17)
at C:\Users\AppData\Roaming\npm\node_modules\protractor\lib\runner.js:326:35
at _fulfilled (C:\Users\sr\AppData\Roaming\npm\node_modules\protractor\node_modules\q\q.js:797:54)
at self.promiseDispatch.done (C:\Users\AppData\Roaming\npm\node_modules\protractor\node_modules\q\q.js:
826:30)
at Promise.promise.promiseDispatch (C:\Users\ppData\Roaming\npm\node_modules\protractor\node_modules\q
\q.js:759:13)
[launcher] Process exited with error code 100
There is a typo inside onPrepare():
onPrepare: function() {
browser.driver.manage().window().maximize();
require('jasmine-reporters');
jasmine.getEnv().addReporter(
new jasmine.JUnitXmlReporter('xmloutput', true. true));
// HERE^
},
You meant instead:
jasmine.getEnv().addReporter(
new jasmine.JUnitXmlReporter('xmloutput', true, true));
Error: TypeError: undefined is not a function
at
at Object.exports.run (C:\Users\AppData\Roaming\npm\node_modules\protractor\lib\frameworks\jasmine.js:6
The error is from the library. I think you might missed some dependencies that the library requires.
Removing this line fixed a similar problem for me:
seleniumAddress: 'http://localhost:4444/wd/hub',
I'm trying to get tests written with Mocha to work running Karma, and they sort of work, but I cannot use the done() method to implement async tests, which essentially makes the tools useless to me. What am I missing?
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '../..',
frameworks: ['mocha', 'requirejs', 'qunit'],
client: {
mocha: {
ui: 'bdd'
}
},
files: [
{pattern: 'libs/**/*.js', included: false},
{pattern: 'src/**/*.js', included: false},
{pattern: 'tests/mocha/mocha.js', included: false},
{pattern: 'tests/should/should.js', included: false},
{pattern: 'tests/**/*Spec.js', included: false},
'tests/karma/test-main.js'
],
exclude: [
'src/main.js'
],
// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['progress', 'dots'],
port: 9876,
colors: true,
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_WARN,
autoWatch: true,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['Chrome'],
// 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
});
};
test-main.js (configuring RequireJS)
var allTestFiles = [];
var pathToModule = function(path) {
return path.replace(/^\/base\//, '../').replace(/\.js$/, '');
};
Object.keys(window.__karma__.files).forEach(function(file) {
if (/Spec\.js$/.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(pathToModule(file));
}
});
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base/src',
paths: {
'should': '../tests/should/should',
'mocha': '../tests/mocha/mocha',
'pubsub': '../libs/pubsub/pubsub',
'jquery': '../libs/jquery/jquery-1.10.2',
'jquery-mobile': '//code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min'
},
// dynamically load all test files
deps: allTestFiles,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
tests/fooSpec.js
define(['music/note'], function(Note) {
describe('nothing', function(done) {
it('a silly test', function() {
var note = new Note;
note.should.not.eql(32);
});
done();
});
...
Though this is a contrived example, it succeeds if I remove the done() call. As it is, I get:
Uncaught TypeError: undefined is not a function
at /Library/WebServer/Documents/vg/tests/mocha/fooSpec.js:8
This is the done() line. How/why is this not defined? I'm not understanding where else to configure Mocha (or with what options). Is there some sort of global namespace or meta-programming magic causing RequireJS to interfere with Mocha?
I'm running the tests in Chrome 33 on OS X 10.9.2, in case that is at all relevant. I've killed a ton of time on this and am ready to give up on automated testing :( -- had similar brick walls with QUnit/Karma/RequireJS and have not been able to find any alternative to successfully automate tests. I feel like an idiot.
In Mocha, the done callback is for it, before, after, beforeEach, afterEach. So:
describe('nothing', function() {
it('a silly test', function(done) {
var note = new Note;
note.should.not.eql(32);
done();
});
});
Here's the doc.
The test you are running in that example doesn't require the done() callback. It is not asynchronous. An example of when the done callback is need....
describe('Note', function() {
it('can be retrieved from database', function(done) {
var note = new Note();
cb = function(){
note.contents.should.eql("stuff retrieved from database");
done()
}
//cb is passed into the async function to be called when it's finished
note.retrieveFromDatabaseAsync(cb)
});
});
Your test should not have a done callback
describe('nothing', function() {
it('umm...', function() {
var note = new Note;
note.should.not.eql(32);
});
});
Only the 'it' function provides a done callback. describe does not. Your problem does not rest with karma. Your mocha tests are not defined correctly.
Holy %$##!
I would never in a million years have thought this would barf:
describe('nothing', function(done) {
it('umm...', function() {
var note = new Note;
note.should.not.eql(32);
});
done(); // throws error that undefined is not a function
});
But this works just fine:
describe('nothing', function(done) {
it('umm...', function() {
var note = new Note;
note.should.not.eql(32);
});
setTimeout(function() {
done(); // MAGIC == EVIL.
}, 1000);
});