使用数据提供程序进行TestNG并行测试

时间:2019-02-05 19:39:24

标签: selenium selenium-webdriver testng testng-dataprovider

我想使用Selenium和TestNG并行模拟具有各种搜索参数的Google搜索。下面是我的测试类和testng.xml。我已经使用以下注释@Test(dataProvider="googlesearchDataProvider",threadPoolSize=3,singleThreaded=false)或testng.xml测试了我的测试类。两种情况下的测试都在单线程单浏览器中运行。您能否让我知道哪里出了问题以及需要做什么。我希望一次有3个独立的浏览器实例与不同的搜索数据并行运行,并从数据提供者那里获取数据。

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 个答案:

答案 0 :(得分:0)

我使用@Factory解决了这个问题,并更改了testng.xml。更改如下。

请使用问题说明中的其他方法。

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>