`

Selenium 29:How to Speed up Test Execution Using Selenium Grid

 
阅读更多

http://www.softwaretestinghelp.com/selenium-grid-selenium-tutorial-29/

 

We are now close to the end of this comprehensive Selenium tutorials series. Next week, we will conclude this online Selenium Training series with “effort estimation of Selenium Projects” and “Selenium Interview questions and answers” tutorials.

Today, in this tutorial we will introduce you with Selenium Grid –  adistributed test execution environment to speed up the execution of a test pass.

What is need of Selenium Grid?

As you go through entire Selenium WebDriver tutorials you will find out WebDriver will execute your test cases in a single machine.

Selenium GRID

Here are few problems with such a setup:

  1. What if you want to execute your test cases for different Operating Systems?
  2. How to run your test cases in different version of same browser?
  3. How to run your test cases for multiple browsers?
  4. Why a scenario should wait for execution of other test cases even if it does not depend upon any test cases?

All these problems are addressed in Selenium GRID.

As we proceed with the Selenium course, we will get the idea about how we can overcome to these problems. Basically Grid architecture is based on master slave architecture. Master machine distributes test cases to different slave machines.

There are 2 versions of Grid available. Selenium Grid 2.0 is the latest from Selenium. Selenium 1.0 was the earlier version. Most of the Selenium experts prefer using Selenium Grid 2.0 as it is packed with new features. Selenium Grid 2.0 supports both Selenium RC and Selenium WebDriver scripts.

Benefits of Selenium Grid:

  1. Selenium Grid gives the flexibility to distribute your test cases for execution.
  2. Reduces batch processing time.
  3. Can perform multi browser testing.
  4. Can perform multi OS testing.

Basic terminology of Selenium Grid:

Hub: Hub is central point to the entire GRID Architecture which receives all requests. There is only one hub in the selenium grid. Hub distributes the test cases across each node.

Node: There can be multiple nodes in Grid. Tests will run in nodes. Each node communicates with the Hub and performs test assigned to it.

Selenium grid Tutorial 1

Install Selenium GRID

Step 1: Download Selenium Server jar file from Selenium’s official website which is formerly known as Selenium RC Server and save it at any location on the local disk.

URL of selenium HQ: http://www.seleniumhq.org/download/

Step 2: Open command prompt and navigate to folder where the server is located. Run the server by using below command

java -jar selenium-server-standalone-2.41.0.jar -role hub

The hub will use the port 4444 by default. This port can be changed by passing the different port number in command prompt provided the port is open and has not been assigned a task.

Status can be checked by using the web interface:http://localhost:4444/grid/console

Step 3: Go to the other machine where you intend to setup Nodes. Open the command prompt and run the below line.

1 java -jar selenium-server-standalone-2.41.0.jar -role node -hub
2 http://localhost:4444/grid/register -port 5556

Run the selenium server in other machines to start nodes.

Selenium grid Tutorial 2

Browser and Nodes:

After starting hub and nodes in each machine when you will navigate to GRID Console

You will find 5 Chrome, 5 Firefox and 1 IE browser under Browser section like below.

Selenium grid Tutorial 3

This indicates that by default you can use 5 Chrome, 5 Firefox and 1 IE browser.

For Instance if you want to use only IE you can start the node by using below command:

1 java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
2 http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore

Verify the browser Type along with other details in GRID Console by clicking on view config.

Selenium grid Tutorial 4

Similarly for Firefox:

1 java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
2 http://localhost:4444/grid/register -port 5556 -browser browserName=firefox

Selenium grid Tutorial 5

For Chrome:

1 java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
2 http://localhost:4444/grid/register -port 5556 -browser browserName=chrome

Selenium grid Tutorial 6

There are few scenarios where you may need browser from each type i.e.: IE, Chrome and Firefox.

For instance you may need to use 1 IE and 1 Firefox and 1 Chrome browser

Selenium grid Tutorial 7

1 java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
2 http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore
3 -browser browserName=firefox -browser browserName=chrome

maxInstances:

maxInstance is used to limit the number of browser initialization in a node.

For example if you want to work with 2 Firefox and 2 IE then you can start the node using maxInstance.

1 java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
2 http://localhost:4444/grid/register -port 5556 -browser browserName=firefox,maxInstance=3

Maximum instance can be verified under configuration tab.

Selenium grid Tutorial 8

Similarly other browser instances can be configured using maxInstances.

maxSession:

maxSession is used to configure how many number of browsers can be used parallel in the remote system.

1 java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
2 http://localhost:4444/grid/register -port 5556 -browser browserName=chrome,maxInstance=3
3 -browser browserName=firefox,maxInstance=3 –maxSession 3

Similarly you can start multiple nodes and configuration can be verified in the console.

NODE1:

Selenium grid Tutorial 9

NODE2:

Selenium grid Tutorial 10

------------

Sample Grid Code:

Here I have used TestNG to run sample GRID test case.

Prerequisite: Create Hub and nodes as explained earlier and TestNG should be configured in eclipse.

Here I have taken sample test to login to Gmail and enter username and password

1 public class GridExample {
2           @Test
3           public void mailTest() throwsMalformedURLException{
4                    DesiredCapabilities dr=null;
5                    if(browserType.equals("firefox")){
6                    dr=DesiredCapabilities.firefox();
7                    dr.setBrowserName("firefox");
8                    dr.setPlatform(Platform.WINDOWS);
9                    }else{
10                             dr=DesiredCapabilities.internetExplorer();
11                             dr.setBrowserName("iexplore");
12                             dr.setPlatform(Platform.WINDOWS);
13                    }
14                    RemoteWebDriver driver=newRemoteWebDriver(new    URL("http://localhost:4444/wd/hub"), dr);
15                    driver.navigate().to("http://gmail.com");
16                    driver.findElement(By.xpath("//input[@id='Email']")) .sendKeys("username");
17                    driver.findElement(By.xpath("//input[@id='Passwd']")) .sendKeys("password");
18                    driver.close();
19 }

As in the example you have to use RemoteWebDriver if you are using GRID and you have to provide capabilities to the browser. You have to set the browser and platform as above.

In this example I have used platform as WINDOWS. You can use any platform as per your requirement.

Version of browser can also be set by using dr.setVersion(“version”)

For Instance you need to run this test serially in multiple browsers you have to configure your testng.xml .Below is the testng.xml suite for above test to run your test serially.

1 <?xml version="1.0" encoding="UTF-8"?>
2 <suite name="GRID SAMPLE TEST" thread-count="2">
3     <test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER IE">
4     <parameter name ="browserType" value="IE"/>
5         <classes>
6             <class name ="GridExample"/>
7         </classes>
8     </test>
9  
10     <test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER FF ">
11     <parameter name ="browserType" value="firefox"/>
12         <classes>
13             <class name ="GridExample"/>
14         </classes>
15     </test>
16 </suite>

To run the test parallel, you have to change your testng.xml like below.

1 <?xml version="1.0" encoding="UTF-8"?>
2 <suite name="GRID SAMPLE TEST" parllel="tests" thread-count="3">
3     <test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER FF">
4     <parameter name ="browserType" value="firefox"/>
5         <classes>
6             <class name ="GridExample"/>
7         </classes>
8     </test>
9     <test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER IE">
10     <parameter name ="browserType" value="IE"/>
11         <classes>
12             <class name ="GridExample"/>
13         </classes>
14     </test>
15 </suite>

Here in the testng.xml you have to specify parameter asparllel=“tests” and thread-count=“3” describes the maximum number of threads to be executed in parallel.

Configuration using JSON file:

Grid can also be launched along with its configuration by using json configuration file.

Create a json file for having below configuration. Here I have created a json file named as grid_hub.json

1 {
2   "host": null,
3   "port": 4444,
4   "newSessionWaitTimeout": -1,
5   "servlets" : [],
6   "prioritizer": null,
7   "capabilityMatcher":   "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
8   "throwOnCapabilityNotPresent": true,
9   "nodePolling": 5000,
10  
11   "cleanUpCycle": 5000,
12   "timeout": 300000,
13   "maxSession": 5
14 }

Start the hub using below command

java -jar selenium-server-standalone-2.41.0.jar -role hub –hubConfig grid_hub.json

Similarly create different json file for different nodes as per required configuration.

Here is an example of json configuration file for node named as grid_node.json

1 {
2   "capabilities":
3       [
4         {
5           "browserName": "chrome",
6           "maxInstances": 2
7         },
8         {
9           "browserName": "firefox",
10           "maxInstances": 2
11         },
12         {
13           "browserName": "internet explorer",
14           "maxInstances": 1
15         }
16       ],
17     "configuration":
18         {
19         "nodeTimeout":120,
20         "port":5555,
21  
22         "hubPort":4444,
23         "hubHost":"localhost",
24  
25         "nodePolling":2000,
26  
27         "registerCycle":10000,
28         "register":true,
29         "cleanUpCycle":2000,
30         "timeout":30000,
31         "maxSession":5,
32         }
33 }

To start the node

java -jar selenium-server-standalone-2.41.0.jar -role rc –nodeConfig grid_node.json

You can change all the configuration of browser, maxInstances, port, maxSession etc. in the json file.

You can provide browser version, platform in the json config file like below

{
   “browserName”: “chrome”,”version”:”8”,”platform”:”Windows”
}

Conclusion:

It is advisable to use Selenium Grid when you have to perform multi browser testing and you have large number of test cases.

In this module we covered how to setup Grid hub and nodes along with how to run Grid test cases using testng.xml and json file.

Next Tutorial #30Automation testing with Selenium and Cucumber toolCucumber is a BDD testing tool and Framework. We will learn features of Cucumber tool and its usage in real time scenario including how to integrate Selenium WebDriver with Cucumber

分享到:
评论

相关推荐

    基于Python的selenium操作:判断元素是否存在+判断元素是否可以点击.zip

    【Python的selenium操作:判断元素是否存在】 在Python的自动化测试中,Selenium是一个非常强大的工具,用于模拟用户与网页的交互。Selenium库提供了一系列API,使得我们可以控制浏览器进行各种操作,例如点击按钮...

    TestAutomationusingSeleniumWebDriverJavaPreview-1.pdf

    综合以上信息,我们可以了解到《Test Automation Using Selenium WebDriver with Java》是一本专注于教授如何使用Java语言结合Selenium WebDriver进行Web应用自动化测试的实用指南。它涵盖了自动化测试的基本概念、...

    Selenium:Selenium Grid.zip

    史上最全软件测试技术全套教程,包括: Postman Selenium 单元测试 压力测试 回归测试 安全测试 性能测试 测试工具 集成测试 等流行技术的系列教程

    selenium私房菜系列

    Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: ... 4.Selenium Grid:允许同时并行地、在不同的环境上运行多个测试任务,极大地加快Web应用的功能测试。

    selenium开发:谷歌浏览器和驱动配套版本

    在进行自动化测试时,Selenium 是一款非常强大的工具,它允许开发者编写脚本来模拟用户对...同时,对于大型项目,可以考虑使用版本管理工具,如Selenium Grid,来管理不同版本的浏览器和驱动,以适应不同的测试需求。

    Playwright 结合 Selenium Grid - windows 环境使用教程.pdf

    Playwright 结合 Selenium Grid - Windows 环境使用教程 本资源提供了 Playwright 结合 Selenium Grid 在 Windows 环境中的使用教程,介绍了如何将 Playwright 连接到 Selenium Grid Hub,启动 Google Chrome 或 ...

    selenium framework design data driven testing

    Users will learn how to design and build a Selenium Grid from scratch to allow the framework to scale and support different browsers, mobile devices, versions, and platforms, and how they can leverage...

    《python3网络爬虫开发实战》学习笔记::selenium——xpath:Unable to locate element

    selenium+firefox在定位时遇到selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: 由于是js加载页面,想确认是否是js的原因,随后进行多次调试时发现“//div”竟然也出现了...

    Selenium.Testing.Tools.Cookbook.2nd.Edition.178439251

    Set up Selenium Grid for faster and parallel running of tests, increasing test coverage and reducing test execution time for cross-browser testing Build extended Selenium WebDriver tests for ...

    selenium grid教程

    ### Selenium Grid 教程 #### 一、简介 Selenium Grid 是一款强大的自动化测试辅助工具,主要用于提升基于 Web 应用程序的测试效率。它能够实现在多台计算机上同时执行多个 Selenium Remote Control (RC) 的任务,...

    Selenium WebDriver Recipes in C#(Apress,2ed,2015)

    How to work with and manage tests and testing using Selenium Remote Control and Selenium Server Audience This book is for experienced .NET and C# Windows application programmers/developers.

    selenium gird 简介

    Selenium Grid 是一个强大的工具,用于扩展 Selenium WebDriver 的功能,特别是在进行分布式自动化测试时。它允许用户在多台机器上并行运行测试,显著提高了测试覆盖率和效率,减少了整体测试时间。下面将详细介绍 ...

    Selenium IDE: PHP Formatters

    用于firefox录制的插件,是php语言的文件导出,使用firefox打开的话下载完成后直接安装。

    chrome 浏览器 selenium IDE插件

    ** Selenium IDE 插件在 Chrome 浏览器中的应用** Selenium IDE 是一款强大的自动化测试工具,专为 Web 应用程序设计。它基于 Firefox 和 Chrome 浏览器的插件形式存在,允许用户录制、编辑和回放浏览器操作,以...

    selenium grid最新版本

    Selenium Grid是一款强大的工具,专为扩展Selenium WebDriver的功能而设计,使得远程控制多台机器上的浏览器成为可能。它的最新版本提供了更为高效和灵活的分布式测试能力,这对于进行大规模的Web应用程序自动化测试...

    selenium-server-grid(包含版本3.14和4.0.0)

    Selenium服务器是运行远程Selenium WebDriver(网格)所必需的。 通过将客户端发送的命令路由到远程浏览器实例,Selenium Grid允许在远程计算机(虚拟或真实)上执行WebDriver脚本。它旨在提供一种在多台计算机上...

    Mastering_Selenium_WebDriver_3.0.epub

    and learn how to quickly spin up a Selenium Grid using Docker containers. In the concluding chapters, you ll work through a series of scenarios that demonstrate how to extend Selenium to work with ...

Global site tag (gtag.js) - Google Analytics