<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經轉移至github,大家請訪問 http://qaseven.github.io/

    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("http://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("http://button//span[contains(text(),'登出')]"));
    logoutButton.click();
    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }

    posted on 2014-11-26 14:21 順其自然EVO 閱讀(5702) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

    <2014年11月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    30123456

    導航

    統計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 成人毛片18岁女人毛片免费看 | 日韩a毛片免费观看| 亚洲精品综合久久| 97在线线免费观看视频在线观看| 激情吃奶吻胸免费视频xxxx| 久久亚洲熟女cc98cm| 国产亚洲精品a在线观看| 成年女人免费视频播放77777| eeuss免费天堂影院| 麻豆69堂免费视频| 涩涩色中文综合亚洲| 亚洲国产精品成人精品小说| 亚洲国产一区视频| 免费在线观看毛片| 国产精品冒白浆免费视频| 91嫩草国产在线观看免费| 最好看最新的中文字幕免费| 久久精品成人免费网站| 国产在线播放线91免费| 亚洲精品免费观看| 国产男女爽爽爽免费视频| 亚洲精品老司机在线观看| 91禁漫免费进入| 中国一级特黄的片子免费| 亚洲精品无码久久久久久久| 在线观看片免费人成视频播放| 欧洲亚洲国产精华液| 亚洲国产精品成人精品小说| 成年女人18级毛片毛片免费观看| 久久精品免费观看| 69国产精品视频免费| 最近2019免费中文字幕视频三| 日韩精品无码专区免费播放| 国产成人免费在线| 黄a大片av永久免费| 亚洲中久无码不卡永久在线观看| 亚洲国产精品无码av| 亚洲大尺码专区影院| 黄页网址在线免费观看| 成在线人免费无码高潮喷水| 男男gvh肉在线观看免费|