乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      UI自動化測試框架之Selenium關(guān)鍵字驅(qū)動

       小豬窩969 2016-03-16

      一、學習關(guān)鍵點

      使用工具:eclipse

      用到的第三方j(luò)ar包:poi.jar(操作excel);selenium.jar

      理解難點:java反射機制;逐步分層

      二、框架構(gòu)思

      1、編寫腳本

      首先我們來寫一個登陸開源中國的腳本

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      public class Login_Script {
                  public static WebDriver driver=null;
                  public static void main(String []agrs) throws InterruptedException{
      //                啟動火狐瀏覽器
                      driver= new FirefoxDriver();
      //                最大化
                      driver.manage().window().maximize();
      //                打開開源中國網(wǎng)址
                      driver.get("http://www.oschina.net/");
      //                點擊登錄
                      driver.findElement(By.xpath("//*[@id='OSC_Userbar']/a[1]")).click();
      //                輸入用戶名
                      driver.findElement(By.xpath("//*[@id='f_email']")).sendKeys("XXXXXXB");
      //                輸入密碼
                      driver.findElement(By.xpath("//*[@id='f_pwd']")).sendKeys("XXXXXXXA");
      //                點擊登錄按鈕
      //                driver.findElement(By.xpath("//*[@id='login_osc']/table/tbody/tr[7]/td/input")).click();
      //                Thread.sleep(30);
      //                點擊退出按鈕
                      driver.findElement(By.xpath("//*[@id='OSC_Userbar']/a[3]")).click();
      //                關(guān)閉瀏覽器
                      driver.quit();
                      }
      }
      2、腳本分析

      這是登陸的場景

      操作步驟

      第一步:啟動瀏覽器

      第二步:輸入網(wǎng)址

      第四步:點擊登錄

      第五步:輸入用戶名

      第六步:輸入密碼

      第七步:點擊登錄按鈕

      第八步:點擊退出

      第九步:關(guān)閉瀏覽器

      3、使用excel

      建立一個excel

      在java中創(chuàng)建一個操作excel的類 ,主要實現(xiàn)是對excel的讀和寫,主要代碼如下:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      public class ExcelUtils {
              public static HSSFSheet ExcelSheet;
              public static HSSFWorkbook    ExcelBook;
              public static HSSFRow Row;
              public static HSSFCell    Cell;
              public static void setExcelFile(String Path,String    SheetName) throws Exception{
                  FileInputStream    ExcelFile=new FileInputStream(Path);
                  ExcelBook=new HSSFWorkbook(ExcelFile);
                  ExcelSheet=ExcelBook.getSheet(SheetName);        
              }
              public static void setCellData(String Result,  int RowNum, int ColNum,String Path) throws Exception{
                    Row  = ExcelSheet.getRow(RowNum);
                  Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);
                  if (Cell == null) {
                      Cell = Row.createCell(ColNum);
                      Cell.setCellValue(Result);
                      else {
                          Cell.setCellValue(Result);
                      }
                  FileOutputStream fileOut = new FileOutputStream(Path);
                  ExcelBook.write(fileOut);
                  fileOut.flush();
                  fileOut.close();
              }
              public static String getCellDate(int RowNum,int CloNum){
                  Cell=ExcelSheet.getRow(RowNum).getCell(CloNum);
                  String cellData=Cell.getStringCellValue();
                  return cellData;
              }
      }
      4、新建一個ActionKeyWords類
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      public class ActionKeyWords {
          public static WebDriver driver=null;
      //    啟動瀏覽器并最大化
          public static void OpenBrowser (){
              driver= new FirefoxDriver();
              driver.manage().window().maximize();
          }
      //    打開開源中國網(wǎng)址
          public static void Navigate (){
              driver.get("http://www.oschina.net/");
          }
      //    點擊登錄
          public static void Login_Click (){
              driver.findElement(By.xpath("//*[@id='OSC_Userbar']/a[1]")).click();
          }
      //    輸入用戶名
          public static void Input_Name (){
              driver.findElement(By.xpath("//*[@id='f_email']")).sendKeys("XXXXXXA");
          }
      //    輸入密碼
          public static void Input_Password (){
              driver.findElement(By.xpath("//*[@id='f_pwd']")).sendKeys("XXXXXXB");
          }
      //    點擊登錄按鈕
          public static void Login_Button (){
              driver.findElement(By.xpath("//*[@id='login_osc']/table/tbody/tr[7]/td/input")).click();
          }
          //    點擊退出按鈕
          public static void Logout_Click (){
              driver.findElement(By.xpath("//*[@id='OSC_Userbar']/a[3]")).click();
          }
      //    關(guān)閉瀏覽器
          public static void CloseBrowser (){
              driver.quit();
          }
      }
      5、修改Login_Script腳本.
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      public class Login_Script {
                  public static void main(String []agrs) throws Exception{
                      ExcelUtils.setExcelFile("D:\\data\\TestData.xls""steps");
                      ActionKeyWords actionKeyWords= new ActionKeyWords();
                      String Keywords=null;
                      for(int RowNum=1;RowNum<=ExcelUtils.getLastRowNums();RowNum++){
                          Keywords=ExcelUtils.getCellDate(RowNum, 3);
                          if(Keywords.trim().equals("OpenBrowser")){
                              actionKeyWords.OpenBrowser();
                          }else if(Keywords.trim().equals("Navigate")){
                              actionKeyWords.Navigate();
                          }else if(Keywords.trim().equals("Login_Click")){
                              actionKeyWords.Login_Click();
                          }else if(Keywords.trim().equals("Input_Name")){
                              actionKeyWords.Input_Name();
                          }else if(Keywords.trim().equals("Input_Password")){
                              actionKeyWords.Input_Password();
                          }else if(Keywords.trim().equals("Login_Button")){
                              actionKeyWords.Login_Button();
                          }else if(Keywords.trim().equals("Logout_Click")){
                              actionKeyWords.Logout_Click();
                          }else if(Keywords.trim().equals("CloseBrowser")){
                              actionKeyWords.CloseBrowser();
                          }
                      }
                  }
      }

      這樣代碼的框架就基本已經(jīng)搭建起來了,代碼結(jié)構(gòu)如下:

      三、結(jié)構(gòu)優(yōu)化

      1、優(yōu)化Login_Script 類中的代碼

      注:這里用到了反射機制

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
       public class Login_Script {
                  public static ActionKeyWords actionKeyWords;
                  public static String Keywords=null;
                  public static Method[] method;
                  public Login_Script(){
                      actionKeyWords= new ActionKeyWords();
                      method=actionKeyWords.getClass().getMethods();
                  }
                  public static void main(String []agrs) throws Exception{
                      ExcelUtils.setExcelFile("D:\\data\\TestData.xls""steps");
                      new Login_Script();
                      for(int RowNum=1;RowNum<=ExcelUtils.getLastRowNums();RowNum++){
                          Keywords=ExcelUtils.getCellDate(RowNum, 3);
                          login_action();
                      }
                  }
                  public static void login_action(){
                      for(int i=0;i<method.length;i++){
      //                    System.out.println(method[i].getName()+"     "+actionKeyWords+Keywords);
                          if(method[i].getName().trim().equals(Keywords)){
                              try {
                                  method[i].invoke(actionKeyWords);
                              catch (IllegalAccessException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              catch (IllegalArgumentException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              catch (InvocationTargetException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }
                          }
                      }
                  }
      }
      2、將程序中的常量統(tǒng)一管理

      例如:網(wǎng)頁的地址,賬戶、密碼,excel路徑,這里我們在文件下面建立一個

      1
      2
      3
      4
      5
      6
      7
      8
      9
      public class Contants {
          public static  String url="http://www.oschina.net/";
          public static String excelFile="D:\\data\\";
          public static String excelName="TestData.xls";
          public static String excelSheet="steps";
          public static int excelKWCloNum=3;
          public static String userName="XXXXXXXA";
          public static String userPassword="XXXXXB";
      }
      3、增加對象庫

      下面我們看一下ActionKeyWords類中定位元素的路徑 是在代碼里面的,如果每次去修改的定位路徑的是時候都要修改代碼,為了便于維護,我們將這些元素的對象放在一個文件中,同時我們在Excel增加一列 Page Objects,這樣程序根據(jù)Excel中的Page Objects,去文件中讀取相應的元素,這里我們增加一個類OrpUtil,讀取元素的對象 

      1
      2
      3
      4
      5
      6
      7
      # Home Page Objects
      Userbar_login=//*[@id='OSC_Userbar']/a[1]
      Userbar_logout=//div[@id='OSC_Userbar']/a[3]
      #Login Page Objects
      Input_name=//*[@id='f_email']
      Input_password=//*[@id='f_pwd']
      Login_button=//*[@id='login_osc']/table/tbody/tr[7]/td/input
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      //OrpUtil類
      public class OrpUtil {
          public static String  readValue(String a){
              Properties pro=new Properties();
              String popath=Contants.ObjectReUrl;
              String value=null;
              try {
                  InputStream in =new BufferedInputStream(new FileInputStream(popath));
                  pro.load(in);
                  value=pro.getProperty(a);
              catch (FileNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              return value;
          }
      }

      優(yōu)化后的ActionKeyWords

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      public class ActionKeyWords {
          public static WebDriver driver=null;
      //    啟動瀏覽器并最大化
          public static void OpenBrowser (String OR){
              System.setProperty("webdriver.chrome.driver"".//server//chromedriver.exe");
              driver= new ChromeDriver();
              driver.manage().window().maximize();
          }
      //    打開開源中國網(wǎng)址
          public static void Navigate (String OR){
              driver.get(Contants.url);
          }
      //    點擊登錄
          public static void Login_Click (String OR){
              driver.findElement(By.xpath(OrpUtil.readValue(OR))).click();
          }
      //    輸入用戶名
          public static void Input_Name (String OR){
              driver.findElement(By.xpath(OrpUtil.readValue(OR))).clear();
              driver.findElement(By.xpath(OrpUtil.readValue(OR))).sendKeys(Contants.userName);
          }
      //    輸入密碼
          public static void Input_Password (String OR){
              driver.findElement(By.xpath(OrpUtil.readValue(OR))).click();
              driver.findElement(By.xpath(OrpUtil.readValue(OR))).sendKeys(Contants.userPassword);
          }
      //    點擊登錄按鈕
          public static void Login_Button (String OR){
              driver.findElement(By.xpath(OrpUtil.readValue(OR))).click();
          }
          //    點擊退出按鈕
          public static void Logout_Click (String OR){
              try {
                  Thread.sleep(300);
                  driver.findElement(By.xpath(OrpUtil.readValue(OR))).click();
              catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      //    關(guān)閉瀏覽器
          public static void CloseBrowser (String OR){
              driver.quit();
          }
      }

      這個OR的值是從Excel中讀取的

      4、增加測試場景

      從Excel中我們可以看到,這操作是對應的用例編寫中的我們的操作步驟,在用例設(shè)計的時候還有測試場景和結(jié)果,這里

      我們先增加個場景在EXCEL中增加一個名稱為Suite的Sheet頁

      我們程序的運行邏輯是循環(huán)讀取Suite頁中的Runmode,當為YES時根據(jù)對應的TestSuiteID去讀取對應的Steps頁中的操作在步驟,進行運行

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      public static void main(String []agrs) throws Exception{
                      ExcelUtils.setExcelFile(Contants.excelFile+Contants.excelName );
                      new Login_Script();
                      bResult = true;
      //              循環(huán)讀取suitSheet里面的值,找出運行的場景
                      for(int j=1;j<=ExcelUtils.getLastRowNums(Contants.suitSheet);j++){
                           
                          String Runmode=ExcelUtils.getCellDate(j, Contants.suitRunmode,Contants.suitSheet);
                          String suitTestSuiteId=ExcelUtils.getCellDate(j, Contants.suitTestSuiteId,Contants.suitSheet);
                          int sRowNum;
                           
                          if(Runmode.equals("YES")){
      //                      根據(jù)stepTestSuiteId在caseSheet中循環(huán)查找相對應的執(zhí)行步驟
                              for(sRowNum=1;sRowNum<=ExcelUtils.getLastRowNums(Contants.caseSheet);sRowNum++){
                                  String stepTestSuiteId=ExcelUtils.getCellDate(sRowNum, Contants.stepTestSuiteId,Contants.caseSheet);
                                  System.out.println(ExcelUtils.getCellDate(sRowNum, Contants.excelKWCloNum,Contants.caseSheet));
                                  if(stepTestSuiteId.trim().equals(suitTestSuiteId)){            
                                      Keywords=ExcelUtils.getCellDate(sRowNum, Contants.excelKWCloNum,Contants.caseSheet);
                                      r=ExcelUtils.getCellDate(sRowNum, Contants.excelPOCloNum,Contants.caseSheet);
                                      login_action(sRowNum);
                                      if(bResult == false){
                                          ExcelUtils.setCellData(Contants.fail, j, Contants.suitResult,Contants.excelFile+Contants.excelName, Contants.suitSheet);
                                           
                                      }
                                  }  
                              }
                              if(bResult == true){
                                  ExcelUtils.setCellData(Contants.pass, j, Contants.suitResult,Contants.excelFile+Contants.excelName, Contants.suitSheet);
                              }
                                                                                                       
                          }else{
                               
                              System.out.println("沒有要執(zhí)行的用例");
                              break;
                          }
                           
                      }
                       
                                       
                  }
      5、增加測試結(jié)果

      在Excel中新增一列Resut

      在Login_Script中定義一個boolean類型的變量bResult,默認是true在各個地方try,,cacth,當出現(xiàn)異常的時候在bResult賦值為false,在Excel工具類中增加一個寫入excel值得方法

      四、小結(jié)

      這樣我們的關(guān)鍵字驅(qū)動框架就初步搭好了,下面我們回歸一下基本思路

                  

      代碼地址:http://git.oschina.net/hellotester/SeleniumKeywordDrive/tree/KeydriveSelenium.v1.0/

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多