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:
Related
I have a problem when I'm trying to test some javascript, I'm not sure that the code is actually testable, but then I would be pleased to know how to make it testable.
$("#Button").on('touchstart', () => testFunc());
function testFunc() {
button(true, userInfo, gameCode);
}
I want to test when the button is touched that the testFunc is called. I'm using Jasmine as my test framework.
But for whatever reason when I try to call the testFunc from Jasmine just to test that something is working I get the error
"ReferenceError: Can't find variable: testClass in file:///E:/Dokumenter/SemesterProjekt%20fun%20stuff/PRJ4Web/TankWebApplication/TankWebApplication/Test/Test.js (line 8)s"
I have made the reference to the file. So I'm not sure what is going on.
The test code that gives me the error
describe("Joystick test for functionallity",
function() {
beforeEach(function() {
});
it("All default values",
function() {
testFunc();
});
});
How do I test this code? Is it possible to do?
Your goal is to see if the function will be called upon button click, therefore, you must check the actual function call with
toHaveBeenCalled jasmine method.
Invoking the functions by itself doesn't make any sense (like in your example)
You should do something like this:
it('All default values', => {
spyOn(class.testFunc);
document.getElementById('Button').click();
expect(class.testFunc).toHaveBeenCalled();
}));
})
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.
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
});
});
I'm trying to loop through some options and build tests based on those options in mocha. I've set up a simple proof of concept for making dynamic tests based roughly on this gist: https://gist.github.com/cybertk/fff8992e12a7655157ed
I keep getting the error: "TypeError: test.retries is not a function" when I run dynamicSuite.addTest(). I cannot figure out what is causing the error. There doesn't seem to be much documentation for this method of building tests in mocha.
Here's the code:
var dynamicSuite = describe('dynamic suite', function() {
this.timeout(10000);
before( function (done) {
var a = ['a', 'b', 'c'];
for(let item of a){
dynamicSuite.addTest(new common.Mocha.Test('test' + item, function(done){
done();
}));
}
done();
});
it('this is needed to make sure tests run', function (done) {
done();
});
after(function(done) {
done();
});
});//end describe test block
Test readability is important. Consider whether programatically generating tests compromises the readability of the tests and/or increases the chances that the tests contain bugs.
That said, this should work:
describe('dynamic suite', function(){
['a','b','c'].forEach(function(letter, index){
it('should make a test for letter' + letter, function(done){
// do something with letter
done();
});
});
});
You are currently adding tests in the beforeEach block, which will execute once per test in the file. So if you added another test in the file all of tests would get duplicated.
The code above works because declaring a test is just executing a function (it(name, test)), so we can just iterate over each input and execute the it 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.