Selenium之利用Excel实现参数化

上一篇 / 下一篇  2015-11-10 17:50:34 / 个人分类:Selenium

http://www.blogjava.net/qileilove/archive/2014/11/26/420620.html

 说明:我是通过Workbook方式来读取excel文件的,这次以登陆界面为例
  备注:使用Workbook读取excel文件,前提是excel需要2003版本,其他版本暂时不支持
  具体步骤:
  第一步:新建一个excel文件,并且输入数据内容
  第二步:在eclipse中新建一个java class,编写获取excel文件的代码
  CODE:
package test.test2;

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>();
    // 通过Workbook方式来读取excel
    Workbook book;
    String username;
    /*
     * 获取excel文件第一列的值,这里取得值为username
     */
    String sourceFile = "D:\\worktools\\workbench\\test3\\src\\test\\test2\\login.xls";;

    public List<String> readUsername(String sourceString) throws IOException, Exception {
        List<String> userList = new ArrayList<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>();
        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:
package test.test2;

//import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
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 LoginCenter2 {
    private WebDriver driver;
    private String url;
    String sourceFile = "D:\\worktools\\workbench\\test3\\src\\test\\test2\\login.xls";
    private static Logger log = Logger.getLogger(LoginCenter2.class);
    @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);
        driver = new FirefoxDriver();
        url = "http://aaaa.test.bj";
    }

    @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> passList = excelbook.readPassword(sourceFile);
            // 把取出来的值,输入到界面的输入框中
            int usersize = userList.size();
            for (int i = 0; i < usersize; i++) {
                // 通过id定位到username输入框
                WebElement username = driver.findElement(By.id("username"));
                // 通过id定位到password输入框
                WebElement password = driver.findElement(By.id("password"));
                // 通过id定位登录按钮
                WebElement submit = driver.findElement(By.id("login_form_submit"));
                // 清除username输入框的内容
                username.clear();
                // 把list中数据一个一个的取出来
                String name = userList.get(i);
                username.sendKeys(name);
                log.info(name);
                String pass = passList.get(i);
                password.sendKeys(pass);
                log.info(pass);
                // 点击登录按钮
                submit.click();
                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                // 通过xpath定位登出按钮
                WebElement logoutButton = driver.findElement(By.xpath(".//*[@onclick='logout()']"));
                logoutButton.click();
                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                driver.get(url);
                }
               
       
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




TAG: Excel

 

评分:0

我来说两句

Open Toolbar