TestNG Parallel Tests using Data Provider

ぃ、小莉子 提交于 2019-12-24 08:09:16

问题


I would like to simulate Google Search with various search parameters in parallel using Selenium and TestNG. Below are my test class and testng.xml. I have tested my test clases using below annotations @Test(dataProvider="googlesearchDataProvider",threadPoolSize=3,singleThreaded=false) or testng.xml. Both the cases tests ran in single browser single threaded. Could you please let me know what is wrong and what needs to be done. I want at a time there will be 3 independent browser instances running in parallel with different search data taking it from data provider.

package com.test.google.search;

import static org.testng.Assert.fail;

import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.aig.testframework.Driver;

/**
 * @author dpoddar
 *
 */
public class GoogleSearch {

    private WebDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();

    //@BeforeClass(alwaysRun = true)
    @BeforeTest(alwaysRun = true)
    public void setUp() throws Exception {
        Driver.getInstance().setDriver("firefox", "Windows 7", "local");
        driver =  Driver.getInstance().getDriver(); 

        baseUrl = "https://www.google.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test(dataProvider="googlesearchDataProvider",threadPoolSize=3,singleThreaded=false)
    public void testGoogleSearch(String serachParam) throws Exception {
        System.out.println("testGoogleSearch. Thread id is: " + Thread.currentThread().getId());
        driver.get("https://www.google.com/");
        driver.findElement(By.name("q")).clear();
        driver.findElement(By.name("q")).sendKeys(serachParam);
        driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
    }

    //@AfterClass(alwaysRun = true)
    @AfterTest(alwaysRun=true)
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }


    @DataProvider(name = "googlesearchDataProvider")
    public Object[][] createData(Method m) {
      System.out.println(m.getName());  // print test method name
      return new Object[][] { new Object[] { "Test"},{"google"},{"Java"},{"Spring"},{"AWS"},{"Market Trends"},{"Hotels Near 91367"},{"Starbucks"}};
    }
}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="instances" thread-count="3">
  <test name="Regression 1" >
    <classes>
      <class name="com.test.google.search.GoogleSearch"/>
    </classes>
  </test>
</suite>

回答1:


I solved this using @Factory and changes in testng.xml. Changes are below.

Please use the other methods form the problem statement.

public class GoogleSearch {
 private String searchParam;
 @Factory(dataProvider = "googlesearchDataProvider",enabled=true)
 public GoogleSearch(String searchParam) {
      this.searchParam = searchParam;
  }

 @Test
 public void testGoogleSearch() throws Exception {
        System.out.println("testGoogleSearch with "+searchParam+". Thread id is: " + Thread.currentThread().getId());
        driver.get("https://www.google.com/");
        driver.findElement(By.name("q")).clear();
        driver.findElement(By.name("q")).sendKeys(searchParam);
        driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
    }
}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" verbose="2">
  <test name="Regression 1" parallel="instances" thread-count="3">
    <classes>
      <class name="com.test.google.search.GoogleSearch"/>
    </classes>
  </test>
</suite>


来源:https://stackoverflow.com/questions/54541861/testng-parallel-tests-using-data-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!