Attaching protractor to existing browser instance

Attaching protractor to existing browser instance

Protractor-test-runner by default behavior is

  1. Creates web-driver instance & opens up browser
  2. Executes your tests
  3. 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,

  1. Main: Paste the lines of code which you are interested in running unitly.
  2. 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
  3. 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.

要查看或添加评论,请登录

Sagar Shroff的更多文章

  • Testing LLM Query Outputs with Cosine Similarity

    Testing LLM Query Outputs with Cosine Similarity

    Introduction Few weeks ago, I was pondering on the thought on how to effectively test LLM based application features…

    5 条评论
  • TF-IDF Technique Overview

    TF-IDF Technique Overview

    I am currently learning about feature-engineering, and today I explored the TF-IDF technique. I decided to write it up…

    1 条评论
  • Java - Metaprogramming - Ability to add new functionality to existing API

    Java - Metaprogramming - Ability to add new functionality to existing API

    Ever came across scenario where you wished you could possibly add new functionality to an existing Java API? i.e say…

    2 条评论
  • Approaching test automation development

    Approaching test automation development

    In today's fast-paced product development, test-automation is one of the key to drive the organization's ability to…

    7 条评论
  • How to correctly implement a RetryAnalyzer in TestNG

    How to correctly implement a RetryAnalyzer in TestNG

    A very common requirement while automating testing of a product is you may wish to implement a retry mechanism for a…

    12 条评论
  • Working along with Groovy Closures

    Working along with Groovy Closures

    Closures, they play a very key role in Groovy vocabulary. They are used everywhere in groovy API.

社区洞察

其他会员也浏览了