[Selenium+java] Parallel Execution & Session handling in Selenium

Source: Internet
Author: User
Tags goto testng guru99

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 have to interact with the browser all the time to execute given commands. At the time of execution, it was also possible that, before current execution completes, someone else starts execution of a nother script, in the same machine and in the same type of browser.

In such situation, we need a mechanism by which we have a different executions should not overlap with each other. This can is achieved using Session handling in Selenium.

How to achieve Session handling in Selenium webdriver?

If you check the source code of Selenium Webdriver, you'll find a variable named as ' SessionId '. Whenever we create a new instance of a Webdriver object, a new ' sessionId ' would be generated and attached with that partic Ular firefox/chrome/ie Driver ().

So anything we have the this would execute only on that particular Firefox browser session.

As this was an in-built functionality, there are no explicit need to assign the session ID

Code Example: Here, different sessions would be generated for the 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 is situations where you want to run multiple tests at the same time.

In such cases, one can use "parallel" attribute

The parallel attribute of the suite tag can accept four values:

Tests All the test cases inside <test> tag of testing XML file would run parallel.
Classes All the test cases inside a Java class would run parallel
Methods All the methods with @Test annotation would execute parallel.
Instances Test cases in same instance would execute parallel but both methods of the different of the would run in instances thread.

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

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

The complete project would look like:

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 the order and dependency of Test case execution.

Suppose you have both test cases, ' testguru99tc1 ' and ' testguru99tc2 ' and you want to the Execute test case ' TESTGURU99TC2 ' is Fore ' testguru99tc1 '. In this case we'll use the ' 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 would 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

[Selenium+java] Parallel Execution & Session handling in Selenium

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.