Is there an Jasmine JS equivalent of setUpClass? - javascript

Is there some trick to run some code at the begining and at the end of a "describe" test suite?
I am looking for something similar to setUpClass/tearDownClass from XUnit
In this example, i want to run "login_as_admin" only once before all tests and "logout" only once after all tests.
Thanks!
Here is the sample code.
/*
Functional tests.
*/
describe('Services Page', function() {
it('setUpClass', function() {
login_as_admin()
})
/*
Before each test make sure we are on the services page.
*/
setup(function() {
browser().navigateTo('/PAGE_UNDER_TEST')
})
it(
'Click on add service will get us to the Add service page.',
function() {
element('#add-service').click()
expect(browser().location().path()).toBe('/services/_add')
})
it(
'Click on edit service will get us to the Edit service page.',
function() {
element('#edit-service').click()
expect(browser().location().path()).toBe('/services/local-manager')
})
it('tearUpClass', function() {
logout()
})
})

There are a few patches to jasmine (1) and (2) which support doing this. However they do not appear to be well maintained. I have moved away from Jasmine to Mocha for this very reason.

Related

jasmine angular testing - is it possible to add a attribute on a describe method?

I am new to jasmine testing and coming from a xUnit .Net background.
Is it possible to label a test or a suite of tests in such a fashion:
[SomeAttribute]
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});
Does jasmine support any sort of attributes or identifiers? My goal really is to run a describe group of tests twice, with a different setting between test runs. I did not want to duplicate the tests. Is it possible for a test to kick off other tests?
This question is assuming that I am satisfied with duplicating a build step to run the test suit twice, just with a subset of tests for the second run.
Edit: More realistic example of how I would hope to consume it
[Theory]
[TestData(true)]
[TestData(false)]
describe("A suite", function() {
beforeEach(() => {
configureTestBed(/*someHow get input*/);
});
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});
What you can do is to define separate functions which will accept parameters from somewhere else in your code.
Something like this would do:
describe('Sample describe', () => {
testFunction(1);
});
function testFunction(param1) {
it('should execute test with params', () => {
console.log(param1);
expect(param1).toBe(1);
});
}

Nightwatch - Determine if tests have passed or failed

I'm trying to integrate my test suite with Saucelabs and to determine if the tests have passed or failed I need to do this myself.
Here is the code for the test that I have (notice I'm using Mocha):
describe('Smoke Test', () => {
describe('Login', () => {
it('Should login', (client) => {
pages.login(client).validLogin(client.globals.users.SMOKE.USERNAME, client.globals.users.SMOKE.PASSWORD);
});
});
after((client, done) => {
client.end(() => {
done();
});
});
});
Is it possible in the after block to know if the test have passed or failed?
From some examples that I found, including Saucelabs example I've seen this line:
client.currentTest.results
However currentTest have only name and method attributes.
Well, this might be late reply for you. Hope this is useful for other viewers.
afterEach will have results. Add afterEach to your example.
Apart from this, you can also get results in runner itself. Explore 'reporter' file in mocha-nightwatch. It is in your node_modules.
..\node_modules\mocha-nightwatch\lib\reporters\json.js
There are events like
runner.on(start ..)
and
runner.on(end..)
these will trigger for each tests. Give a try.

How to check result of jasmine test without using html reporter

Suppose i have a browser test that does:
describe('Test', function() {
it('should fail', function() {
expect(true).toBe(false);
});
it ('should pass', function() {
expect(true).toBe(true);
});
});
Usually what i do is call the jasmine test reporter but i need to check the result of a test without relying on html. How do i do this?
You could add your own custom reporter, collect the data into a report structure that fits you and in jasmineDone you can use the data. For more tips on how data gets sent to reporters check out the JsApiReporter provided by jasmine.

Mocha test order file-wise and webdriverjs instance persistence

I'm testing my web-app with Mocha and WebDriver. I'm struggling with the best practices regarding Mocha test-order and persistent state of the driver.
I want to separate tests to different files, e.g.
test\
index.js
selenium\
login.js
search.js
So in execution wise, login.js has to be first because it logs in to the app and gets authenticated. Only after that search.js is possible to do. But how? In login.js I now have this:
webdriverjs = require('webdriverjs');
describe 'UI/Selenium', ->
client = {}
before ->
client = webdriverjs.remote
desiredCapabilities:
browserName: 'chrome'
client.init()
client.windowHandleSize({width: 1920, height: 1080})
it 'should let us login', (done) ->
client.url('http://127.0.0.1:1337/login')
.setValue('#username', 'username')
.setValue('#password', 'password')
.buttonClick('button[type="submit"]')
.waitFor '#search_results_user', 5000, (err) -> throw err if err
.call done
How can I persist the state of client to other tests without having to re-init it every time? And how do I define the execution order of files with Mocha?
How can I persist the state of client to other tests without having to re-init it every time?
You setup whatever you want to share among tests in a before hook (and tear it down in an after hook). This would mean moving the code in your test for logging in into your before hook. Supposing you are testing the "foo" view, you could do:
describe("foo view", function () {
before(function () { /* create selenium driver */ });
describe("when used by a logged in user", function () {
before(function () { /* log in */ });
it(...
it(...
after(function () { /* log out */ });
});
describe("when used by a logged out user", function () {
it(...
it(...
});
after(function () { /* shut down the driver */ });
});
And how do I define the execution order of files with Mocha?
Mocha tests should not depend on one another and consequently should not depend on the order in which they are executed.
If you are in a situation where you must break this cardinal rule, you could just invoke Mocha from the command line with the list of test files, in the order you want. Or you could launch Mocha programmatically and use addFile to add the files in order.

How to reuse beforeEach/afterEach in Jasmine JS?

When writing tests with JasmineJS I have many tests that have similar beforeEach/afterEach code.
Is there a way to implement an inheritance model using JasmineJS test suites?
I can group all tests in a single describe but in this case I will end with a single HUGE JS file containing all tests.
I would like to split the tests for each page.
Here is an example:
describe('Services Page', function() {
beforeEach(function() {
login_as_admin()
})
beforeEach(function() {
browser().navigateTo('/services')
})
if('Some test for services page', function() {})
afterEach(function() {
logout()
})
})
describe('Administrators Page', function() {
beforeEach(function() {
login_as_admin()
})
beforeEach(function() {
browser().navigateTo('/administrators')
})
if('Some test for administrators page', function() {})
afterEach(function() {
logout()
})
})
I think this is partially examined in this blog post and also answered here but i'm adding an adapted answer for your example:
Reusable code:
function sharedSetup(startPage) {
beforeEach(function() {
login_as_admin();
browser().navigateTo(startPage);
});
afterEach(function() {
logout();
});
};
How to use it:
describe('Services Page', function() {
sharedSetup('/services');
it('Some test for services page', function() {});
});
describe('Administrators Page', function() {
sharedSetup('/administrators');
it('Some test for administrators page', function() {});
});
If you want to do this for all your suites, you can register a beforeEach or afterEach function in the topSuite:
jasmine.getEnv().topSuite().beforeEach({fn: function() {
//log in as admin
}});
If you only want to apply it on some suites, you can work with sub-suites:
describe("as_admin", function() {
beforeEach(function() {
//log in as admin
});
describe('Services Page',function() {...});
describe('Administrators Page',function() {...});
}
Jasmine does allow you to put beforeEach and afterEach outside of a describe call. In this way you can have setup and teardown that is global for all of your specs. Your logout() call seems like it might be a good candidate for global teardown, and if all of your specs login as an admin, you could move that out to global scope as well.
For things that are used in some, but not all, specs, having a method like your login_as_admin() seems like the best way to consolidate that logic in one place.
Reference: (Pivotal Labs Blog:Davis W. Frank)
He describes collecting shared functionality in a function that is called with parameters for the different individual suites. Calling this function within each suite will execute the common setup/configuration.
As to splitting tests across files; the file with the shared function can either be included within each page with a <script> tag if the tests are browser based, or by a require(...) near the top if the tests are node based. You can then run the tests independently but using that shared setup which is defined only once.
Found in the issues.
So in 3.7.0 afterEach along with other methods got moved out of Env namespace and into Globals.
the call in test.ts should be now:
afterEach(() => {});
that's it.

Categories

Resources