Thursday, April 6, 2017

Parallel execution of scripts with TestNG

Parallel execution of scripts with TestNG


TestNG gives us the facility to execute multiple tests in parallel and two different methods of doing it are:

Method 1: Having multiple test method in one class file

Step 1: Create a class file with name “DummyTest1” and add following code in it.

public class DummyTest1

{

       public WebDriver driver;

  @Test
  public void main()
  {
     driver.findElement(By.name("userName")).sendKeys("mercury");
     driver.findElement(By.name("password")).sendKeys("mercury");
     driver.findElement(By.name("login")).click();
  }
 
  @Test
  public void main1()
  {
     driver.findElement(By.name("userName")).sendKeys("mercury");
     driver.findElement(By.name("password")).sendKeys("mercury");
     driver.findElement(By.name("login")).click();
  }

  @BeforeMethod

  public void beforeMethod()
  {
      driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.get("http://newtours.demoaut.com/");
  }

  @AfterMethod

  public void afterMethod()
  {
       driver.quit();
  }

}

Step 2: Add following code in testng.xml file. Make sure you add the right class name.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
  <test name="Regression 1">
    <classes>
      <class name="AutomationFrameworkForDummy.DummyTest1"/>
    </classes>
  </test>
</suite>


Step 3: Run it and you will see multiple browser window in parallel.

Method 2:  Having two test method in two class file.

Step 1: Create a class file and add following code in it.

public class Chrome_Search {
    WebDriver driver;
    
    @BeforeTest
       @Parameters("browser2")
     public void test1(String browser2)
     {
              if(browser2.equalsIgnoreCase("chrome"))
              {
                     System.setProperty("webdriver.chrome.driver", "E://Rajesh Document//AutomationFrameworks//WordPressAutomation//Drivers//chromedriver.exe");
                       driver =new ChromeDriver();
                       driver.get("www.google.com");
                      
                 } 
              else
              {
                     System.out.println("Google Chrome"); 
              }
         }
  
   }

Step 2: Create another class file and add following code in that class file.

public class Mozilla_OpenAmazon {
    WebDriver driver;
       @BeforeTest
       @Parameters("browser1")
    public void launchbrowser(String browser1)
     {
              if(browser1.equalsIgnoreCase("firefox"))//For Firefox we dont need to download any jar file as we have Inbuilt firefox jar files
              {
                     driver=new FirefoxDriver(); 
                  System.out.println("Hello Google..."); 
                  driver.get("https://www.amazon.com/");
                  driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Dolly");
                                                  
           }
              else
              {
                     System.out.println("Firefox"); 
              }
     }
              @AfterTest
              public void closeBrowser()
              {
                     driver.quit();
              }
}

Step 3: Add following code in testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  <suite name="Suite" parallel="tests">

  <test name="FirefoxTest" >
  <parameter name = "browser1" value="Firefox"/>
  <classes>
  <class name ="AutomationFrameworkForDummy.Mozilla_OpenAmazon"> </class>
  </classes>
  </test>
   
 
  <test name ="Chrome Test">
    <parameter name = "browser2" value="Chrome"/>
   <classes>
  <class name ="AutomationFrameworkForDummy.Chrome_Search"/>
  </classes>
   </test>
   </suite>


Step 4: Now Run it.

No comments:

Post a Comment