[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.

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 ().

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

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

The parallel attribute of suite tag can accept four values:

tests All the test cases inside <test> tag ofTestingxml file will run parallel.
classes All the test cases inside aJavaclass 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:

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 ofTest Caseexecution.

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

原文地址:https://www.cnblogs.com/alicegu2009/p/9083064.html

时间: 2024-10-07 00:03:11

[Selenium+Java] Parallel Execution & Session Handling in Selenium的相关文章

[Selenium+Java] Implicit Wait &amp; Explicit Wait in Selenium

https://www.guru99.com/handling-dynamic-selenium-webdriver.html here are two types of HTML tables published on the web- Static tables: Data is static i.e. Number of rows and columns are fixed. Dynamic tables: Data is dynamic i.e. Number of rows and c

[Selenium+Java] Listeners and their use in Selenium WebDriver

Original URL: https://www.guru99.com/listeners-selenium-webdriver.html TestNG Listeners in Selenium WebDriver There are two main listeners. WebDriver Listeners TestNG Listeners In this tutorial, we will discuss onTestngListeners. Here is what you wil

Selenium+Java 环境搭建

从事开发工作一年,测试工作三年,一直希望能够做自动化方面的测试,但因为各种缘由一直没做成,终于有时间自己学学.因为有一些java基础,所以从Selenium+Java开始. 搭建Selenium+Java环境过程发生很多问题,主要是浏览器版本和selenium jar包不兼容问题,在此做个总结. 先把所有需要的文件准备好: 1.jdk,可以直接官网下载,我这里是1.7  链接:http://pan.baidu.com/s/1dDDdAcp 密码:mt98 2.eclipse,可以直接官网下载  

[Selenium+Java] Handling AJAX Call in Selenium Webdriver

Original URL: https://www.guru99.com/handling-ajax-call-selenium-webdriver.html Handling AJAX Call in Selenium Webdriver Ajax is a technique used for creating fast and dynamic web pages. This technique is asynchronous and uses a combination of Javasc

[Selenium+Java] Selenium Grid Tutorial: Command Line and JSON Example

Original URL: https://www.guru99.com/introduction-to-selenium-grid.html What is Selenium Grid? Selenium Grid is a part of the Selenium Suite that specializes in running multiple tests across different browsers, operating systems, and machines in para

[Selenium+Java] TestNG: Execute multiple Test Suites

Original URL: https://www.guru99.com/testng-execute-multiple-test-suites.html TestNG: Execute multiple Test Suites TestNG enables you to run test methods, test classes and test cases in parallel inside your project. By performing parallel execution,

自动化测试【Maven+Eclipse+Selenium+Java环境搭建和测试】

一.下载必要的文件 1.eclipse Eclipse官网 2.jdk jdk官网 3.selenium IDE.Selenium Server.Selenium Client Drivers(Java)等等 Selenium下载地址  备注:需要代理服务器才能下载 我使用的是太太猫 4.maven安装.配置等 二.安装    1.Eclipse解压缩就可以用了    2.jdk安装.配置变量等    3.Selenium相关的安装    4.maven 最新版本的Eclipse已经自带mave

[Selenium+Java] Selenium Framework: Keyword Driven &amp; Hybrid

Original from: https://www.guru99.com/creating-keyword-hybrid-frameworks-with-selenium.html What is Selenium Framework? Selenium Framework is a code structure that helps to make code maintenance easy. Without frameworks, we will place the “code” as w

[Selenium+Java] https://www.guru99.com/using-robot-api-selenium.html

Original URL: https://www.guru99.com/using-robot-api-selenium.html Robot Class in Selenium Webdriver Why Robot Class? In certain Selenium Automation Tests, there is a need to control keyboard or mouse to interact with OS windows like Download pop-up,