zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Selenium之利用Excel实现参数化

Excel 实现 利用 参数 selenium
2023-09-11 14:20:31 时间
说明:我是通过Workbook方式来读取excel文件的,这次以登陆界面为例 备注:使用Workbook读取excel文件,前提是excel需要2003版本,其他版本暂时不支持 具体步骤: 第一步:新建一个excel文件,并且输入数据内容 第二步:在eclipse中新建一个java class,编写获取excel文件的代码 CODE:
//利用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 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); //把取出的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/

selenium webdriver读取excel进行数据驱动测试 最近做自动化需要从文件读取数据做参数化,网上发现一个不错的解决方案。 准备:新建一个excel文件,文件名为测试类名,sheet名为测试方法名         excel第一行为标题,从第二行开始为测试数据         build path:jxl.
Python selenium webdriver 基本使用 系列文章目录 selenium webdriver 的常用示例 一、Pip安装&创建Bowser对象 1.Pip install selenium 2.创建Bowser对象 二、webdriver.ChromeOptions配置 配置浏览器的常用模式 三、常用代码 四、selenium的异常处理