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 閱讀(5705) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

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

          導航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 蒲城县| 镇江市| 禹州市| 和林格尔县| 绥棱县| 嘉黎县| 襄城县| 江油市| 新乐市| 高青县| 綦江县| 黄梅县| 那坡县| 德令哈市| 根河市| 呼伦贝尔市| 济南市| 景德镇市| 河南省| 呼玛县| 宁夏| 九江县| 新密市| 长海县| 阿克陶县| 东乡| 玉溪市| 肥西县| 榕江县| 上饶县| 通渭县| 安溪县| 永善县| 和龙市| 紫金县| 伊宁市| 库尔勒市| 德化县| 衡阳市| 赫章县| 修文县|