Protractor Executing afterEach before the expect has occured - javascript

For the life of me I cannot figure out why this is happening. Basically the 'it' test is running and executing correctly UNTIL the expect, at which point it jumps off the new page immediately and executes the afterEach statement. I've tried using browser.wait, .then, adding the afterEach into the 'it' browser.sleep etc and get the same result.
So does someone with more knowledge in protractor know how I can force it to execute the expect before running the afterEach?
afterEach(function() {
browser.get(browser.params.urls.base).then(function() {
$('a[href="/Account/LogOff"]').click();
});
});
it('Should use the access code url and login to the content', function() {
loginPage.registerWithAccessCode();
loginPage.accessCodeExisting(accessCode, browser.params.user.email, browser.params.user.password);
loginPage.accessCodeSubmit();
expect(browser.getCurrentUrl()).toContain('pal/placementtest/welcome');
});

Are you wrapping this in a describe?
Try this structure:
describe('My login scenario', function() {
beforeEach(function() {
loginPage.registerWithAccessCode();
// the rest of your "act" steps...
});
it('should...', function() {
expect(browser.getCurrentUrl()).toContain('pal/placementtest/welcome');
});
afterEach(function() {
// Logout
});
});

Related

Skip a global function on Cypress

I have some global variables on my cypress test scenario.
describe('AutoLogin Test Case',function(){
beforeEach(function(){
Cypress.Cookies.preserveOnce('_session_id')
})
afterEach(function(){
cy.get('[id="ajax_working"]',{timeout:6000}).should('not.be.visible')
})
it('input login info',function(){
cy.visit('https://***********.******.com/')
cy.get('[id^=user_username]')
.type('ChrisPbacon').should('have.value','ChrisPbacon')
cy.get('[id^=user_password]')
.type('welcome123').should('have.value','welcome123')
cy.contains('Sign In Now').click()
})
})
After the test case is completed the system is gonna check for the "after each" function and look for "ajax_working"... I need to skip that check ONLY on the shown "it" test, but I still need to run it on the rest of the program. I don't wanna write the aftermath function on each test as it's cumbersome and overall not clean. Anyone got any tips?
Not sure if this will fit your problem, but one of the ways you could achieve this is to wrap afterEach hook in the same context with tests than need it using describe declaration, like this:
beforeEach(function () {
cy.log("Before each hook");
});
describe('AutoLogin Test Case',function() {
afterEach(function () {
cy.log("After each hook");
});
it('Some test', function () {
cy.log("Your first test case")
});
});
describe('AutoLogin Test Case 2',function() {
it('Some other test', function() {
cy.log("Your second test case");
});
});
Note the output which has your expected behavior, beforeEach hook called in both tests, while afterEach hook is called only on first test:

How not to run certain codes in BeforeAll for a specific Describe() block in Jasmine

I'm working on a Jasmine unit test spec. There are multiple describe() blocks in the JS file.
In BeforeAll, I want to only call function for Describe-Block "A" and "C", so how can I do that? The logic is like this:
//SampleSpec.js
BeforeAll(function() {
if(descirbe name !== "B"){
DoSomething();
}
});
descirbe("A", function() {
//...
});
descirbe("B", function() {
//..
});
descirbe("C", function() {
//...
});
Without knowing more about your setup, beforeAll will always run. But you could create your own sharedSetup() function & just call it at the top of A and C, but not B. If it is promise based, you can sharedSetup.then() to defer running the rest of your code until the promise resolves.
This is beneficial as it keeps the logic for a test clearly within that test, rather than having a conditional for the test elsewhere in the test file.

Ember Data's store promises don't work in quint tests

Update
A bit of context into some quirks of the illustrative code below. StoreProxy exists as a model, created by the ApplicationRouter, that has a reference to the store. This lets other objects access the store directly (for singletons, tests, etc). Example:
MyApp.StoreProxy = DS.Model.extend();
MyApp.ApplicationRoute = U.Route.extend({
model: function () {
return this.store.createRecord('storeProxy');
}
});
Before the route is executed, StoreProxy doesn't have a store property. After, it does. I can only assume this is because of some ember-data magic.
I very well realize your reaction to this may be "Ugh! No! You're doing it wrong!". Noted. We'll move to do it the right way from here over time. That said, this is where the code is now. So, given that, and given this method for getting a reference to the current store, why doesn't the code below call its accept or rejection handlers?
Original question
I'm writing a qUnit unit test for ember. I'm using fixture data. The findAll call on the store isn't resolving or rejecting the promise.
test('Find all in store', function() {
expect(1);
var findPromise;
findPromise = MyApp.StoreProxy.store.findAll('rule');
findPromise.then(function(result) {
console.log('yes');
ok(true);
}, function(error) {
console.log('no');
});
});
I tried using async tests mentioned in this question:
testing ember fixture data with quint but the resolve and reject are never called, so the test hangs indefinitely.
I've also tried placing Ember.run calls around my code, in case it's a weird run loop thing. But to no avail.
asyncTest('Find all in store', 1, function() {
var findPromise;
Ember.run(function() {
findPromise = MyApp.StoreProxy.store.findAll('rule');
findPromise.then(function(result) {
console.log('yes');
ok(true);
start();
}, function(error) {
console.log('no');
start();
});
});
});
The code I'm testing runs fine when I run the application normally (fixture adapter or no), so it feels like something with the test environment.
Any thoughts on what to try? I'm stumped.
The way that you're writing your asynchronous tests is incorrect. Check out QUnit's page on async testing. Your test should look something like this:
asyncTest('Find all in store', function() {
var findPromise = ...;
findPromise.then(function(result) {
start();
ok(result);
}, function() {
start();
ok(false);
});
});
Specifically:
You put an extra parameter in the asyncTest function, which likely causes the test to not run at all.
You're using Ember.Application.store, which is not how you should access your store (and probably isn't even a valid store). I'm not sure what your context is, but you should be getting your store from elsewhere.
You're putting the start() calls after your assertions when they should be before.

Jasmine jQuery: loadFixtures() "undefined"?

My Set-up
Win7/64bit
Wamp Server
SublimeText 2
What I did...
Loaded jasmine via ruby (jasmine init)
Ran rake (rake jasmine)
deleted all the default public javascripts, spec files and helper files.
added jquery and jasmine-jquery to the "helpers" directory
Opened a browser, fired up liveReload and ran the following tests...
... of which, only "readFixtures()" passed. All others fail. wtf? Please advise!
"readFixtures()" test works perfectly...
describe("test read fixtures", function() {
it("should be able to read fixtures", function() {
// expect(readFixtures()).toBeDefined();
expect(readFixtures()).toBeDefined();
});
});
"loadFixtures()" test returns "Expected undefined to be defined."
describe("test load fixtures", function() {
it("should be able to load fixtures", function() {
// expect(loadFixtures()).toBeDefined();
expect(loadFixtures()).toBeDefined();
});
});
"setFixtures()" test returns "Expected undefined to be defined."
describe("test set fixtures", function() {
it("should be able to set fixtures", function() {
// expect(setFixtures()).toBeDefined();
expect(setFixtures()).toBeDefined();
});
});
I think you are not testing what you want to here. The functions setFixtures and loadFixtures do not have a return value. What that means is that when you call setFixtures() it will always return undefined. You want to test that the functions are defined, not the functions' return values. Your tests should look like this instead:
it("should be able to set fixtures", function() {
expect(setFixtures).toBeDefined(); // Notice I took out the ()
});

Expected a spy, but got Function

I am trying to implement a test (1) for this module (2).
My purpose is to check if the collection is fetched when a particular event is triggered.
As you can see from my comment in (2) I get the message Error: Expected a spy, but got Function.
The module works but the test fails. any ideas?
(1)
// jasmine test module
describe('When onGivePoints is fired', function () {
beforeEach(function () {
spyOn(this.view.collection, 'restartPolling').andCallThrough();
app.vent.trigger('onGivePoints');
});
it('the board collection should be fetched', function () {
expect(this.view.collection.restartPolling).toHaveBeenCalled();
// Error: Expected a spy, but got Function.
});
});
(2)
// model view module
return Marionette.CompositeView.extend({
initialize: function () {
this.collection = new UserBoardCollection();
this.collection.startPolling();
app.vent.on('onGivePoints', this.collection.restartPolling);
},
// other code
});
You need to get into the actual method, which in this case is on the prototype.
describe('When onGivePoints is fired', function () {
beforeEach(function () {
spyOn(UsersBoardCollection.prototype, 'restartPolling').andCallThrough();
app.vent.trigger('onGivePoints');
});
it('the board collection should be fetched', function () {
expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled();
});
});
Spying on the prototype is a nice trick you can use when you can't get to the actual instance you want to spy on.
I was also getting the same issue but I resolved it by passing an argument in the function call. Then you have to write your test case like this in the it
var data = {name:"test"}
spyOn(UsersBoardCollection.prototype, "restartPolling").and.callThrough();
UsersBoardCollection.prototype.restartPolling(data);
expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled();
I had this bug because I had two versions of sinon loaded, or possibly i wasn't initialising sinon-jasmine correctly. When I explicitly loaded sinon and then sinon jasmine in my spec setup, it started running correctly.

Categories