I am running this test and it seems that when the test get's to the function portion of my describe block, it skips the whole thing and gives a false positive for passing.
// required libraries
var webdriverio = require('webdriverio');
var describe = require('describe');
var after = require('after');
console.log("Lets begin");
describe('Title Test for google site', function() {
console.log("MARTY!!");
// set timeout to 10 seconds
this.timeout(10000);
var driver = {};
console.log("before we start");
// hook to run before tests
before( function (done) {
// load the driver for browser
console.log("before browser");
driver = webdriverio.remote({ desiredCapabilities: {browserName: 'firefox'} });
driver.init(done);
});
it('should load correct page and title', function () {
// load page, then call function()
return driver
.console.log("before site")
.url('http://www.ggogle.com')
// get title, then pass title to function()
.getTitle().then( function (title) {
// verify title
(title).should.be.equal("google");
// uncomment for console debug
// console.log('Current Page Title: ' + title);
});
});
});
// a "hook" to run after all tests in this block
after(function(done) {
driver.end(done);
});
console.log ("Fin");
This is the output I get
Lets begin
Fin
[Finished in 0.4s]
As you can see it skips everything else.
This is wrong and should be removed:
var describe = require('describe');
var after = require('after');
Mocha's describe and after are added to the global space of your test files by Mocha. You do not need to import them. Look at all the examples on the Mocha site, you won't find anywhere where they tell you to import describe and its siblings.
To get Mocha to add describe and its siblings, you need to be running your test through mocha. Running node directly on a test file won't work. And for mocha to be findable it has to be in your PATH. If you installed it locally, it is (most likely) not in your PATH so you have to give the full path ./node_modules/.bin/mocha.
Related
I am running into this issue where I execute my protractor test only to find out that it has failed. Now the issue is not with the actual functionality failing but with the Default Timeout Interval apparently. The Code runs fine, performs all the operation on the Webpage and just when you expect a green dot it errors out.
Before anyone marks it duplicate, I would just like to tell that I have tried the below approaches going through the answers of other similar question.
Included argument in the "it" block and called the argument after the test.
Changed the default_timeout_interval in conf.js file to 30s.
Tried using Async/await in the last function to wait for the promise to get resolved.
I would not only like to find a answer to this one but also if someone can give me an explanation on what exactly Protractor wants to convey here. To me as a novice in JavaScript and Protractor this looks like a very vague message.
Below is my spec file :
describe("Validating Booking for JetBlue WebApplication", function(){
var firstPage = require("../PageLogic/jetBlueHomePage.js");
it("Validating One Way Booking", function(pleaserun){
firstPage.OneWayTrip();
firstPage.EnterFromCity("California");
firstPage.EnterToCity("New York");
firstPage.SelectDepartureDate();
firstPage.searchFlights();
pleaserun();
});
});
Below is my Page file:
var homePage = function(){
this.OneWayTrip = function(){
element(by.xpath("//label[text()=' One-way ']/parent::jb-radio/div")).click();
}
this.EnterFromCity = function(FromCityName){
element(by.xpath("//input[#placeholder='Where from?']")).clear();
element(by.xpath("//input[#placeholder='Where from?']")).sendKeys(FromCityName);
browser.sleep(3000);
element(by.xpath("//ul[#id='listbox']/li[1]")).click();
}
this.EnterToCity = function(ToCityName){
element(by.xpath("//input[#placeholder='Where to?']")).clear();
element(by.xpath("//input[#placeholder='Where to?']")).sendKeys(ToCityName);
browser.sleep(3000);
element(by.xpath("//ul[#id='listbox']/li[1]")).click();
browser.sleep(3000);
}
this.SelectDepartureDate = function(){
element(by.xpath("//input[#placeholder='Select Date']")).click();
browser.sleep(3000);
element(by.xpath("//span[text()=' 24 ']")).click();
}
this.NumberOfPassengers = function(){
element(by.xpath("//button[#pax='traveler-selector']")).click();
}
this.searchFlights = async function(){
await element(by.buttonText('Search flights')).click();
}
};
module.exports = new homePage();
Below is the Conf.js file :
exports.config = {
directConnect: true,
specs:["TestSpecs/jetBlueBookingTest.js"],
onPrepare: function(){
browser.get("https://www.jetblue.com/");
browser.manage().window().maximize();
},
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
isVerbose: true,
includeStackTrace: true,
}
};
Seeking help from all the protractor pros out there to help me get this solution and hopefully make me understand the concept.
the message means that an async script was run in the browser, and it hasn't returned a result within the expected time limit. it would be useful if you could share the full error stack, the root cause is probably mentioned deeper down in it
I wish to render a page using Nuxt's renderAndGetWindow in order to test the values returned by my API.
Here's how I do it:
// Nuxt instance
let nuxt = null;
// Our page to test
let homePage = null;
beforeAll(async (done) => {
// Configuration
const rootDir = resolve(__dirname, '../..');
let config = {};
config = require(resolve(rootDir, 'nuxt.config.js'));
config.rootDir = rootDir; // project folder
config.env.isDev = false; // dev build
config.mode = 'universal'; // Isomorphic application
nuxt = new Nuxt(config);
await new Builder(nuxt).build();
nuxt.listen(3001, 'localhost');
homePage = await nuxt.renderAndGetWindow('http://localhost:3001/');
});
Which gives me 2 distinct errors:
console.error node_modules/jest-jasmine2/build/jasmine/Env.js:157
Unhandled error
console.error node_modules/jest-jasmine2/build/jasmine/Env.js:158
TypeError: setInterval(...).unref is not a function
And
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
at mapper (node_modules/jest-jasmine2/build/queue_runner.js:41:52)
I get this ever since I switched from Ava to Jest.
Am I handling my rendering wrong?
unref
The default test environment for Jest is a browser-like environment through jsdom.
unref is a special function provided by Node. It is not implemented in browsers or in jsdom, but it is implemented in the "node" test environment in Jest.
It looks like testing a Nuxt app requires both a Node environment to start a server, and a jsdom environment to test the resulting UI.
This can be done by setting the test environment to "node" and initializing a window using jsdom during the test setup.
timeout
Jest will "wait if you provide an argument to the test function, usually called done". This applies to test functions and setup functions like beforeAll.
Your beforeAll function has an argument done that is never called. Jest will wait until either done is called or the timeout configured with jest.setTimeout expires (defaults to 5 seconds).
You are using an async function and are using await on what looks to be the asynchronous part of the function so you don't need done. Change your beforeAll function to not take any parameters and that will prevent Jest from waiting for done to be called.
In my tests starting the Nuxt server takes quite a while so you can pass a timeout value as an additional parameter to beforeAll to increase the timeout for just that function.
Here is an updated test with these changes:
/**
* #jest-environment node
*/
// TODO: Set the environment to "node" in the Jest config and remove this docblock
// TODO: Move this to a setup file
const { JSDOM } = require('jsdom');
const { window } = new JSDOM(); // initialize window using jsdom
const resolve = require('path').resolve;
const { Nuxt, Builder } = require('nuxt');
// Nuxt instance
let nuxt = null;
// Our page to test
let homePage = null;
beforeAll(async () => {
// Configuration
const rootDir = resolve(__dirname, '../..');
let config = {};
config = require(resolve(rootDir, 'nuxt.config.js'));
config.rootDir = rootDir; // project folder
config.env.isDev = false; // dev build
config.mode = 'universal'; // Isomorphic application
nuxt = new Nuxt(config);
await new Builder(nuxt).build();
nuxt.listen(3001, 'localhost');
homePage = await nuxt.renderAndGetWindow('http://localhost:3001/');
}, 20000); // Give the beforeAll function a large timeout
afterAll(() => {
nuxt.close();
});
describe('homepage', () => {
it('should do something', () => {
});
});
Is there any automation that I can use when a test fails on jasmine to prepare the same environment for a specific failing test? I'm using karma as the spec runner.
For example:
describe("this plugin", function(){
beforeEach(function(){
$("body").append("<input type='text' id='myplugintester'>");
this.plugin = $("#myplugintester").plugin({
cond1: true,
cond2: new Date(),
condetc: null
});
}
afterEach(function(){
$("#myplugintester").data("plugin").destroy();
$("#myplugintester").remove();
}
it("should show the correct value", function(){
expect(this.plugin.value).toEqual("somevalue");
});
it("should display 'disabled' when cond3 is not null", function(){
this.plugin.cond3 = "blabla";
expect(this.plugin.value).toEqual("somevalue");
});
});
When the second case fails, I have to write this to a test page to debug what goes wrong with the code.
var expect = function(){
// filler code
};
$("body").append("<input type='text' id='myplugintester'>");
this.plugin = $("#myplugintester").plugin({
cond1: true,
cond2: new Date(),
condetc: null
});
this.plugin.cond3 = "blabla";
console.log(this.plugin.value);
$("#myplugintester").data("plugin").destroy();
$("#myplugintester").remove();
Does any node package automate this? Or how do other developers react in this cases?
Note: I switched from grunt-jasmine to grunt-karma because of the speed. grunt-jasmine allowed me to run single test cases on browsers which I could debug with Chrome Developer Tools. I looked for several html reporters for karma but they only state the result on output HTML. They are not running the specs which I can interrupt and debug.
At last, I wrote a karma plugin to fill that gap. If someone needs that:
https://www.npmjs.com/package/karma-code-reporter
I built a yeoman generator that grabs data from a rest api and populates html, css, and js files in an app dir with those files. I am trying to learn how to build the respective mocha test but keep running into an error on my file exist test.
I pass the "does the generator load" test, but when i try the "do files exist" test, I get an error at the console that:
1)creates files creates expected files:
Uncaught TypeError: cb is not a function
at null.<anonymous> (node_modules/yeoman-generator/lib/base.js:400:5)
at Queue.<anonymous> (node_modules/grouped-queue/lib/queue.js:68:12)
My test is as follows
/*global describe, beforeEach, it*/
'use strict';
var path = require('path');
var helpers = require('yeoman-generator').test;
describe("creates files", function(){
beforeEach(function (done){
helpers.testDirectory(path.join(__dirname, 'temp'), function(err){
if(err){
return done(err);
}
this.app = helpers.createGenerator('snow:app',['../../app']);
done();
}.bind(this));
});
it("creates expected files", function(done){
helpers.mockPrompt(this.app,{
hostname : "empasiegel1",
username : "password",
password : "password",
solution : "testSnow"
});
this.app.run({}, function() {
helpers.assertFile('package.json');
done();
});
});
});
Could it have soemthing to do with the asynchronous nature of the rest calls?
I want to be able to have a set of Casper JS tests and get an exit code back of 0 on success and non-zero on error or test failure (I want to run the casper command from java and determine if a test passed).
The problem I am having is that an exit code of 0 is always returned. Here is an example test where this happens:
var casper = require('casper').create();
casper.start('http://www.google.com', function() {
this.test.assertEquals(true, casper.cli.options['value']);
});
casper.run(function() {
casper.test.done(1);
});
All of the following commands result in an exit code of 0:
C:/casperjs/bin/casperjs test --value=true C:/Temp/simpletest.js
C:/casperjs/bin/casperjs test --value=false C:/Temp/simpletest.js
C:/casperjs/bin/casperjs --value=true C:/Temp/simpletest.js
C:/casperjs/bin/casperjs --value=false C:/Temp/simpletest.js
How can I invoke Casper and determine whether the tests succeeded or failed/errored from Java?
First, you cannot overwrite the casper instance in test mode, see http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options
Remove
var casper = require('casper').create();
from your code.
Then try
casper.start('http://www.google.com', function(test) {
test.assertEquals(true, casper.cli.options['value']);
});
Start casperjs with
--fail-fast
so that each test will exit with code 1.
Then in Java
String[] args = {"/bin/sh", "-c", "casperjs test --fail-fast simpletest.js"};
Process proc = Runtime.getRuntime().exec(args);
logger.log(Level.INFO, IOUtils.toString(proc.getInputStream()));
String warnings = IOUtils.toString(proc.getErrorStream());
if (StringUtils.isNotBlank(warnings)) {
logger.log(Level.WARNING, warnings);
}
int exitValue = proc.exitValue();
assertEquals(0, exitValue);
Of course you need to change the paths to suit your environment.
Hope that helps!
The problem I am having is that an exit code of 0 is always returned.
Your casper test should be like that:
var casper = require('casper').create();
var system = require('system');
var param;
casper.start('http://www.google.com', function() {
//get parameter from command line
system.args.forEach(function (arg, i) {
if(arg.search(/--value=/i) != -1){
param = arg.replace(/--value=/g, "");
}
});
this.test.assertEquals(true, Boolean(param));
});
casper.run(function() {
this.test.done(1);
this.test.renderResults(true);
});
To run:
casperjs simpletest.js --value=true
How can I invoke Casper and determine whether the tests succeeded or failed/errored from Java?
You should look this answer:
CasperJS passing data back to PHP
Return the Predefined code for failure (for eg for us we gave 99 (random))
//Capture all fails
casper.test.on("fail", function () {
casper.exit(99);
});
Similarly you can define different code for other issues at high level
eg: to get the retry logic in place we can use onWaitTimeout codes
casper.options.onWaitTimeout = function () {
casper.screenCapture('POETerror', true);
this.exit(98);
};