2011-08-17 64 views
1

我已經用selenium 1成功創建了一個數據驅動的框架,並試圖使用selenium 2(WebDriver)來做同樣的事情。我正在做一些基本的R和D.我的代碼如下。有沒有辦法在WebDriver上使用testNG來進行數據驅動測試?

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運行測試時打開並關閉。任何人都可以提出一個正確的方法去做或如何使這項工作。

回答

2

只是做輕微的修正案UR 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; 
    } 
}} 

現在它會正常工作..

相關問題