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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 10I 高級 WebDriver – 使用屬性文件 > 原文: [https://javabeginnerstutorial.com/selenium/10i-advanced-webdriver-property-files/](https://javabeginnerstutorial.com/selenium/10i-advanced-webdriver-property-files/) 嗨呀冠軍! 歡迎回到另一篇有趣的文章,它告訴您為什么**屬性文件**首先存在! 這個特定的概念不僅限于 Selenium WebDriver 項目。 它基本上可以用于任何涉及硬編碼內容的 Java 項目中。 假設您有數百個自動化測試用例,并且在每個測試用例中,您都對要測試的應用的 URL 進行了硬編碼。 到目前為止,一切都很好。 但是,如果彈出另一個版本,將應用 URL 更改為另一個版本,并且要再次執行相同的測試用例(回歸測試),該怎么辦? 您可能會想,“那很簡單! 我只需要再運行一次構建文件。 只需單擊一下,我就可以玩超級馬里奧了!”。 您是否錯過了 URL 部分? 手動轉到每個測試用例并編輯硬編碼的 URL 以使其正常工作又會帶來多大的痛苦呢? 不好了!!! 別擔心! 我們有一個解決方案。 財產文件將為我們提供幫助! 為了使您的測試用例更加動態,請確保不要在其中放入任何硬編碼的值。 將這些值抽象到屬性文件中,以便每當它們更改時,您都可以在一個位置進行編輯,并且測試用例可以再次完美地工作。 事不宜遲,讓我們僅通過三個小步驟就可以了解如何在我們的項目中實現此目標, ## 步驟 1: 右鍵單擊項目->新建->包。 確保“源文件夾”顯示您的項目名稱,并將包名稱設為“資源”。 現在,右鍵單擊“資源包->新建->文件”。 讓文件名是“`config.properties`”。 ![Folder Structure](https://img.kancloud.cn/4b/67/4b67ad3bf389edf4d9047a5d10599c21_350x281.png) ## 步驟 2: 現在是時候從測試用例中提取所有這些硬編碼的值了。 在“`config.properties`”文件中,將所有必需的屬性作為鍵值對。 這將幫助我們在測試用例中引用每個屬性及其鍵,該屬性將在一分鐘內演示。 可以在這個特定文件中對值進行任何更改,并且這些更改將神奇地反映在進行引用的所有測試用例中。 ![property file](https://img.kancloud.cn/e1/f4/e1f4e8001cf6c53d1aba275cf72bfbe2_737x241.png) ## 步驟 3: 為了在測試案例中使用這些屬性, `Properties props = new Properties();` 聲明“屬性”類型的“屬性”變量。 這將創建一個沒有默認值的空屬性列表。 這需要從`java.util`包導入`import java.util.Properties;` `FileInputStream fis = new FileInputStream("resources//config.properties");` – 創建一個連接以從“`resources`”包下的“`config.properties`”文件中讀取所有屬性。 這也需要從`java.io`包中導入`import java.io.FileInputStream;` `props.load(fis);` - 使用打開的連接“`fis`”從輸入字節流中讀取所有屬性。 `props.getProperty("baseURL");` – 要獲取特定屬性的值,請使用“`getProperty`”方法。 將在雙引號中提及相應的鍵作為 `getProperty()`方法的參數。 它使用屬性列表中的指定鍵搜索屬性。 如果找不到鍵,則返回`null`。 ## 概覽 讓我們看一個測試案例,實現到目前為止所涵蓋的概念, ### 場景 1. 打開 Firefox 瀏覽器。 2. 從屬性文件中讀取 firefox 驅動程序路徑和基本 URL。 3. 導航到[演示站點](https://chandanachaitanya.github.io/selenium-practice-site/) 4. 按名稱找到“三輪車”復選框,然后將相應的消息打印到控制臺 5. 檢查“三輪車”復選框是否已啟用,并將相應消息打印到控制臺 6. 根據“貨車”和“SUV”復選框的當前選擇狀態,選中或取消選中并打印執行`click()`動作前后的狀態 7. 使用 XPath 找到“轎車”復選框 8. 使用兩次迭代在選擇狀態和取消選擇狀態之間切換 9. 使用`cssSelector`找到“雜志”單選按鈕 10. 檢查是否默認選中 11. 如果是,則將相應消息打印到控制臺,如果否,請選擇單選按鈕 驗證 Eclipse IDE 控制臺輸出屏幕和 JUnit 窗格是否成功 ### 此方案的 JUnit 代碼 **`Config.properties`** ```java #Properties as key-value pairs baseUrl=https://chandanachaitanya.github.io/selenium-practice-site/ logoPath=E:\\Selenium\\Logo.png gmail=tester01@gmail.com pdfReportPath=E:\\Selenium\\junit.pdf firefoxPath=E:\\Softwares\\Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe chromePath=browser-drivers\\chromedriver.exe IEPath=browser-drivers\\IEDriverServer.exe ``` **`RadioBtns_Checkboxes.java`** ```java package com.blog.junitTests; import java.io.FileInputStream; import java.util.List; import java.util.Properties; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class RadioBtns_Checkboxes { // Declaring variables private WebDriver driver; private String baseUrl; Properties props; @Before public void setUp() throws Exception { // Creates an empty property list props = new Properties(); // A connection is created to config.properties file FileInputStream fis = new FileInputStream("resources//config.properties"); // Reads the properties from the input byte stream props.load(fis); // Get the firefox driver path from property file String firefoxPath = props.getProperty("firefoxPath"); // Assign the URL to be invoked to a String variable baseUrl = props.getProperty("baseUrl"); // Mention the property where required System.setProperty("webdriver.gecko.driver", firefoxPath); // Create a new instance for the class FirefoxDriver // that implements WebDriver interface driver = new FirefoxDriver(); // Implicit wait for 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); } @Test public void testPageTitle() throws Exception { // Open baseUrl in Firefox browser window driver.get(baseUrl); // Locate 'Tricycle' checkbox using name WebElement tricycleCheckbox = driver.findElement(By.name("vehicle2")); // Check if tricyle is displayed System.out.println("Is tricycle displayed? "+ tricycleCheckbox.isDisplayed()); // Check if tricyle is enabled to select if (tricycleCheckbox.isEnabled()) { // Click if enabled tricycleCheckbox.click(); } else { // Print message to console if disabled System.out.println("Unable to select 'Tricycle' checkbox as it is disabled."); } //Get all checkbox elements in a list List<WebElement> list = driver.findElements(By .cssSelector("input[type='checkbox']")); // Loops through all checkboxe elements for (int i = 0; i < list.size(); i++) { // Checking if the checkbox is a 'Van' or 'SUV' if ((list.get(i).getAttribute("value").trim() .equalsIgnoreCase("van")) || (list.get(i).getAttribute("value").trim() .equalsIgnoreCase("suv"))) { // Print selection status to console System.out.println("BEFORE: Is " + list.get(i).getAttribute("value") + " selected? " + list.get(i).isSelected()); // Check if the checkbox is selected if (!(list.get(i).isSelected())) { // Click the checkbox list.get(i).click(); System.out.println("AFTER: Is " + list.get(i).getAttribute("value") + " selected? " + list.get(i).isSelected()); } else { // Uncheck the checkbox list.get(i).click(); System.out.println("AFTER: Is " + list.get(i).getAttribute("value") + " selected? " + list.get(i).isSelected()); } System.out.println("Next..."); } } // Locate 'Sedan' checkbox using xPath WebElement sedanCheckbox = driver.findElement(By .xpath("//input[@name='vehicle5']")); System.out.println("Trying to select and de-select Sedan checkbox..."); for (int i = 0; i < 2; i++) { // Click the checkbox sedanCheckbox.click(); // Print current status to console System.out.println("Selection status of 'Sedan' checkbox: " + sedanCheckbox.isSelected()); } // Locate 'Magazines' radio button using cssSelector WebElement magazinesRadioBtn = driver.findElement(By .cssSelector("input[value='Magazines']")); // Check if radio button is selected by default if (magazinesRadioBtn.isSelected()) { // Print message to console System.out.println("Magazines radio button is selected by default"); } else { // Click the radio button magazinesRadioBtn.click(); } } //End of @Test @After public void tearDown() throws Exception { // Close the Firefox browser driver.close(); } } ``` ## 輸出 每行代碼都提供了清晰的注釋,使其易于說明。 執行測試用例后,eclipse IDE 控制臺窗口的輸出如下, ![property file eclipse output](https://img.kancloud.cn/26/eb/26eb3bc3a8803060c75c928646ec705a_678x473.png) 屬性文件使自動化測試人員的生活成真,夢想成真。 由此證明! 現在該嘗試使用今天的概念了。 請務必戴好安全帽,以免碰到[異常](https://javabeginnerstutorial.com/core-java-tutorial/exception-handling-try-catch-java/)! 還有一件事,我可以在 [GitHub 倉庫](https://github.com/JBTAdmin/Selenium/tree/master/AdvancedWebDriver/Property%20Files)中找到所有代碼文件。 去看一下! 再見! 祝你有美好的一天!
                  <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>

                              哎呀哎呀视频在线观看