<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 在 Selenium WebDriver 中創建對象存儲庫:XML &屬性文件 > 原文: [https://www.guru99.com/object-repository-selenium.html](https://www.guru99.com/object-repository-selenium.html) ## 什么是對象存儲庫? 對象存儲庫是所有對象的通用存儲位置。 在 Selenium WebDriver 上下文中,對象通常是用于唯一標識 Web 元素的定位器。 使用對象存儲庫的主要優點是將對象與測試用例分離。 如果一個 Web 元素的定位符值發生更改,則僅需要更改對象存儲庫,而不需要在使用該定位符的所有測試用例中進行更改。 維護對象存儲庫可提高框架實現的模塊化程度。 在本教程中,您將學習- * [什么是對象存儲庫?](#1) * Selenium Web 驅動程序中的[對象存儲庫類型](#2) * [使用屬性文件](#3)的 Selenium Web 驅動程序對象存儲庫 * [使用 XML 文件](#4)的 Selenium WebDriver 對象存儲庫 ## Selenium Web 驅動程序中的對象存儲庫類型 Selenium WebDriver 默認情況下不提供內置對象存儲庫。 但是,可以使用鍵值對方法構建對象存儲庫,其中鍵是指賦予對象的名稱,值是指用于唯一標識網頁中對象的屬性。 以下是可以在 Selenium WebDriver 中創建的對象存儲庫的類型。 1. 使用屬性文件的對象存儲庫 2. 使用 XML 文件的對象存儲庫 ## 使用屬性文件的 Selenium Web Driver 對象存儲庫 在這種方法中,屬性文件是一個文本文件,其中數據以鍵值對的形式存儲。 下面的教程將解決以下主題。 * [在 Eclipse 中創建屬性文件](#11) * [將數據存儲到屬性文件](#12) * [從屬性文件](#13)讀取數據 * [在測試腳本](#14)中使用屬性文件 ### 步驟 1)在 Eclipse 中創建屬性文件 1. 首先,需要在 eclipse 中創建以下 java 項目結構。 項目名稱和程序包名稱可以是任何有效名稱。 ![](https://img.kancloud.cn/f1/3d/f13d9ee44b34688c457e6dbf2aa790f6_481x257.png) 2. 右鍵單擊主項目文件夾,然后選擇新建->其他 ![](https://img.kancloud.cn/fd/80/fd80ad478e39e62c5a793bdc43a2a007_1074x587.png) 3. 在下一個窗口中,選擇常規->文件,然后單擊“下一步”按鈕 ![](https://img.kancloud.cn/3b/4d/3b4defa98f96a513740e8c0b69376165_700x447.png) 4. 在新的文件資源窗口上提供帶有擴展名“ .properties”的有效文件名,然后單擊“完成”按鈕 ![](https://img.kancloud.cn/fc/7f/fc7fa212d480a9e468ba6949644534dd_800x684.png) 5. 必須在項目結構上顯示名為“ application.properties”的文件 ![](https://img.kancloud.cn/d7/d3/d7d3632127cf89ff2f3d85b15a8d65a0_479x220.png) ### 步驟 2)將數據存儲到屬性文件中 1. 數據以鍵值對的形式存儲在屬性文件中,鍵在整個文件中是唯一的。 2. 我們將嘗試使用屬性文件通過定位符值來識別網絡元素。 3. 在 Eclipse 中打開 application.properties 文件并存儲以下數據 ``` MobileTesting=//a[text()='MOBILE TESTING'] EmailTextBox = philadelphia-field-email SignUpButton = philadelphia-field-submit ``` ![](https://img.kancloud.cn/cb/5a/cb5aedbf8bebdbae257f20d07e291e01_351x160.png) 4)對于本教程,正在使用以下演示網站: [http://demo.guru99.com/test/guru99home/](http://demo.guru99.com/test/guru99home/) 。 這是測試方案: * 單擊使用 XPATH 的“移動測試”鏈接 * 向后導航 * 使用 ID 將數據輸入電子郵件文本框 * 單擊使用 ID 的“注冊”按鈕 ### 步驟 3)從屬性文件讀取數據 1. 可以使用 java.util 包中提供的內置 Properties 類從屬性文件中讀取數據。 2. 最初,需要按以下方式創建 Properties 類的對象 ``` Properties obj = new Properties(); ``` 3. 我們需要創建一個 FileInputStream 類的對象,并帶有屬性文件的路徑 ``` FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\application.properties"); ``` 4. 可以使用 Java 中 Properties 類提供的 load 方法從屬性文件中讀取數據。 下面的代碼演示了 load 方法的用法。 ``` Properties obj = new Properties(); FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\application.properties"); obj.load(objfile); String mobileTesting = obj.getProperty("MobileTesting"); ``` 字符串“ mobileTesting”將包含 XPATH,以標識網頁內的“移動測試”鏈接。 ### 步驟 4)在測試腳本中使用屬性文件 通過從屬性文件讀取數據并將數據作為參數傳遞給 findElement 方法,可以在測試腳本中使用屬性文件。 以下代碼演示了在測試腳本中從屬性文件讀取的數據的用法。 ``` driver.findElement(By.xpath(obj.getProperty("MobileTesting"))).click(); driver.findElement(By.id(obj.getProperty("EmailTextBox"))).sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it. "); driver.findElement(By.id(obj.getProperty("SignUpButton"))).click(); ``` 以下是用于上述測試方案的完整代碼。 ``` package com.objectrepository.demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class DemoOR { public static void main(String[] args) throws IOException { // Create WebDriver Instance WebDriver driver; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://demo.guru99.com/test/guru99home/"); driver.manage().window().maximize(); // Load the properties File Properties obj = new Properties(); FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\application.properties"); obj.load(objfile); // Nagigate to link Mobile Testing and Back driver.findElement(By.xpath(obj.getProperty("MobileTesting"))).click(); driver.navigate().back(); // Enter Data into Form driver.findElement(By.id(obj.getProperty("EmailTextBox"))).sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it."); driver.findElement(By.id(obj.getProperty("SignUpButton"))).click(); } } ``` ## 使用 XML 文件的 Selenium WebDriver 對象存儲庫 XML 代表可擴展標記語言。 XML 文件使用文檔對象模型(DOM)作為基本結構。 XML 文件格式將復制用于構建網頁的 HTML 格式。 以下是將涉及的主題列表。 * [在 Eclipse 中創建 XML 文件](#21) * [將數據存儲到 XML 文件](#22) * [從 XML 文件](#23)讀取數據 * [在測試腳本](#24)中使用 XML 文件 ### 步驟 1)在 Eclipse 中創建 XML 文件 1. 需要在 Eclipse 中創建以下 Java 項目結構。 ![](https://img.kancloud.cn/48/97/4897d2c0b9c916b278262b592d1de9e1_478x186.png) 2. 右鍵單擊項目文件夾,選擇新建->其他 ![](https://img.kancloud.cn/4e/5c/4e5cabbb333c38f6f61707997d0c684d_467x231.png) 3. 在 XML 文件夾中選擇 XML 文件,然后單擊“下一步”按鈕 ![](https://img.kancloud.cn/0d/79/0d79484d944b2c83f36fb3fec8103353_855x550.png) 4. 輸入有效的 XML 文件名,然后單擊“完成”按鈕 ![](https://img.kancloud.cn/0d/fe/0dfe3c19be6f599a9c3642d4e67fff20_700x598.png) 5. XML 文件將添加到項目文件夾,如下所示 ![](https://img.kancloud.cn/fa/04/fa04c380d3e52e43d25f9bd89752e017_471x210.png) ### 步驟 2)將數據存儲到 XML 文件中 數據可以以文檔對象模型(DOM)的形式存儲在 XML 文件中。 為了簡單起見,我們可以使用以下測試場景作為示例。 * 單擊使用 XPATH 的“移動測試”鏈接 * 導航回到主頁 * 使用 ID 將數據輸入電子郵件文本框 * 單擊使用 ID 的“注冊”按鈕 以下是要使用的 XML 文件的格式。 ``` <menu> <mobiletesting>//a[text()='MOBILE TESTING']</mobiletesting> <email> philadelphia-field-email</email> <signup> philadelphia-field-submit </signup> </menu> ``` 將以上 XML 代碼存儲在 properties.xml 中 ![](https://img.kancloud.cn/b1/51/b1515a887ab2ab06f0bfc0c377c2c7f7_520x104.png) 在設計標簽中,您將看到 ![](https://img.kancloud.cn/5b/1b/5b1bedb696d45d934a4b76d156577b8a_519x167.png) ### 步驟 3)從 XML 文件讀取數據 1.可以使用 Java 中的內置“ dom4j”類完成從 XML 文件讀取數據的操作。 請注意,在繼續執行代碼之前,需要將以下 JAR 文件添加到項目的 buildpath 中。 * jaxen.jar * dom4j-1.6.jar 2.下面是從 XML 文件讀取數據的代碼。 ``` File inputFile = new File(System.getProperty("user.dir") +"\\properties.xml"); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(inputFile); String mobileTesting = document.selectSingleNode("//menu/mobiletesting").getText(); String emailTextBox = document.selectSingleNode("//menu/email").getText(); String signUpButton = document.selectSingleNode("//menu/signup").getText(); ``` 3.最初,我們需要創建一個 File 對象,并將其作為參數傳遞給 SAXReader 類的“ read”方法。 成功讀取 XML 文件數據后,我們可以使用“ selectSingleNode”方法訪問 XML 文檔的各個節點。 ### 步驟 4)在測試腳本中使用 XML 文件 通過從 XML 文件讀取數據并將數據作為參數傳遞給 findElement 方法,可以在測試腳本中使用 XML 文件。 以下代碼演示了在測試腳本中從 XML 文件讀取的數據的用法。 ``` driver.findElement(By.xpath(mobileTesting)).click(); driver.findElement(By.id(emailTextBox)).sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it."); driver.findElement(By.id(signUpButton)).click(); ``` 以下代碼演示了 Selenium WebDriver 中 XML 文件的使用 ``` package com.objectrepository.demo; import java.io.*; import java.util.*; import org.dom4j.*; import org.dom4j.io.SAXReader; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class DemoORXML { public static void main(String[] args) throws DocumentException { // Creating WebDriver Instance WebDriver driver; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://demo.guru99.com/test/guru99home/"); driver.manage().window().maximize(); // Reading XML File File inputFile = new File(System.getProperty("user.dir") +"\\properties.xml"); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(inputFile); String mobileTesting = document.selectSingleNode("//menu/mobiletesting").getText(); String emailTextBox = document.selectSingleNode("//menu/email").getText(); String signUpButton = document.selectSingleNode("//menu/signup").getText(); //Navigating to Mobile Testing and back driver.findElement(By.xpath(mobileTesting)).click(); driver.navigate().back(); //Entering Form Data driver.findElement(By.id(emailTextBox)).sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it."); driver.findElement(By.id(signUpButton)).click(); } } ``` [下載 WebDriver Eclipse 項目](https://drive.google.com/uc?export=download&id=1IweUfq7atfRYYGSVoR6N6o1bO0GtT46w) ## 摘要: * 對象存儲庫是所有對象的通用存儲位置 * Selenium WebDriver 默認情況下不提供內置對象存儲庫 * 您可以在 Selenium 中創建 2 種類型的對象庫 1. 使用屬性文件的對象存儲庫 2. 使用 XML 文件的對象存儲庫 * 屬性文件是一個文本文件,其中數據以鍵值對的形式存儲 * XML 文件格式將復制用于構建網頁的 HTML 格式。
                  <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>

                              哎呀哎呀视频在线观看