我进行使用硒webdriver的自动化测试通过遍历数组,这个代码是TestNG的dataprovider
,总结:我从Excel工作表中获取数据到数据, 它的正常工作。当我正在调试代码时,我得到TestData为testGoogle1(String search1, String Search2)
第一次迭代它Search1 = Webdriver, Search2 = Qtp, so on,,,,
我想要的是,它应该直接返回值的数组,如testGoogle1(String search[])
,以便在@Test
本身我可以添加我的函数迭代所有行和列并测试它们。TestNG的:使用dataProvider
任何人都可以请给我想法如何写它。
测试数据表
这里是我的代码
package ExcelTest;
import com.thoughtworks.selenium.*;
import static org.testng.AssertJUnit.*;
import java.io.IOException;
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import jxl.*;
public class Sample{
WebDriver driver;
@BeforeTest
public void startTest(){
driver = Startup.basic();
}
@DataProvider(name = "DP1")
public Object[][] createData1() throws Exception{
Object[][] retObjArr=getTableArray("G:\\Selenium Jar Files\\TestData\\Data.xls","DataPool");
return(retObjArr);
}
@Test (dataProvider = "DP1")
public void testGoogle1(String search1, String Search2) throws Exception{
//selenium.open("http://www.google.co.in/");
// driver.get("http://www.google.co.in/");
//String hello = search.length;
//for(int i=0; i< search.length ;i++)
//{
System.out.println("param " +search);
Thread.sleep(3000);
System.out.println("Opened");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys(search);
element.submit();
System.out.println("Clicked");
}
//}
@AfterClass
public void tearDown() throws Exception {
//selenium.stop();
}
public String[][] getTableArray(String xlFilePath,String sheetName) throws Exception{
String[][] tabArray=null;
File inputWorkbook = new File(xlFilePath);
Workbook w;
int startRow,startCol, endRow, endCol,ci,cj;
try {
//w = Workbook.
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(sheetName);
// Loop over first 10 column and lines
endRow = sheet.getRows();
endCol = sheet.getColumns();
tabArray=new String[endRow-1][endCol-1];
ci=0;
for (int i=1;i<endRow;i++,ci++){
cj=0;
for (int j=1;j<endCol;j++,cj++){
Cell cell = sheet.getCell(j, i);
tabArray[ci][cj] = cell.getContents();
}
// System.out.println("");
}
//file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return(tabArray);
}
}
谁能请分享任何想法? 感谢
编辑代码:
public class Sample{
WebDriver driver;
@BeforeTest
public void startTest(){
driver = Startup.basic();
}
@DataProvider(name = "DP1")
public Object[][][] createData1() throws Exception{
Object[][][] retObjArr=getTableArray("G:\\Selenium Jar Files\\TestData\\Data.xls","DataPool");
return (retObjArr);
}
@Test (dataProvider = "DP1")
public void testGoogle1(String search, String het) throws Exception{
System.out.println("param " +search);
Thread.sleep(3000);
System.out.println("Opened");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys(search);
element.submit();
System.out.println("Clicked");
}
//}
@AfterClass
public void tearDown() throws Exception {
//selenium.stop();
}
public Object[][][] getTableArray(String xlFilePath,String sheetName) throws Exception{
Object[][] tabArray=null;
File inputWorkbook = new File(xlFilePath);
Workbook w;
int startRow,startCol, endRow, endCol,ci,cj,ck;
try {
//w = Workbook.
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(sheetName);
// Loop over first 10 column and lines
endRow = sheet.getRows();
endCol = sheet.getColumns();
tabArray=new String[endRow-1][endCol-1];
ci=0;
for (int i=1;i<endRow;i++,ci++){
cj=0;
for (int j=1;j<endCol;j++,cj++){
Cell cell = sheet.getCell(j, i);
tabArray[ci][cj] = cell.getContents();
}
// System.out.println("");
}
//file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return(tabArray); /// Here Getting the error **Type mismatch: cannot convert from Object[][] to Object[][][]**
}
}
那么,从这个代码,它看起来像你知道如何将数据提供商工作。所有你需要做的就是给它一个String的三维数组,并且它会把最内层的数组传递给测试。 –
Iam新到这可以请你帮我这样做, – Learner
您好MrTi,你能帮我一下,或者给一个链接来解决这个问题,Iam在这里着色,不能继续下去。 – Learner