How often do I need to set the setFindTimeout for my functional test?
When I execute the setFindTimeout,sometimes I need to put it in other test cases to change the findBy* method timeout.
-----------------Edit------------------------
What happen if I have multiple suite on different file.
For example I have start.js and admin.js
Is it your answer still valid for this case?
start.js
'use strict';
define([
'intern!object',
'intern/chai!assert',
'intern/dojo/node!fs'
], (registerSuite, assert, fs) => {
registerSuite(() => {
let testData;
let timeout;
let testCases;
testCases = {
name: 'Project Feature Test',
'Log into project': function() {
this.timeout = 60000 * 5;
return this.remote
.get(testData['site'])
.findByXpath('//a[#href="/saml-redirect"]').click().end()
.findById('userName').type(testData['username']).end()
.findById('password').type(testData['password']).end()
.findById('loginButton').click().end();
},
};
return testCases;
});
});
admin.js
'use strict';
define([
'intern!object',
'intern/chai!assert',
'intern/dojo/node!fs'
], (
registerSuite,
assert,
fs
) => {
registerSuite(() => {
testCases = {
name: 'Test Administration Page',
'Check User Functions': function() {
this.timeout = timeout * 3;
return this.remote
.sleep(3000)
.findByClassName('admin-button').click().end() //goto admin page
}
};
return testCases;
});
});
setFindTimeout sets the findBy timeout for a test session. Once you set it, the new timeout value persists through all the remaining tests in that session, or until it's changed by another call to setFindTimeout.
In my view, if you cannot control which file will run first, which will run after, then it's better to set in both files.
I often set it once in before of the top parent suite
Related
I'm working on Magento 2.1.7 and I'm facing a weird problem in my custom module.
I want to override Magento_Checkout/js/model/shipping-save-processor/default.
I created a requirejs-config.js like this :
var config = {
"map": {
"*": {
"Magento_Checkout/js/model/shipping-save-processor/default" : "<my_vendor>/js/shipping-save-processor/default-override"
}
}
};
and the default-override.js like this :
define(
[
'jquery',
'underscore',
'ko',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/resource-url-manager',
'mage/storage',
'Magento_Checkout/js/model/payment-service',
'Magento_Checkout/js/model/payment/method-converter',
'Magento_Checkout/js/model/error-processor',
'Magento_Checkout/js/model/full-screen-loader',
'Magento_Checkout/js/action/select-billing-address'
],
function ($,
_,
ko,
quote,
resourceUrlManager,
storage,
paymentService,
methodConverter,
errorProcessor,
fullScreenLoader,
selectBillingAddressAction) {
'use strict';
return {
functionA: function () {
console.log(resourceUrlManager); //good => return object with all the function defined in the file Magento_Checkout/js/model/resource-url-manager.js
this.functionB();
},
functionB: function () {
console.log(resourceUrlManager); // fail => Uncaught ReferenceError: resourceUrlManager is not defined
}
};
});
I'm wondering why the dependency injection "works" only for my functionA and not in functionB when its called from functionA and how to solve it cleanly without passing resourceUrlManager by parameter to functionB.
Thank you for your help.
I am running my intern test using the following code
node node_modules/intern/runner.js config=tests/intern
on my local machine. The application is using Dojo.
Basically I am trying to override the window.alert function as one of my test is failing because of unexpected alert.
window.alert = function(msg) {
//override alert function
//...
}
I tried putting this in my intern test and got the error. After some search I learned that window object is not available on node environment. Where can I override the alert?
The intern file looks like
define(['intern/lib/args'], function(args) {
var DEFAULT_PORT = "8080";
var urlInfo = {
PORT: args.port || DEFAULT_PORT,
BASE_URL : "http://localhost:".concat(args.port || DEFAULT_PORT, "/webtest"),
};
var config = {
proxyPort: 9000,
proxyUrl: 'http://localhost:9000',
capabilities: {
'selenium-version': '2.45.0',
},
...
...
};
return config;
});
Intern Test file example
define([
'intern!object',
'intern/chai!assert',
'intern/dojo/node!leadfoot/helpers/pollUntil',
'intern',
'intern/dojo/node!fs'
], function(registerSuite, assert, Pages, intern, fs) {
registerSuite ({
name: 'Tests',
setup: function() {
window.alert = function(msg){
console.log("Unexpected Alert: "+msg);
}
return this.remote.get(require.toUrl( intern.config.functionalInfo.BASE_URL)).maximizeWindow();
},
beforeEach: function() {
return
},
afterEach: function() {
return
},
'Test1' : function() {
this.timeout = 600000;
return this.remote
.setFindTimeout(5000)
....
},
}
window does not exist in node, you have to override its alert from code that runs on the browser (the code being tested), not on node itself. I would do it in the setup code for each test that uses it.
I have Grunt build automation added in my project.
I have created one custom task which sets a variable for other task and now I want to run the task using the value I set.
grunt.registerTask('dist-flow', function () {
if (!grunt.option('env')) {
grunt.option('env', 'prod');
console.log(grunt.option('env'));
}
grunt.registerTask('dist',['dev_prod_switch']);
grunt.task.run('distdev');
});
But whenever I run the dist-flow task it will set env to prod but dev_prod_switch always take default value which I set for dev_prod_switch.
So I want set the options from task and run specific task using that new value.
The problem:
Based on your question and comments, I'm assuming your Gruntfile.js looks something like this.
Gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
dev_prod_switch: {
options: {
environment: grunt.option('env') || 'dev',
env_char: '#',
env_block_dev: 'env:dev',
env_block_prod: 'env:prod'
},
all: {
files: {
'appCommon/config.js': 'appCommon/config.js',
}
}
},
});
grunt.registerTask('dist-flow', function () {
if (!grunt.option('env') ) {
grunt.option('env', 'prod');
console.log(grunt.option('env'));
}
grunt.registerTask('dist',['dev_prod_switch']);
grunt.task.run('distdev');
});
};
Your issue is that you are trying to set the option inside a task, and read it back in the initConfig object. The trouble is, initConfig runs before your tasks, so environment has already been set to the default when your dist-flow task is run.
This line:
environment: grunt.option('env') || 'dev',
Runs before this line:
grunt.option('env', 'prod');
A Solution:
Inside your task, you can access your config option through grunt.config, so you could modify the value in the config object like so.
grunt.config.data.dev_prod_switch.options.environment = grunt.option('env');
Example Gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
dev_prod_switch: {
options: {
environment: grunt.option('env') || 'dev',
env_char: '#',
env_block_dev: 'env:dev',
env_block_prod: 'env:prod'
},
all: {
files: {
'appCommon/config.js': 'appCommon/config.js',
}
}
},
});
grunt.registerTask('dist-flow', function () {
if (!grunt.option('env') ) {
grunt.option('env', 'prod');
console.log(grunt.option('env'));
grunt.config.data.dev_prod_switch.options.environment = grunt.option('env');
console.log(grunt.config.data.dev_prod_switch.options.environment);
}
grunt.registerTask('dist',['dev_prod_switch']);
grunt.task.run('distdev');
});
};
I'm new at unit testing but I've tried many methods of getting my script (require module) loaded for use with my mocha test script. No matter what I do I always get undefined when I try to read a property or function or anything. Can anyone help point out what may be causing this?
rmq.js (script to test)
define(['bx_slider/1/bx_slider'], {
...
ansrTotal: 0,
...
init: function(settings) {
var self = this;
// do some stuff
return self;
}
});
test-bootstrap.js
require.config({
paths: {
'chai': '/node_modules/chai/chai',
'bx_slider/1/bx_slider': '/test/lib/bx_slider'
},
baseUrl: '/',
nodeRequire: require
});
mocha.setup({
ui: 'bdd'
});
require(['test/test'], function() {
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
} else {
mocha.run();
}
});
test.js
define(['chai'], function(chai) {
var expect = chai.expect;
var rmq = require(['../src/js/rmq']);
describe('rmq test suite', function() {
before(function() {
return rmq.init();
});
it('should blah', function() {
expect(rmq.ansrTotal).to.equal(0);
});
});
});
If it helps, my directory structure is
.
/node_modules
/src
/js
rmq.js
/test
/lib
bx_slider.js
require.js
test-bootstrap.js
test.js
The exact error (for what I have currently written and posted here) in my CLI is
Testing: test/test.js
rmq test suite
"before all" hook
'undefined' is not a function (evaluating 'rmq.init()')
As Mathletics mentioned in a comment, you could do:
define(['chai', '../src/js/rmq'], function(chai, rmq) {
If, for some reason, you cannot do that, there's an alternative. (Maybe you simplified your code so much in your question that the reason is no longer apparent.)
define(['chai'], function(chai) {
var expect = chai.expect;
describe('rmq test suite', function() {
var rmq;
before(function (done) {
require(['../src/js/rmq'], function (_rmq) {
// Save the module we got to the `rmq` variable.
rmq = _rmq;
// Init and call `done` when finished.
rmq.init().then(done);
});
});
it('should blah', function() {
expect(rmq.ansrTotal).to.equal(0);
});
});
});
I have assumed that rmq.init() returns a promise since you did return rmq.init() in your code, and doing this does not make sense unless rmq.init() returns a promise. If that's not the case then you'd have to call rmq.init() and then call done() after it.
The code you had cannot work because require(['../src/js/rmq']) gets the module asynchronously so it does not return the module. You have to use it like I've shown above.
I've been looking at the documentation for protractor and I want to be able to select an array of via a selector of any sort and get the text value of the first one and assert it. No matter what I try with protractor it times out when I try getText() and get().
This is what I'm looking at. Reference
Here is my code:
describe('E2E: Global Scan', function () {
beforeEach(function () {
browser.get('#/dashboard/global-scan');
});
it('should grab all a tags within li tags', function () {
element.all(by.css('li a')).then(function(items) {
expect(items.length).toBeDefined()
expect(items[0].getText()).toBeDefined();
});
var list = element.all(by.css('li a'));
expect(list.length).toBeDefined()
expect(list.get(0).getText()).toBeDefined();
});
});
As you can see I tried both with a then promise function and save the result of element.all to a variable and looping through that. That doesn't work either. What happens is the test just times out.
Failures:
1) E2E: Global Scan should grab all a tags within li tags Message:
Error: Timed out waiting for Protractor to synchronize with the page after 10 seconds. Please see
https://github.com/angular/protractor/blob/master/docs/faq.md
If I try browser.element that starts to return me something. But it still breaks when I use get or getText().
Does anyone have any ideas? Doing what the documentation says doesn't actually work. Do I have a really old version of protractor?
You can increase that with setting protractor config
allScriptsTimeout: timeout_in_millis
Refer the https://github.com/angular/protractor/blob/master/docs/timeouts.md page Waiting for Page Synchronization topic for more info.
I was facing the same issue while doing E2E for Angular Project.
Finally, I configured the protractor.conf.js file and spec.e2e.ts to get rid of "Times Out Waiting For Element
".
protractor.conf.js
const { SpecReporter } = require('jasmine-spec-reporter')
exports.config = {
allScriptsTimeout: 110000,
getPageTimeout: 110000,
specs: [
'./e2e/**/example.e2e-spec.ts',
],
capabilities: {
browserName: 'chrome'
},
directConnect: true,
baseUrl: 'http://localhost:8123/',
framework: 'jasmine',
jasmineNodeOpts: {
showTiming: true,
showColors: true,
includeStackTrace: false,
defaultTimeoutInterval: 600000
},
onPrepare () {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
})
browser.driver.manage().deleteAllCookies();
browser.driver.manage().window().maximize();
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
}
}
example.e2e-spec.ts
import { browser, by, protractor, element, promise, ElementFinder, ElementArrayFinder } from 'protractor';
describe('Test', () => {
var originalTimeout;
beforeEach(() => {
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;
browser.driver.sleep(3000);
});
afterEach(function () {
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
browser.driver.sleep(5000);
});
it('It should display title and it should login also',() => {
browser.ignoreSynchronization = true;
browser.get('/',5000);
expect(browser.getTitle()).toEqual('TitleExample');
});
});