zoukankan      html  css  js  c++  java
  • [Selenium+Java] Parallel Execution & Session Handling in Selenium

    Source URL: https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html

    To understand how to run scripts in parallel, let's first understand

    Why do we need Session Handling?

    During test execution, the Selenium WebDriver has to interact with the browser all the time to execute given commands. At the time of execution, it is also possible that, before current execution completes, someone else starts execution of another script, in the same machine and in the same type of browser.Parallel Execution & Session Handling in Selenium

    In such situation, we need a mechanism by which our two different executions should not overlap with each other. This can be achieved using Session Handling in Selenium.

    How to achieve Session Handling in Selenium WebDriver?

    If you check the source code of Selenium WebDriver, you will find a variable named as 'sessionId'. Whenever we create a new instance of a WebDriver object, a new 'sessionId' will be generated and attached with that particular Firefox/Chrome/IE Driver ().

        Parallel Execution & Session Handling in Selenium

    So anything we do after this will execute only in that particular Firefox browser session.

    Parallel Execution & Session Handling in Selenium

    As this is an in-built functionality, there is no explicit need to assign the session id

    Code Example: Here two different sessions will be generated for two different WebDriver.

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class SessionHandling {
    public static void main(String...strings ){
        //First session of WebDriver
        WebDriver driver = new FirefoxDriver();
        //Goto guru99 site
        driver.get("http://demo.guru99.com/V4/");
        
        //Second session of WebDriver
        WebDriver driver2 = new FirefoxDriver();
        //Goto guru99 site
        driver2.get("http://demo.guru99.com/V4/");
    }
    }

    Run Scripts in Parallel

    There are situations where you want to run multiple tests at the same time.

    In such cases, one can use "parallel" attribute

    Parallel Execution & Session Handling in Selenium

    The parallel attribute of suite tag can accept four values:

    tests All the test cases inside <test> tag of Testing xml file will run parallel.
    classes All the test cases inside a Java class will run parallel
    methods All the methods with @Test annotation will execute parallel.
    instances Test cases in same instance will execute parallel but two methods of two different instances will run in different thread.

    The attribute thread-count allows you to specify how many threads should be allocated for this execution.

          

    Complete Example: In this Example, three test cases will run parallel and fill login data in http://demo.guru99.com

    The Complete project will look like:

    Parallel Execution & Session Handling in Selenium

    TestGuru99MultipleSession.java

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.Test;
    public class TestGuru99MultipleSession {
        @Test    
        public void executSessionOne(){
                //First session of WebDriver
            System.setProperty("webdriver.chrome.driver","chromedriver.exe");
                WebDriver driver = new ChromeDriver();
                //Goto guru99 site
                driver.get("http://demo.guru99.com/V4/");
                //find user name text box and fill it
                driver.findElement(By.name("uid")).sendKeys("Driver 1");
                
            }
            
        @Test    
            public void executeSessionTwo(){
                //Second session of WebDriver
            System.setProperty("webdriver.chrome.driver","chromedriver.exe");
            WebDriver driver = new ChromeDriver();
                //Goto guru99 site
            driver.get("http://demo.guru99.com/V4/");
            //find user name text box and fill it
            driver.findElement(By.name("uid")).sendKeys("Driver 2");
            
            }
            
        @Test    
            public void executSessionThree(){
                //Third session of WebDriver
            System.setProperty("webdriver.chrome.driver","chromedriver.exe");
            WebDriver driver = new ChromeDriver();
                //Goto guru99 site
            driver.get("http://demo.guru99.com/V4/");
            //find user name text box and fill it
            driver.findElement(By.name("uid")).sendKeys("Driver 3");
            
            }        
    }

    TestNG.XML

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="TestSuite" thread-count="3" parallel="methods" >
    <test name="testGuru">
    <classes>
    <class name="TestGuru99MultipleSession">
    </class>
    </classes>
    </test>
    </suite>

    Test Case order and Dependency

    You can set order and dependency of Test Case execution.

    Suppose you have two test cases , 'testGuru99TC1' and 'testGuru99TC2' and you want to execute test case 'testGuru99TC2' before 'testGuru99TC1'. In that case we will use 'dependsOnMethods' attribute to make dependency and order of execution.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="TestSuite" thread-count="3" parallel="methods" >
    <test name="testGuru">
    <classes>
    <class name="TestGuru99MultipleSession">
    <include value="testGuru99TC1" dependsOnMethods=" testGuru99TC2"/>
    <include value="testGuru99TC2"/>
    </class>
    </classes>
    </test>
    </suite>

    Summary

    • A new sessionID is created for a new instance of WebDriver.
    • One session will bind with one particular browser.
    • Using attribute thread and parallel, you run your scripts in parallel.
    • You can use attribute dependency to set the order to test execution
  • 相关阅读:
    Oracle里的执行计划
    Java线程锁总结
    Java面试总结-链接
    oracle 排序函数(转载)
    微软今天的Windows 10硬件发布会汇总:手机瞬间变PC
    DevOps 在公司项目中的实践落地
    阿里云云计算工程师ACP学习笔记--知识点总结
    中小型互联网公司微服务实践-经验和教训
    Prometheus监控的最佳实践——关于监控的3项关键指标
    深度学习的Attention模型
  • 原文地址:https://www.cnblogs.com/alicegu2009/p/9083064.html
Copyright © 2011-2022 走看看