1. 封装读取excel数据的方法:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import jxl.*;
import jxl.read.biff.BiffException;
public class TestExcel {
/**
* 将EXCEL表格按单元格读取,放入一个二维数组中。 <一句话功能简述> <功能详细描述>
*
* @param fileName
* 文件的名称,带路径
* @param sheetName
* excel的sheet的名称
* @param fromRownum
* 开始的行数
* @param endRownum
* 结束的行数
* @return
* @throws BiffException
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static String[][] getExcelData(String fileName, String sheetName,
int fromRownum, int endRownum) throws BiffException, IOException {
Workbook book = Workbook.getWorkbook(new File(fileName));
String[][] data = null;
Sheet st = book.getSheet(sheetName);
int rows = st.getRows();
int cols = st.getColumns();
if (endRownum > rows)
endRownum = rows;// 如果指定结束的行数大于excel表格内容的最大行数,结束的行数取excel的最大行数作为结束的行数
data = new String[endRownum - fromRownum + 1][cols];
int rRow = 0;
for (int iRow = fromRownum; iRow <= endRownum; iRow++) {
for (int iCol = 0; iCol < cols; iCol++) {
String val = st.getCell(iCol, iRow).getContents().trim();// 列,行
data[rRow][iCol] = val;
}
rRow++;
}
book.close();
return data;
}
}
2. 封装对象,这里以注册为例,故以注册信息为对象封装:
public class RegisterData {
public String iphoneN;
public String passN;
public String imgYz;
public String messYz;
public String errNote;
public void setiphoneN(String iphoneN){
this.iphoneN=iphoneN;
}
public String getiphoneN(){
return iphoneN;
}
public void setpassN(String passN){
this.passN=passN;
}
public String getpassN(){
return passN;
}
public void setimgYz(String imgYz){
this.imgYz=imgYz;
}
public String getimgYz(){
return imgYz;
}
public void setmessYz(String messYz){
this.messYz=messYz;
}
public String getmessYz(){
return messYz;
}
public void seterrNote(String errNote){
this.errNote=errNote;
}
public String geterrNote(){
return errNote;
}
}
3. 使用testng+selenium进行注册测试:
采用了DataProvider,由于必须是object或Iterator<Object[]>对象,故对步骤2读出来的对象转换了一下。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class RegisterTest {
static TestExcel ResEx=new TestExcel();
WebDriver regpage=new ChromeDriver();
@DataProvider(name = "testData")
public Iterator<Object[]> RegisterTest_data() {
List<RegisterData> datalist = new ArrayList<RegisterData>();
List<Object[]> dataToBe = new ArrayList<Object[]>();
try {
String[][] s = ResEx.getExcelData("F:\\Users\\Wangtest\\workspace\\TestNG\\data\\register.xls", "Sheet1", 1, 3);
for (int i = 0; i < s.length; i++) {
RegisterData data = new RegisterData();
data.setiphoneN(s[i][0]);
data.setpassN(s[i][1]);
data.setimgYz(s[i][2]);
data.setmessYz(s[i][3]);
data.seterrNote(s[i][4]);
datalist.add(data);
}
for (RegisterData userData : datalist)
{
dataToBe.add(new Object[] { userData } );
}
} catch (Exception e) {
e.printStackTrace();
}
return dataToBe.iterator();
}
@Test(dataProvider="testData")
public void f(RegisterData dataex)
throws Exception {
regpage.get("http://login.maijia.com/register.htm");
try {
Thread.sleep(3000);
regpage.findElement(By.name("mobile")).sendKeys(dataex.iphoneN);
regpage.findElement(By.name("loginPassword")).sendKeys(dataex.passN);
regpage.findElement(By.name("validateCode")).sendKeys(dataex.imgYz);
regpage.findElement(By.name("identifyCode")).sendKeys(dataex.messYz);
regpage.findElement(By.xpath("//button[contains(.,'注册')]")).click();
WebElement tips=regpage.findElement(By.xpath("//*[@id='J_Page-register']/div/div[2]/div/form/div[2]/div[3]/div"));
if(tips.getText().matches(dataex.errNote)){
System.out.println(dataex.errNote+":pass");
Thread.sleep(2000);
}
else{
System.out.println(dataex.errNote+":error");
Thread.sleep(2000);
}
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
}
}