About - Selenium

Definition

Development and Versions

Pre-requisites

Languages supported

Manual vs Automation

Choosing Selenium

Selenium - Components

Selenium IDE

Selenium RC

Selenium WebDriver

Selenium Grid

QTP

Selenium vs QTP

Selenium - IDE

Features

Install and configure

Selenium - Webdriver

About WebDriver

install and configure

First test case

Web element

Element Locators

Example of locators

Chropath

Web driver commands

Eclipse and TestNG

About Eclipse

Choose and download

About TestNG

Testng installation on eclipse

Testng Annotations

Pass Fail and Skip test cases

Run test cases in batch

testng.xml - Explanation

testng grouping

Running tests parallely

dependsOnMethods

dependsOnGroups

Prioritization

Passing parameters

Data providers

Read data from excel

Ant - XSLT reports

Testng listeners

Cross browser testing

Page Object Model



Selenium - Running tests in parallel
 
1) Suppose we have a class like below:

Public class SeleniumParallelTest(){

WebDriver driver = null;
@BeforeMethod
Public void startBrowser(){
driver= new FireFoxDriver();

}
@AfterMethod
Public void stopBrowser(){
driver.close()
}


@Test
Public void test1(){
driver.get("https://www.amazon.in/")
driver.findElement(By.name(Amazon Pay)).click();
Thread.sleep("4000");

}

@Test
Public void test2(){
driver.get("https://www.amazon.in/")

driver.findElement(By.name(Amazon Pay)).click();
Thread.sleep("4000");
}

@Test
Public void test3(){
driver.get("https://www.amazon.in/")
driver.findElement(By.name(Amazon Pay)).click();
Thread.sleep("4000");
}

}

Suppose the testng.xml file looks like below:
<Suite name="ParallelSuite" parallel="methods" thread-count="3">

<Test name="parallelMethods" >
<Classes>
< Class name="package. SeleniumParallelTest">
</Classes>
</Test>
</Suite>




By running this we expect that all the three tests run successfully.

But only 1 test will pass and other two will fail. This is because of common driver instance. After one driver is created by one thread, if another test runs in parallel, the driver object is propagated to second thread and now it is not available to the other threads.

To resolve this issue we need to use TheadLocal class. Lets see how we can do so ...
ThreadLocal<WebDriver> dr=new ThreadLocal<WebDriver>;


and then the code looks like below for Before Method and After method.

@BeforeMethod
Public void startBrowser(){
driver= new FireFoxDriver();
dr.set(driver);
}

@AfterMethod
Public void stopBrowser(){
dr.get().close();
dr.set(null);
}




Now the above code executes successfully with all the three test methods running in parallel.

Suppose, we set thread-count="2"> and run the above suite, execution will be as follows.

First, only any two of the threads get executed and if one of them is finished (means one of the thread is free), then the other thread starts executing.


2) Suppose there is another class (AnotherClassFile) with another two test methods inside it.

And if the testng.xml looks like below:
<Suite name="ParallelSuite" parallel="methods" thread-count="3">

<Test name="parallelMethods" >
<Classes>
< Class name="package. SeleniumParallelTest">
< Class name="package. AnotherClassFile">
</Classes>
</Test>
</Suite>


In the above scenario, there are 5 tests totally in both classes. Now if we run above test suite, any three of the test cases in any of the classes gets executed first, and after one thread is finished and the thread is free other methods will execute.

This applies to all methods in all classes as parallel="methods" is marked at the suite level which means parallel is applicable to the entire suite.


3) Now within the same scenario where there are 2 classes, there is a provision for running test cases in parallel at class level. Just replace methods with classes and thead-count to 2 as below.

<Suite name="ParallelSuite" parallel="Classes" thread-count="2">

<Test name="parallelClasses" >
<Classes>
<Class name="package. SeleniumParallelTest">
<Class name="package. AnotherClassFile">

</Classes>
</Test>
</Suite>


If we run the above suite, one thread is assigned to one class and another thread is assigned to another class.

There will be only two thread instances for two classes running, where one thread will execute test methods under one class another will execute test methods uner another class.

Now consider a scenario where there are 2 classes in two different tests as below. One class in one test tag and another class in another test tag as below. And we want to run these test level classes in parallel, we have a provision to do so by making parallel value replacing with "tests"
<Suite name="ParallelSuite" parallel="tests" thread-count="2">

<Test name="parallelMethods1" >
<Classes>
<Class name="package. SeleniumParallelTest">
</Classes>
</Test>
<Test name="parallelMethods2" >
<Classes>
<Class name="package. AnotherClassFile">

</Classes>
</Test>
</Suite>


If we run the above suite, one thread is assigned to one test tag at test level and another thread is assigned to another test tag at test level.

There will be only two thread instances for two tests running, where one thread will execute test methods under one test level another will execute test methods uner another test level.

There is another way to do parallel execution by making @DataProvider (parallel=true)


I conduct online tutions on Manual Testing, Selenium and the following. For more details contact anandaibmindia@gmail.com
Manual Testing - 1600/-           Selenium - 1600/-
           Jsp and Servlet -2000/-           Java training - 2000/-