parallel testing code example

Example 1: what is parallel testing

--Parallel Execution: In parallel testing multiple browsers are
invoked simultaneously and all the test scripts are executed
parallelly.

Example 2: parallel testing in testng

I achieve it with xml file.

In TestNG is quite simple to do parallel testing. Let's say we have our test
cases in different classes or packages.
First I create TestNG xml runner.
Then I create suites or test tags in the xml runner. In these tag, I point to
package or class I want to run.
Then I add and option like parallel="test" into pom.xml, it means all tests
will start running in parallel.

Inside suite<> tag there are 3 attribute which is name,parallel and 
thread count.

name is mandatory and should be unique, it has parallel because we want to run 
different threads in this suite, we have thread count which is number of 
threads to use.

Multi Browser with parallel?
There is parameter annotation where I define name and value;
In TestNG we can pass browser type from xml runner. In our java code, we use 
@parameters annotations to read this browser type parameter.

@Test
@Parameters("browser")
public void parameterized(String browser){
if(browser.equals("chrome")){
sout("open chrome")
}else if...
}

How to check if your TestNG runs in parallel
	inside methods you can use ==> Thread.currentThread.getId()
	should have multiple different thread ids

Example 3: parallel testing

To run parallel testing in Junit
We are using maven-surefire plugin
in pom.xml file.
We can specify how many threads we
want to run as at the same time.

We also have to make some changes 
in Driver class. We need to use 
"ThreadLocal<WebDriver> instead of
"WebDriver" Because "WebDriver" 
generates only a single WebDriver object, 
but "ThreadLocal<WebDriver>" generates
multiple browser to run parallel testing.


 <testFailureIgnore>TRUE</testFailureIgnore>
     <parallel>METHODS</parallel>
     <threadCount>40</threadCount> 
     <forkCount>2C</forkCount>
     <perCoreThreadCount>false</perCoreThreadCount>
     <include>**/*Runner*.java</include>

*Runner* - any test class Contains Runner
 Runner* - any test class starts with Runner
*Runner  - any test class ends with Runner 

PerCoreThreadCount: set false it will
try to match number of threads with number
of CPU cores. Regardsless on threadCount value.

Tags:

Misc Example