Selenium+excel實現參數化自動化測試

來源:互聯網
上載者:User

標籤:

使用到的技術:POI對excel的解析、selenium自動化測試、junit

測試案例:登陸www.1905.com執行登陸-退出的操作

執行步驟:

1、首先建立一個excel,裡面有使用者名稱和密碼列

2、建立 一個解析excel的java類

package com.m1905.java;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.poifs.filesystem.POIFSFileSystem;/** * 讀取excel檔案中的測試資料,將資料分為使用者名稱和密碼兩組 * */public class ExcelWorkBook{        String username;    String password;    /**     * 讀取excel檔案中的使用者名稱列,即第一列     * @throws IOException      * */    public List<String> readUsername(String filesrc) throws IOException{        List<String> userList = new ArrayList<String>();        //讀取excel檔案        InputStream is = new FileInputStream(filesrc);        POIFSFileSystem fs = new POIFSFileSystem(is);        HSSFWorkbook wb = new HSSFWorkbook(fs);        HSSFSheet sheet = wb.getSheetAt(0);        if(sheet==null){            System.out.println("暫無資料,請輸入測試資料");        }        //擷取檔案行數        int rows = sheet.getLastRowNum();        //擷取檔案列數        /*int cols = sheet.getRow(0).getPhysicalNumberOfCells();        //擷取第一行的資料,一般第一行為屬性值,所以這裡可以忽略        String colValue1 = sheet.getRow(0).toString();        String colValues2 = sheet.getRow(1).toString();*/        //取出第一列的使用者名稱,去除掉第一行中的標題        for(int i =1;i<rows+1;i++){            username = sheet.getRow(i).getCell(0).toString();            System.out.println(username);            userList.add(username);        }        System.out.println(userList);        return userList;    }        /**     * 擷取第二列,得到密碼list     * @throws IOException      * */    public List<String> readPassword(String filesrc) throws IOException{        List<String> passwordList = new ArrayList<String>();        //讀取excel檔案            InputStream is = new FileInputStream(filesrc);            POIFSFileSystem fs = new POIFSFileSystem(is);            HSSFWorkbook wb = new HSSFWorkbook(fs);            HSSFSheet sheet = wb.getSheetAt(0);            if(sheet==null){                System.out.println("暫無資料,請輸入測試資料");            }        //取出第二列的密碼值,去掉第一行中的標題            int rows=sheet.getLastRowNum();            for(int i=1;i<rows+1;i++){                password = sheet.getRow(i).getCell(1).toString();                System.out.println(password);                passwordList.add(password);            }            System.out.println(passwordList);        return passwordList;    }}

3、建立一個junit測試類別,對登陸過程進行測試

package com.m1905.junit;import java.io.IOException;import java.util.List;import java.util.concurrent.TimeUnit;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebDriver.Navigation;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;import com.m1905.java.ExcelWorkBook;public class LoginTest {    private static WebDriver driver;    private static Navigation navigate;    private static String url="http://www.1905.com";    private static String  filesrc = "UserAndPassword.xls";    @BeforeClass    public static void setUpBeforeClass() throws Exception {        //載入瀏覽器        driver = new FirefoxDriver();        navigate = driver.navigate();        navigate.to(url);        driver.manage().window().maximize();    }    @AfterClass    public static void tearDownAfterClass() throws Exception {        if(driver!=null){            driver.close();            driver.quit();        }    }    @Test    public void test() throws IOException {        //初始化ExcelWorkBook Class        ExcelWorkBook excelBook = new ExcelWorkBook();        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);        //取出username放在userlist集合裡面        List<String> userList = excelBook.readUsername(filesrc);        //取出password放在passwordList集合裡面        List<String> passwordList = excelBook.readPassword(filesrc);                //把取出來的資料輸入到介面中的使用者名稱和密碼的輸入框中        int usersize = userList.size();        for(int i=0;i<usersize;i++){            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);            //點擊登陸/註冊按鈕            WebElement LogAndReg = driver.findElement(By.xpath(".//*[@id=‘site_nav_md‘]/ul/li[2]/a"));            LogAndReg.click();            //通過xpath定位到username輸入框            WebElement username = driver.findElement(By.xpath(".//*[@id=‘inputUsername‘]"));            //通過xpath定位到password輸入框            WebElement password = driver.findElement(By.xpath(".//*[@id=‘inputPassword‘]"));            //通過xpath定位到登陸按鈕            WebElement login = driver.findElement(By.xpath(".//*[@id=‘loginreg‘]/div/div[1]/form/p/button"));            //清除username輸入框中的內容            username.clear();            //把usernamelist中的資料取出來,寫入            String name = userList.get(i);            username.sendKeys(name);            //輸入對應的password值            for(int j=0;j<passwordList.size();j++){                password.clear();                String pass = passwordList.get(j);                password.sendKeys(pass);            }            //點擊登陸            login.click();            //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);            WebDriverWait wait = new WebDriverWait(driver,10);            WebElement e1 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id=‘site_nav_md‘]/ul/li[3]/a[2]")));            //找到退出登陸按鈕            e1.click();//            WebElement logoutButton = driver.findElement(By.xpath(""));//            logoutButton.click();        }    }}

 

Selenium+excel實現參數化自動化測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.