<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Selenium 自動化框架:數據驅動,關鍵字驅動&混合 > 原文: [https://www.guru99.com/creating-keyword-hybrid-frameworks-with-selenium.html](https://www.guru99.com/creating-keyword-hybrid-frameworks-with-selenium.html) ## 什么是硒框架? Selenium Framework 是一種代碼結構,有助于簡化代碼維護。 如果沒有框架,我們會將“代碼”和“數據”放置在相同的位置,該位置既不可重用也不可讀。 使用 Framework,可以產生有益的結果,例如增加代碼重用,更高的可移植性,減少的腳本維護成本,更高的代碼可讀性等。 Selenium WebDriver 創建了三種類型的框架來自動執行手動測試用例 * 數據驅動測試框架 * 關鍵字驅動的測試框架 * 混合測試框架 ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/be/d1/bed1b88041321ccc4dee74ce9dc21d7c_580x283.png "Creating Keyword & Hybrid Frameworks with Selenium") ## **數據驅動測試框架** **在**數據驅動的框架中,我們所有的測試數據都是從某些外部文件生成的,例如 [Excel](/excel-tutorials.html) ,CSV,XML 或某些數據庫表。 ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/9a/bf/9abf26315e50a4359a2cb5db0f239849_404x214.png "All About Excel in Selenium: POI & JXL") 為了讀取或編寫 Excel,Apache 提供了一個非常著名的庫 POI。 該庫足以讀取和寫入 Excel 的 **XLS** 和 **XLSX** 文件格式。 要讀取 **XLS** 文件,POI 庫提供了 **HSSF** 實現。 要讀取 **XLSX,將選擇 **POI** **庫**的** **XSSF** 實現。 讓我們詳細研究這些實現。 我們已經在[先前的教程](/all-about-excel-in-selenium-poi-jxl.html)中了解了數據驅動測試 ## **關鍵字驅動的測試框架:** 在關鍵字驅動的測試框架中,所有操作和說明都寫在某些外部文件(如 Excel 工作表)中。 這是完整框架的外觀 ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/52/0a/520a3435d79c6e3b865575ef28e6e22a_624x322.png "Creating Keyword & Hybrid Frameworks with Selenium") 如您所見,這是一個 5 步框架。 讓我們逐步研究它 **步驟 1)** * 驅動程序腳本 Execute.java 將調用 ReadGuru99ExcelFile.java * ReadGuru99ExcelFile.java 具有 POI 腳本以從 Excel 讀取數據 **步驟 2)** * ReadGuru99ExcelFile.java 將從 TestCase.xlsx 讀取數據 * 這是工作表的樣子- ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/27/b7/27b782435401708e382bbc45ffd3f3ba_624x358.png "Creating Keyword & Hybrid Frameworks with Selenium") * 根據 Excel 文件中寫入的關鍵字,框架將在 UI 上執行操作。 * 例如,我們需要單擊“登錄”按鈕。 相應地,我們的 Excel 將具有關鍵字“點擊”。 現在,AUT 可以在頁面上具有數百個按鈕,以標識“登錄”按鈕,在 Excel 中,我們將輸入對象名稱作為 loginButton &對象類型作為名稱(請參見上圖中突出顯示的行)。 對象類型可以是 Xpath,名稱 CSS 或任何其他值 **步驟 3)** ReadGuru99ExcelFile.java 會將這些數據傳遞給驅動程序腳本 Execute.java **步驟 4)** * 對于我們所有的 UI Web 元素,我們需要創建一個對象存儲庫,在其中放置它們的元素定位符(例如 Xpath,名稱,CSS 路徑,類名等)。 ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/cd/a7/cda796e7e083bbc4fed6223663e15e8b_254x211.png "Creating Keyword & Hybrid Frameworks with Selenium") * Execute.java(我們的驅動程序腳本)將讀取整個對象存儲庫并將其存儲在變量中 * To read this object repository, we need a ReadObject class which has a getObjectRepository method to read it. ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/41/1c/411cc84d59db901da523dcd4f9da2135_357x254.png "Creating Keyword & Hybrid Frameworks with Selenium") **注意:**您可能會想為什么我們需要創建一個對象存儲庫。 答案有助于代碼維護。 例如,我們在 10 個不同的測試案例中使用名稱為 btnlogin 的按鈕。 將來,開發人員決定將名稱從 btnlogin 更改為 Submit。 您將必須在所有 10 個測試用例中進行更改。 對于對象存儲庫,您只需在存儲庫中進行一次更改。 **步驟 5)** * 驅動程序會將數據從 Excel &對象存儲庫傳遞到 UIOperation 類 * UIOperation 類具有執行與 Excel 中提到的諸如 CLICK,SETTEXT 等…之類的關鍵字相對應的動作的功能。 * UIOperation 類是 [Java](/java-tutorial.html) 類,它具有對 Web 元素執行操作的代碼的實際實現 ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/06/f8/06f830c06eb948820367f467f74d01b3_624x356.png "Creating Keyword & Hybrid Frameworks with Selenium") 整個項目看起來像- ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/01/fc/01fc36e47f33addf07249f1cccb621db_501x359.png "Creating Keyword & Hybrid Frameworks with Selenium") 讓我們看一個例子: 測試場景 * 我們正在執行 2 個測試用例 * 測試案例 1: * 轉到 [http://demo.guru99.com/V4/](http://demo.guru99.com/V4/) * 輸入用戶名 * 輸入密碼 * 單擊重置 * 測試案例 2: * 轉到 [http://demo.guru99.com/V4/](http://demo.guru99.com/V4/) * 輸入用戶名 * 輸入密碼 * 單擊登錄 ## object.properties url = [http://demo.guru99.com/V4/](http://demo.guru99.com/V4/) 用戶名= uid 密碼=密碼 標題=男爵 loginButton = btn 登錄 重置按鈕= btnReset ### ReadGuru99ExcelFile.java ``` package excelExportAndFileIO; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadGuru99ExcelFile { public Sheet readExcel(String filePath,String fileName,String sheetName) throws IOException{ //Create a object of File class to open xlsx file File file = new File(filePath+"\\"+fileName); //Create an object of FileInputStream class to read excel file FileInputStream inputStream = new FileInputStream(file); Workbook guru99Workbook = null; //Find the file extension by spliting file name in substing and getting only extension name String fileExtensionName = fileName.substring(fileName.indexOf(".")); //Check condition if the file is xlsx file if(fileExtensionName.equals(".xlsx")){ //If it is xlsx file then create object of XSSFWorkbook class guru99Workbook = new XSSFWorkbook(inputStream); } //Check condition if the file is xls file else if(fileExtensionName.equals(".xls")){ //If it is xls file then create object of XSSFWorkbook class guru99Workbook = new HSSFWorkbook(inputStream); } //Read sheet inside the workbook by its name Sheet guru99Sheet = guru99Workbook.getSheet(sheetName); return guru99Sheet; } } ``` ### ReadObject.java ``` package operation; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadObject { Properties p = new Properties(); public Properties getObjectRepository() throws IOException{ //Read object repository file InputStream stream = new FileInputStream(new File(System.getProperty("user.dir")+"\\src\\objects\\object.properties")); //load all objects p.load(stream); return p; } } ``` ### UIOperation.java ``` package operation; import java.util.Properties; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class UIOperation { WebDriver driver; public UIOperation(WebDriver driver){ this.driver = driver; } public void perform(Properties p,String operation,String objectName,String objectType,String value) throws Exception{ System.out.println(""); switch (operation.toUpperCase()) { case "CLICK": //Perform click driver.findElement(this.getObject(p,objectName,objectType)).click(); break; case "SETTEXT": //Set text on control driver.findElement(this.getObject(p,objectName,objectType)).sendKeys(value); break; case "GOTOURL": //Get url of application driver.get(p.getProperty(value)); break; case "GETTEXT": //Get text of an element driver.findElement(this.getObject(p,objectName,objectType)).getText(); break; default: break; } } /** * Find element BY using object type and value * @param p * @param objectName * @param objectType * @return * @throws Exception */ private By getObject(Properties p,String objectName,String objectType) throws Exception{ //Find by xpath if(objectType.equalsIgnoreCase("XPATH")){ return By.xpath(p.getProperty(objectName)); } //find by class else if(objectType.equalsIgnoreCase("CLASSNAME")){ return By.className(p.getProperty(objectName)); } //find by name else if(objectType.equalsIgnoreCase("NAME")){ return By.name(p.getProperty(objectName)); } //Find by css else if(objectType.equalsIgnoreCase("CSS")){ return By.cssSelector(p.getProperty(objectName)); } //find by link else if(objectType.equalsIgnoreCase("LINK")){ return By.linkText(p.getProperty(objectName)); } //find by partial link else if(objectType.equalsIgnoreCase("PARTIALLINK")){ return By.partialLinkText(p.getProperty(objectName)); }else { throw new Exception("Wrong object type"); } } } ``` ### ExecuteTest.java ``` package testCases; import java.util.Properties; import operation.ReadObject; import operation.UIOperation; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; import excelExportAndFileIO.ReadGuru99ExcelFile; public class ExecuteTest { @Test public void testLogin() throws Exception { // TODO Auto-generated method stub WebDriver webdriver = new FirefoxDriver(); ReadGuru99ExcelFile file = new ReadGuru99ExcelFile(); ReadObject object = new ReadObject(); Properties allObjects = object.getObjectRepository(); UIOperation operation = new UIOperation(webdriver); //Read keyword sheet Sheet guru99Sheet = file.readExcel(System.getProperty("user.dir")+"\\","TestCase.xlsx" , "KeywordFramework"); //Find number of rows in excel file int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum(); //Create a loop over all the rows of excel file to read it for (int i = 1; i < rowCount+1; i++) { //Loop over all the rows Row row = guru99Sheet.getRow(i); //Check if the first cell contain a value, if yes, That means it is the new testcase name if(row.getCell(0).toString().length()==0){ //Print testcase detail on console System.out.println(row.getCell(1).toString()+"----"+ row.getCell(2).toString()+"----"+ row.getCell(3).toString()+"----"+ row.getCell(4).toString()); //Call perform function to perform operation on UI operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString()); } else{ //Print the new testcase name when it started System.out.println("New Testcase->"+row.getCell(0).toString() +" Started"); } } } } ``` 執行后,輸出將類似于- ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/99/11/991161d7d678f06d666789fd966df674_476x289.png "Creating Keyword & Hybrid Frameworks with Selenium") [在本教程中下載用于演示的 Selenium 項目文件](https://drive.google.com/uc?export=download&id=0B_vqvT0ovzHcZkxzUmpPcHhnOUU) ## 混合測試框架 混合測試框架是一個我們同時利用關鍵字和數據驅動框架的優勢的概念。 在這里,對于關鍵字,我們將使用 Excel 文件來維護測試用例,對于測試數據,我們將使用 [Testng](/all-about-testng-and-selenium.html) 框架的提供者數據。 ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/79/c1/79c1ed335f0ad8607dc61f5fcdb02ce4_577x315.png "Creating Keyword & Hybrid Frameworks with Selenium") 在我們的混合框架中,我們不需要在關鍵字驅動的框架中進行任何更改,這里我們只需要用 HybridExecuteTest.java 文件替換 ExecuteTest.java 文件。 ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/b3/d1/b3d1178c6b68a450e53fc78090d2c6c1_557x283.png "Creating Keyword & Hybrid Frameworks with Selenium") 該 HybridExecuteTest 文件包含由數據提供者概念驅動的關鍵字的所有代碼。 混合框架的完整圖示將如下所示 ![Selenium Framework: Keyword Driven & Hybrid](https://img.kancloud.cn/d4/7f/d47fb936a41272594054cb044c3f35f9_624x295.png "Creating Keyword & Hybrid Frameworks with Selenium") ### HybridExecuteTest.java ``` package testCases; import java.io.IOException; import java.util.Properties; import operation.ReadObject; import operation.UIOperation; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import excelExportAndFileIO.ReadGuru99ExcelFile; public class HybridExecuteTest { WebDriver webdriver = null; @Test(dataProvider="hybridData") public void testLogin(String testcaseName,String keyword,String objectName,String objectType,String value) throws Exception { // TODO Auto-generated method stub if(testcaseName!=null&&testcaseName.length()!=0){ webdriver=new FirefoxDriver(); } ReadObject object = new ReadObject(); Properties allObjects = object.getObjectRepository(); UIOperation operation = new UIOperation(webdriver); //Call perform function to perform operation on UI operation.perform(allObjects, keyword, objectName, objectType, value); } @DataProvider(name="hybridData") public Object[][] getDataFromDataprovider() throws IOException{ Object[][] object = null; ReadGuru99ExcelFile file = new ReadGuru99ExcelFile(); //Read keyword sheet Sheet guru99Sheet = file.readExcel(System.getProperty("user.dir")+"\\","TestCase.xlsx" , "KeywordFramework"); //Find number of rows in excel file int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum(); object = new Object[rowCount][5]; for (int i = 0; i < rowCount; i++) { //Loop over all the rows Row row = guru99Sheet.getRow(i+1); //Create a loop to print cell values in a row for (int j = 0; j < row.getLastCellNum(); j++) { //Print excel data in console object[i][j] = row.getCell(j).toString(); } } System.out.println(""); return object; } } ``` 摘要: * 我們可以使用 Selenium WebDriver 創建三種類型的測試框架。 * 這些是數據驅動,關鍵字驅動和混合測試框架。 * 我們可以使用 TestNG 的數據提供程序來實現數據驅動的框架。 * 在關鍵字驅動的框架中,關鍵字被寫入一些外部文件中,例如 excel 文件,而 Java 代碼將調用此文件并執行測試用例。 * 混合框架是關鍵字驅動和數據驅動框架的混合體。 [在本教程中下載用于演示的 Selenium 項目文件](https://drive.google.com/uc?export=download&id=0B_vqvT0ovzHcOGZOUXBTc1M0X2s)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看