2011-08-17 1 views
1

J'ai réussi à créer une infrastructure basée sur les données avec du sélénium 1 et essayant de faire de même avec du sélénium 2 (WebDriver). Je faisais de la R et D. Je code comme ci-dessous.Existe-t-il un moyen d'utiliser testNG avec WebDriver pour effectuer des tests pilotés par les données?

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

}

Mais avec ce code, le test ne fonctionne pas. Firefox s'ouvre et se ferme en exécutant le test en tant que testNG. Quelqu'un peut-il suggérer une bonne façon de s'y prendre ou comment faire ce travail.

Répondre

2

Il suffit de faire une légère modification dans 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; 
    } 
}} 

maintenant il fonctionne très bien ..

Questions connexes