Attaching protractor to existing browser instance
Sagar Shroff
Sr Software Development Engineer In Test - Selenium | Cucumber | Karate | Cypress | Javascript | Java | AWS
Protractor-test-runner by default behavior is
- Creates web-driver instance & opens up browser
- Executes your tests
- Kills the web-driver instance & thus closing your browser
While automating/developing test scripts; you may want to run just a single line of your automation code and test it for its consistency, rather than executing the entire flow described above (which becomes lot time-consuming).
Follow as below
Unit Test Runner
describe("Development mode", ()=> {
it("Main", ()=>{
/YOUR CODE GOES HERE/
});
beforeAll(()=> {
browser.getProcessedConfig().then((processConfig)=>{
if(processConfig.seleniumSessionId == undefined) {
/YOUR APP URL GOES HERE/
browser.get("<URL>");
browser.getSession().then((session)=>{
console.log("------------------- Created new session : "+session.getId()+" -------------------");
});
}
});
});
afterAll(()=>{
browser.driver.getSession().then((sessionid)=>{
console.log("-------------------Test completed. For debugging use sessionId: : "+sessionid.getId()+"-------------------");
});
browser.getProcessedConfig().then((processConfig)=>{
if(processConfig.seleniumSessionId == undefined){
browser.pause(11111);
}
});
});
});
Basically, in above script,
- Main: Paste the lines of code which you are interested in running unitly.
- BeforeAll: does nothing but opens up a browser only for first execution (ie if no browser is open) and prints the session id created by web-driver; which we can use in our subsequent runs to attach to existing browser
- Afterall: the purpose is to stop the execution for the first run only. This is done to avoid killing of session instance (which web-driver does by default on completion). You can also use a simple breakpoint to achieve the same if you are using an IDE.
Running protractor without session id (one-time activity)
You can keep Main (the method in the script) empty just for the first run; as we need the session-id.
protractor ./Test/protractor.conf.js
So on the first run, the script will open the browser, it prints session-id and pauses.
As seen session id is printed and its paused. Now we need to press Ctrl + C twice to exit.
Running protractor with session id
So all the hard-work for getting session id is completed :). Now just paste the code which you want to execute inside Main (the method in the script) and run the command using the session-id parameter.
protractor ./Test/protractor.conf.js --seleniumSessionId=5d385aaa-060a-43de-813c-1d93d246680f
This will run the code inside main on the existing browser instance because this time we are passing it the session-id of the browser.
Summary
The argument (seleniumSessionId) can not only be used for above use case; ie to do unit testing while developing, but it also enables us to integrate protractor with other automation tools. Like, consider you already have old automated selenium test suite which you automated long time back and obviously you would not want to re-write those test again, rather you would want to simply integrate the selenium and protractor tests and can be done very easily using this property.
Cheers, Do let me know if you have any doubts.