1

我已经成功地使用 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;
    }
}

}

但是使用此代码,测试未运行。Firefox 在以 testNG 身份运行测试时打开和关闭。任何人都可以提出一个正确的方法来解决它或如何使这项工作。

4

1 回答 1

2

只需在您的 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;
    }
}}

现在它会正常工作..

于 2011-08-24T06:41:30.463 回答