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

发布于 2024-11-30 01:28:30 字数 2027 浏览 2 评论 0原文

我已经使用 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 将打开和关闭。任何人都可以建议一种正确的方法来解决这个问题或如何使这项工作发挥作用。

I have successfully created a data driven framework with selenium 1 and trying to do the same using selenium 2 (WebDriver). I was doing some basic R and D. My code is as below.

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;
    }
}

}

But with this code, the test is not running. Firefox opens and closes while running the test as testNG. Can anyone suggest a proper way to go about it or how to make this work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城缀染半城烟沙 2024-12-07 01:28:30

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

现在它可以正常工作了..

just do a slight amendment in ur createData() i.e

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;
    }
}}

now it will work fine..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文