Friday, 5 June 2015

Parallel Execution of Tests in Different Browser using TestNG

For parallel excution of tests in different browsers:

To accomplish this task, We need two files:

1. Leads.java
2. testng.xml

Please find below the code for Leads.java:



import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.ie.InternetExplorerDriver;

import org.testng.annotations.AfterClass;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;



public class Leads {

    public WebDriver driver;

    @Parameters("browser") 

      // Passing Browser parameter from TestNG xml

      @BeforeClass

      public void beforeTest(String browser) {

      // If the browser is Firefox, then do this

      if(browser.equalsIgnoreCase("firefox")) {

      driver = new FirefoxDriver();

      // If browser is IE, then do this 

      }else if (browser.equalsIgnoreCase("ie")) {

      // Here I am setting up the path for my IEDriver

      System.setProperty("webdriver.ie.driver", "C:\\chrome\\IEDriverServer.exe");

      driver = new InternetExplorerDriver();

      }else if (browser.equalsIgnoreCase("chrome")) {

     

      // Here I am setting up the path for my ChromeDriver

      System.setProperty("webdriver.chrome.driver", "C:\\chrome\\chromedriver.exe");

      driver = new ChromeDriver();

      }

      // Doesn't the browser type, lauch the Website

      driver.get("http://localhost:100/");

      }

    @Test

    public void leads() throws InterruptedException

    {

        driver.findElement(By.name("user_name")).sendKeys("admin1");

        driver.findElement(By.name("user_password")).sendKeys("admin1");

        driver.findElement(By.name("Login")).click();

        Thread.sleep(3000);

        driver.findElement(By.name("user_name")).clear();

        driver.findElement(By.name("user_name")).sendKeys("admin");

        driver.findElement(By.name("user_password")).clear();

        driver.findElement(By.name("user_password")).sendKeys("admin");

        driver.findElement(By.name("Login")).click();

        Thread.sleep(3000);

        driver.findElement(By.linkText("New Lead")).click();

        Thread.sleep(3000);

        driver.findElement(By.name("lastname")).sendKeys("ATH");

        driver.findElement(By.name("company")).sendKeys("ATH");

        driver.findElement(By.name("button")).click();

        Thread.sleep(3000);

       

    }

    @AfterClass

    public void afterClass()

    {

        driver.close();

        driver.quit();

    }


}

Please find below the code for testng.xml:



<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="zellaxSolutions" parallel="tests">


<test name="Leads with Firefox" >

<parameter name="browser" value="firefox" />

    <classes>

        <class name="Leads" ></class>         

    </classes>   

</test>



<test name="Leads with IE">

<parameter name="browser" value="ie" />

    <classes>

        <class name="Leads" ></class> 

    </classes>

</test>


<!--  <test name="Leads with Chrome">

<parameter name="browser" value="chrome" />

    <classes>

        <class name="Leads" ></class> 

    </classes>

</test>-->

</suite>

No comments:

Post a Comment