Selenium之利用Excel实现参数化

简介:
说明:我是通过Workbook方式来读取excel文件的,这次以登陆界面为例
  备注:使用Workbook读取excel文件,前提是excel需要2003版本,其他版本暂时不支持
   具体步骤:
  第一步:新建一个excel文件,并且输入数据内容
  第二步:在eclipse中新建一个 java class,编写获取excel文件的代码
  CODE:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
/*
* 获取Excel文件的内容,使用Workbook方式来读取excel
*/
public class ExcelWorkBook {
//利用list集合来存放数据,其类型为String
private List<string> list=new ArrayList</string><string>();
//通过Workbook方式来读取excel
Workbook book;
String username;
/*
* 获取excel文件第一列的值,这里取得值为username
*/
public List</string><string> readUsername(String sourceString) throws IOException,Exception{
List</string><string> userList = new ArrayList</string><string>();
try {
Workbook book =Workbook.getWorkbook(new File(sourceFile));
Sheet sheet=book.getSheet(0);
//获取文件的行数
int rows=sheet.getRows();
//获取文件的列数
int cols=sheet.getColumns();
//获取第一行的数据,一般第一行为属性值,所以这里可以忽略
String col1=sheet.getCell(0,0).getContents().trim();
String col2=sheet.getCell(1,0).getContents().trim();
System.out.println(col1+","+col2);
//把第一列的值放在userlist中
for(int z=1;z<rows ;z++){
String username=sheet.getCell(0,z).getContents();
userList.add(username);
}
} catch (Exception e) {
e.printStackTrace();
}
//把获取的值放回出去,方便调用
return userList;
}
/*
* 获取excel文件第二列的值,这里取得值为password
*/
public List<String> readPassword(String sourceString) throws IOException,Exception{
List<string> passList = new ArrayList</string><string>();
try {
Workbook book =Workbook.getWorkbook(new File(sourceFile));
Sheet sheet=book.getSheet(0);
int rows=sheet.getRows();
for(int z=1;z<rows ;z++){
String password=sheet.getCell(1,z).getContents();
passList.add(password);
}
} catch (Exception e) {
e.printStackTrace();
}
return passList;
}
public List<String> getList(){
return list;
}
}
 第三步:新建一个TestNg Class,把excel数据填写到测试界面,具体代码如下:
  CODE:
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import File.ExcelWorkBook;
public class LoginCenter {
private WebDriver driver;
private String url;
String sourceFile="你文件的路径和文件名称";
@BeforeClass
public void testBefore(){
//设置firefox浏览器
FirefoxProfile file=new FirefoxProfile(new File("C:\\Users\\qinfei\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\t5ourl6s.selenium"));
driver=new FirefoxDriver(file);
url="你的测试地址";
}
@Test
public void login() throws Exception{
//初始化ExcelWorkBook Class
ExcelWorkBook excelbook=new ExcelWorkBook();
//进入到你的测试界面
driver.get(url);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
try{
//把取出的username放在userlist集合里面
List<string> userList=excelbook.readUsername(sourceFile);
//把取出的password放在passlist集合里面
List</string><string> passList=excelbook.readPassword(sourceFile);
//把取出来的值,输入到界面的输入框中
int usersize=userList.size();
for(int i=0;i<usersize ;i++){
//通过css定位到username输入框
WebElement username=driver.findElement(By.cssSelector("input[name=\"j_username\"]"));
//通过css定位到password输入框
WebElement password=driver.findElement(By.cssSelector("input[name=\"j_password\"]"));
//通过xpath定位登录按钮
WebElement submit=driver.findElement(By.xpath("//button//span[contains(text(),'登录')]"));
//清除username输入框的内容
username.clear();
//把list中数据一个一个的取出来
String name=userList.get(i);
//然后填写到username输入框
username.sendKeys(name);
for(int j=0;j<passList.size();j++){
password.clear();
String pass=passList.get(j);
password.sendKeys(pass);
}
//点击登录按钮
submit.click();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
//通过xpath定位登出按钮
WebElement logoutButton=driver.findElement(By.xpath("//button//span[contains(text(),'登出')]"));
logoutButton.click();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
最新内容请见作者的GitHub页:http://qaseven.github.io/
相关文章
|
8月前
selenium-基于unittest的excel数据驱动
selenium-基于unittest的excel数据驱动
61 0
|
Web App开发 数据安全/隐私保护 Python
Python+selenium 自动化-读取excel记录的脚本执行登陆操作实战演示
Python+selenium 自动化-读取excel记录的脚本执行登陆操作实战演示
275 0
Python+selenium 自动化-读取excel记录的脚本执行登陆操作实战演示
|
Java 测试技术
selenium webdriver读取excel进行数据驱动测试
最近做自动化需要从文件读取数据做参数化,网上发现一个不错的解决方案。 准备:新建一个excel文件,文件名为测试类名,sheet名为测试方法名         excel第一行为标题,从第二行开始为测试数据         build path:jxl.
1175 0
|
18天前
|
SQL 缓存 easyexcel
面试官问10W 行级别数据的 Excel 导入如何10秒处理
面试官问10W 行级别数据的 Excel 导入如何10秒处理
47 0
|
29天前
|
安全 Java 数据库连接
jdbc解析excel文件,批量插入数据至库中
jdbc解析excel文件,批量插入数据至库中
20 0
|
1月前
|
Java API Apache
使用AOP+反射实现Excel数据的读取
使用AOP+反射实现Excel数据的读取
excel根据数据得出公式
excel根据数据得出公式
|
1月前
|
SQL 数据可视化 数据处理
使用SQL和Python处理Excel文件数据
使用SQL和Python处理Excel文件数据
52 0
|
29天前
|
安全 Java 数据库连接
jdbc实现批量给多个表中更新数据(解析Excel表数据插入到数据库中)
jdbc实现批量给多个表中更新数据(解析Excel表数据插入到数据库中)
153 0
|
1月前
|
存储 数据处理 Python
使用Python批量合并Excel文件的所有Sheet数据
使用Python批量合并Excel文件的所有Sheet数据
28 0