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

 

转载于:https://www.cnblogs.com/lymblog/p/7298318.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值