Reputation: 37
I have to test a browser page that requires a login. Is there a way to keep the browser open between every it block so I don't need to login for every it block?
Thanks
Upvotes: 1
Views: 510
Reputation: 302
Is there a way to keep the browser open between every it block so I don't need to login for every it block?
Yes: Provide the browser session, before starting protractor - it is called attach in Protractor lingo
Protractor will not close the browser session if you have started the browser session before starting Protractor.
I have created an NPM Package to start the browser easily. Let me know if this helps you.
https://github.com/andreasmarkussen/webdriver-reuse-session
Upvotes: 1
Reputation: 694
Protractor closing browsers between different tests files is by design. However, there are a few ways to solve your problem.
1) Write all your tests in one file
Self explanatory, this will accomplish what you are trying to do, but only works as long as you aren't running tests in parallel. This will work for you, but I would recommend using the 2nd option. You are able to have multiple describes in the same test file which will help it be more readable if you go this route. Example test.js file:
describe('Testing login',function(){
it('Should login',function(){
//test here
});
});
describe('Testing foo',function(){
it('Should test foo here',function(){
//test here
});
it('Should test foo here as well',function(){
//test here
});
});
describe('Testing bar',function(){
it('Should test bar here',function(){
//test here
});
it('Should test bar here as well',function(){
//test here
});
});
2) Use a login function
This is what I personally use. I have a function that I call at the start of every test that logs in. The function accepts a Username
and Password
. While this doesn't keep the browser open, I personally think this is best way to go at this problem. Example:
Test1.js:
const helper = require util.js;
const util = new helper;
describe('Testing foo',function(){
beforeAll(function(){
util.login();
};
it('Should test foo here',function(){
//test here
});
it('Should test foo here as well',function(){
//test here
});
});
test2.js
const helper = require util.js;
const util = new helper;
describe('Testing bar',function(){
beforeAll(function(){
util.login();
};
it('Should test bar here',function(){
//test here
});
it('Should test bar here as well',function(){
//test here
});
});
util.js:
const helper = function(){
this.login = function(){
//do login here
}
}
Here is a useful stack post about reusing code, it goes more into depth about some syntax.
Upvotes: 0