Timeout Issue in testing Protractor with Jasmine - protractor

I am using Protractor to test my Angular Application.
I am using the modules i created as a pre-condition to test my cases.
When i run my test before the pre-condition met the browser is exiting.
Please check my pre-condition call in my test and suggest any solution to solve this issue.
beforeAll(function (done) {
async.series([
function (callback1) {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1900000;
setTimeout(function () {
console.log("inside timeout.....");
callback1();
}, 500);
browser.driver.manage().deleteAllCookies();
browser.ignoreSynchronization = false;
},
function (callback2) {
registerNewUser(myCredentials, function (res) {
//browser.waitForAngular();
myCredentials = res;
console.log(JSON.stringify(res));
console.log(res);
expect(res).not.toBeNull();
//browser.waitForAngular();
// browser.sleep(5000)
callback2(res);
//browser.pause(10000);
// browser.sleep(5000)
browser.waitForAngular();
});
}
],
function (err) {
// browser.sleep(5000)
console.log("Inside done");
browser.waitForAngular();
done();

I suggest you to use browser.driver.sleep(500) to befire or after your operations. :)

Related

unininstall PWA Manually

The following code can be used to install the program in the PWA:
var fab = document.querySelector('#fab');
var deferredPrompt;
fab.addEventListener('click', function () {
if (deferredPrompt) {
deferredPrompt.prompt();
deferredPrompt.userChoice.then(function (choice) {
if (choice.outcome === 'dismissed') {
console.log('installation was cancelled');
} else {
console.log('User Added To Home Screen');
}
});
deferredPrompt = null;
}
});
//********************************************************************
window.addEventListener('beforeinstallprompt', function (event) {
console.log('beforeinstallprompt run .');
event.preventDefault();
deferredPrompt = event;
return false;
});
now for Uninstall:
It can only be removed from the browser
Now my question is here:
Is it possible to create a code such as manual installation (mentioned above) that the user can uninstall the program without the need to use the browser tool?
Thank you all for your answers

Cannot run multiple karma tests using suave server

When I run the below tests individually (by commenting out) then each test passes. However, when I run all tests then I get an XmlHttpRequest uncaught exception. The suave test server receives the requests and the logging shows no errors or problems:
var HOME_URL = "http://localhost:3000/request";
it("should echo the test request with response", function (done) {
var test = { act: 'test1', qry: {} };
var promise = webix.ajax().post(HOME_URL, JSON.stringify(test));
console.log('test1');
promise.then(function (resp) {
expect(resp.json().succ).to.be(true);
done();
}).fail(function (err) {
done();
throw(err);
});
});
it("should echo the test request with response 2", function (done) {
var test = { act: 'test2', qry: {} };
var promise = webix.ajax().post(HOME_URL, JSON.stringify(test));
console.log('test2');
promise.then(function (resp) {
expect(resp.json().succ).to.be(true);
done();
}).fail(function (err) {
console.log('echo test error', app.util.inspect(promise));
done();
throw(err);
});
});
Any ideas what the problem could be or how to debug these tests?
To run the code yourself (git node and npm have to be installed):
git clone http://github.com/halcwb/GenUnitApp.git
cd GenUnitApp
git checkout failingServer
scripts/run.sh
Open second terminal
./build.sh clienttests
For anyone running into this, you can nest the ajax calls in a before function and later on use the promises (webix.ajax returns a promise) in your tests like:
var HOME_URL = "http://localhost:3000/request";
var test1, test2;
before(function () {
var req = { act: 'test1', qry: {}};
test1 = webix.ajax().post(HOME_URL, JSON.stringify(req));
req.act = "test2";
test2 = webix.ajax().post(HOME_URL, JSON.stringify(req));
});
it("should echo the test request with response", function (done) {
var promise = test1;
promise.then(function (resp) {
expect(resp.json().succ).to.be(true);
done();
}).fail(function (err) {
done();
throw(err);
});
});
it("should echo the test request with response 2", function (done) {
var promise = test2;
promise.then(function (resp) {
expect(resp.json().succ).to.be(true);
done();
}).fail(function (err) {
done();
throw(err);
});
});

Protractor ignoring specs passed in a callback function

I'm running into an issue and need your help.
I have a list of products and I want to run some it blocks for each product.
The function getProducts is an asynchronous function. Here is my code.
jsonLoader = new Promise(function(resolve, reject) {
beforeAll(function(done) {
getProducts(function(loadedProducts) {
resolve(loadedProducts);
done();
});
});
});
describe('product-maintenance', function() {
jsonLoader.then(function(products) {
productsList = products;
//productsList contains the desired products
_.forOwn(productsList, function(product) {
//execute it-blocks
});
});
it('some test', function() {
expect(1).toBe(1);
});
});
He is only executing the it 'some test' and simply ignoring the it blocks in the _.forOwn loop.
Thanks !!! :)
I solved this by using promises in the onPrepare function.
onPrepare: function() {
var deferred = protractor.promise.defer();
getProducts(function(products) {
if (!products) {
deferred.reject(new Error('An error occured while loading products'));
} else {
productsModule.setProducts(products);
deferred.fulfill();
}
});
return deferred.promise;
}

Protractor & Cucumberjs after hook doesn't work as expected

I have written a basic after hook in cucumberjs for some reason , it is not working as expected.It is supposed to attached screenshot and write browser console log , when scenario fails. But it attaches the screen shot after the feature in html report and prints the browser console log at after in between the second scenarioenter image description here.Any clue what's wrong??
this.After(function(scenario, callback) {
if (scenario.isFailed()) {
global.browser.takeScreenshot().then(function(base64png) {
var decodedImage = new Buffer(base64png,'base64').toString('binary');
scenario.attach(decodedImage, 'image/png');
});
global.browser.manage().logs().get('browser').then(function (browserlog){
browserlog.forEach(function (log) {
if (log.level.value > 900) {
console.error(log.message.substring(log.message.indexOf('Error'),log.message.indexOf('\n')))
}
})
});
callback();
} else {
callback();
}
});
According to the cucumberjs github page https://github.com/cucumber/cucumber-js#attachments
Images and other binary data can be attached using a stream.Readable. In that case, passing a callback to attach() becomes mandatory:
You could split the single after hook into two separate hooks:
this.After(function(scenario, next) {
browser.takeScreenshot().then(function(png) {
var decodedImage = new Buffer(png, 'base64').toString('binary');
scenario.attach(decodedImage, 'image/png', next);
}, function(err) {
next(err);
});
});
this.After(function(scenario, next) {
global.browser.manage().logs().get('browser').then(function (browserlog){
browserlog.forEach(function (log) {
if (log.level.value > 900) {
console.error(log.message.substring(log.message.indexOf('Error'),log.message.indexOf('\n')))
}
});
});
});

Drop MongoDB database before running Mocha test

If I try to drop the database using after (at the end of my tests) it works.
If I try the following:
var db = mongoose.connect('mongodb://localhost/db-test')
describe('Database', function() {
before(function (done) {
db.connection.db.dropDatabase(function(){
done()
})
})
...
it does not drop the DB. what is going on? I would prefer dropping the db before starting testing -- so that after testing I can explore the db.
solved by connect in another define.. not sure if ideal.
describe('Init', function() {
before(function (done) {
mongoose.connect('mongodb://localhost/db-test', function(){
mongoose.connection.db.dropDatabase(function(){
done()
})
})
})
describe('Database', function() {
I implemented it a bit different.
I removed all documents in the "before" hook - found it a lot faster than dropDatabase().
I used Promise.all() to make sure all documents were removed before exiting the hook.
beforeEach(function (done) {
function clearDB() {
var promises = [
Model1.remove().exec(),
Model2.remove().exec(),
Model3.remove().exec()
];
Promise.all(promises)
.then(function () {
done();
})
}
if (mongoose.connection.readyState === 0) {
mongoose.connect(config.dbUrl, function (err) {
if (err) {
throw err;
}
return clearDB();
});
} else {
return clearDB();
}
});