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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 9Y WebDriver – 處理多個窗口 > 原文: [https://javabeginnerstutorial.com/selenium/9y-webdriver-handling-multiple-windows/](https://javabeginnerstutorial.com/selenium/9y-webdriver-handling-multiple-windows/) 大家好! 測試涉及多個窗口的工作流已成為生活的一部分。 借助 Selenium WebDriver,在這些打開的窗口之間進行平滑切換已成為小菜一碟。 如果您像我一樣,那么您可能會急切希望看到所有這些在我們的代碼中如何真正起作用。 因此,讓我們繼續深入探討吧…… 每當實例化一個新的 WebDriver 對象時,就會向每個打開的唯一窗口分配一個唯一的字母數字 ID。 這個獨特的 ID 被稱為“窗口句柄”- 這是 *Selenium Ville* 擁擠的街道上的另一個新名詞。 因此,使用這些唯一的 ID,我們可以輕松地在窗口之間切換控件并執行所需的活動。 1\. `String getWindowHandle()`: 此方法用于獲取當前窗口的窗口句柄。 *語法:* ```java // Get current window handle String parentWinHandle = driver.getWindowHandle(); ``` 2\. `Set<String> getWindowHandles()`: 此方法用于獲取`Set`中所有打開的窗口的窗口句柄。 *語法*: ```java // Get the window handles of all open windows Set<String> winHandles = driver.getWindowHandles(); ``` 可以使用分配給每個打開的窗口的唯一句柄的引用,使用相同的`switchTo()`方法從一個窗口切換到另一窗口。 為了更好地了解`switchTo()`,[請單擊此處](https://javabeginnerstutorial.com/selenium/9x-webdriver-handling-alerts-popup-box/)。 。 ## 場景 讓我們看一個實現這些方法的測試案例,以更深入地了解這些概念, 1. 打開 Firefox 瀏覽器 2. 導航到[演示站點](https://chandanachaitanya.github.io/selenium-practice-site/) 3. 獲取當前的窗口句柄并將其打印到控制臺 4. 使用 ID 找到“點擊以打開新的瀏覽器窗口!”按鈕 5. 點擊按鈕打開新窗口 6. 獲取兩個打開的窗口的窗口句柄 7. 通過兩個句柄循環 8. 切換到帶有句柄參考的新窗口 9. 獲取標題并將其打印到控制臺 10. 關閉新窗口 11. 將控件切換回父窗口,然后將 URL 打印到控制臺 12. 驗證 Eclipse IDE 控制臺輸出屏幕和 JUnit 窗格是否成功 此方案的 JUnit 代碼是, ```java package com.blog.junitTests; import java.util.Set; 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 MultipleWindows { // Declaring variables private WebDriver driver; private String baseUrl; @Before public void setUp() throws Exception { // Selenium version 3 beta releases require system property set up System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\" + "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe"); // 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); // Assign the URL to be invoked to a String variable baseUrl = "https://chandanachaitanya.github.io/selenium-practice-site/"; } @Test public void testPageTitle() throws Exception { // Open baseUrl in Firefox browser window driver.get(baseUrl); // Get current window handle String parentWinHandle = driver.getWindowHandle(); System.out.println("Parent window handle: " + parentWinHandle); // Locate 'Click to open a new browser window!' button using id WebElement newWindowBtn = driver.findElement(By.id("win1")); // Click the button to open a new window newWindowBtn.click(); // Get the window handles of all open windows Set<String> winHandles = driver.getWindowHandles(); // Loop through all handles for(String handle: winHandles){ if(!handle.equals(parentWinHandle)){ driver.switchTo().window(handle); Thread.sleep(1000); System.out.println("Title of the new window: " + driver.getTitle()); System.out.println("Closing the new window..."); driver.close(); } } // Switching the control back to parent window driver.switchTo().window(parentWinHandle); // Print the URL to the console System.out.println("Parent window URL: " + driver.getCurrentUrl()); } // End of @Test @After public void tearDown() throws Exception { // Close the Firefox browser driver.close(); } } ``` *執行結果:* 代碼是不言自明的,因為同時提供了注釋。 在“Eclipse IDE 中 -> JUnit 窗格 -> 綠色條”顯示測試用例已成功執行。 控制臺窗口顯示沒有任何錯誤。 它還按預期顯示所有打印的消息。 ![multiple windows eclipse output](https://img.kancloud.cn/0d/e1/0de11d987305bf79d8d3b52e141ba000_819x391.png) 有了這個,我很快就會在另一篇文章中見。 祝你有美好的一天!
                  <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>

                              哎呀哎呀视频在线观看