Friday 5 February 2016

Parallel execution in selenium webdriver

Parallel execution in selenium web driver using testng:
==========================================
When ever you want to run scripts parallely we can take support from testng.
By using parallel attribute we can run scripts parallely.

ex:
----
<suite name="Suite" thread-count="3" parallel="methods">

parallel:
============

The parallel attribute can accept the following attributes as well.
1.parallel="tests"
====================
All the test cases inside <test> tag of testing xml file will run parallel.

2.parallel="classes"
=====================
All the test cases inside a java class will run parallel.

3.parallel="methods"
=======================
All the methods with @Test annotation will execute parallel.


thread-count:
==============
The attribute thread-count allows you to specify how many threads should be allocated for this execution.


thread-count always number as an argument in the form of String.

//Samplle example on parallel execution:
===============================
ParallelRunDemo.java
------------------------------

package testngdemo;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;

public class ParallelRunDemo {

WebDriver webDriver;

@Test
public void testScript() throws InterruptedException 
{

System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
   webDriver = new ChromeDriver();
//webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati")).click();
webDriver.quit();
}
@Test
public void testScript1()
{

System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
   webDriver = new ChromeDriver();
//webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
webDriver.get("rameshselenium.blogspot.in");
webDriver.findElement(By.linkText("Parallel execution in selenium webdriver")).click();
webDriver.quit();
}
@Test
public void testScript2()
{
System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
   webDriver = new ChromeDriver();
//webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
webDriver.get("seleniumhq.org");
webDriver.quit();
}
}

testng.xml:
---------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" thread-count="3" parallel="methods">
  <test name="Test">
    <classes>
      <class name="testngdemo.ParallelRunDemo"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
















No comments:

Post a Comment