java - 有没有办法将 testNG 与 WebDriver 一起使用来进行数据驱动测试?

标签 java testng data-driven-tests selenium-webdriver

我已经使用 selenium 1 成功创建了一个数据驱动框架,并尝试使用 selenium 2 (WebDriver) 执行相同的操作。我正在做一些基本的研发。我的代码如下。

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;
import jxl.*;

@SuppressWarnings("unused")
public class FirstTestusingWebDriver {
private WebDriver driver;
private String baseUrl="http://www.google.co.in";
private StringBuffer verificationErrors = new StringBuffer();
@BeforeClass
public void setUp() throws Exception {
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@DataProvider(name="DP")
Object[] createData(){
    String[] testData = {"Cheese", "Sudeep"};
    System.out.println("Data is getting created");
    return testData;
}

@Test(dataProvider="DP")
public void testUntitled(String testData) throws Exception {
    driver.get("http://www.google.co.in/");
    driver.findElement(By.id("lst-ib")).clear();
    driver.findElement(By.id("lst-ib")).sendKeys(testData);
    driver.findElement(By.name("btnG")).click();
    driver.findElement(By.linkText("Cheese - Wikipedia, the free encyclopedia")).click();
}

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

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}

}

但是使用此代码,测试未运行。作为 testNG 运行测试时,Firefox 将打开和关闭。任何人都可以建议一种正确的方法来解决这个问题或如何使这项工作发挥作用。

最佳答案

只需在你的 createData() 中稍加修改即可,即

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;


@SuppressWarnings("unused")
public class FirstTestusingWebDriver {
private WebDriver driver;
private String baseUrl="http://www.google.co.in";
private StringBuffer verificationErrors = new StringBuffer();
@BeforeClass
public void setUp() throws Exception {
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@DataProvider(name="DP")
Object[][] createData(){
    String[] testData = {"Cheese", "Sudeep"};
    System.out.println("Data is getting created");
    return new Object[][]{{testData[0]+testData[1]}};
}

@Test(dataProvider="DP")
public void testUntitled(String testData) throws Exception {
    driver.get("http://www.google.co.in/");
    driver.findElement(By.id("lst-ib")).clear();

    driver.findElement(By.id("lst-ib")).sendKeys(testData);
    driver.findElement(By.name("btnG")).click();
    //driver.findElement(By.linkText("Cheese - Wikipedia, the free encyclopedia")).click();
}

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

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}}

现在一切正常了..

关于java - 有没有办法将 testNG 与 WebDriver 一起使用来进行数据驱动测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7092429/

相关文章:

java - 如何部署将 javaagent 指向 Bluemix CloudFoundry 的独立 jar

java - 分析每个类的垃圾收集对象实例数

selenium - 在 TestNG XML 文件中实现范围报告时出现 java.lang.ClassCastException 错误

ios - 如何在 Xcode 中执行数据驱动测试

java - 如何在 Groovy 中使用 SOAPUI 类

java - 如何创建动态对象列表

java - 纹理添加黑色和白线

java - 本地方法没有更新我的模拟类的实例变量

javascript - Selenium 测试未正确失败 - 通过但配置失败而不是 "proper"失败

java - 使用 Excel 中的 Selenium WebDriver 填写 Web 表单