zoukankan      html  css  js  c++  java
  • Selenium之利用Excel实现参数化

    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();
    }
    }
    }
  • 相关阅读:
    autocomplete自动完成搜索提示仿google提示效果
    实现子元素相对于父元素左右居中
    javascript 事件知识集锦
    让 IE9 以下的浏览器支持 Media Queries
    「2013124」Cadence ic5141 installation on CentOS 5.5 x86_64 (limited to personal use)
    「2013420」SciPy, Numerical Python, matplotlib, Enthought Canopy Express
    「2013324」ClipSync, Youdao Note, GNote
    「2013124」XDMCP Configuration for Remote Access to Linux Desktop
    「2013115」Pomodoro, Convert Multiple CD ISO to One DVD ISO HowTo.
    「2013123」CentOS 5.5 x86_64 Installation and Configuration (for Univ. Labs)
  • 原文地址:https://www.cnblogs.com/timssd/p/5089320.html
Copyright © 2011-2022 走看看