使用Java在Selenium中如何處理多個視窗?


在使用Selenium和Java進行工作時,有效地處理多個視窗至關重要。在Selenium中管理多個視窗涉及在測試自動化期間在不同的瀏覽器視窗或彈出視窗之間導航。它允許與各種元素互動並驗證跨不同視窗的功能。

正確處理多個視窗可確保全面的測試覆蓋率和準確的結果。透過利用Selenium的WebDriver介面和Java,您可以檢索視窗控制代碼,在視窗之間切換,對特定視窗執行操作,並自動化涉及多個瀏覽器視窗的複雜場景。掌握這項技能可以提高Selenium測試自動化指令碼的效率和可靠性。

Selenium

Selenium是一個開源自動化框架,廣泛用於測試Web應用程式。它透過提供一系列庫和工具來簡化瀏覽器互動、使用者行為模擬和功能測試。此外,Selenium支援包括Java在內的多種程式語言,使其成為全球開發人員的熱門選擇。

WebDriver driver = new ChromeDriver();

方法

在使用Java的Selenium處理多個視窗時,可以使用各種方法和技術。本教程包含不同的示例,以幫助讀者理解如何有效地處理多個視窗。

  • 使用`getWindowHandles()`

  • 使用`switchTo().window(handle)`

  • 使用`quit()`

方法1:使用`getWindowHandles()`

在使用Java的Selenium處理多個視窗時,有一個名為`getWindowHandles()`的有用方法。此方法將所有當前開啟的視窗控制代碼作為集合返回。透過以這種方式儲存它們,使用者可以在需要時輕鬆地在不同的視窗之間切換。要使用此方法,首先使用`getWindowHandle()`獲取當前視窗控制代碼。執行開啟新視窗的操作後,使用`getWindowHandles()`檢索所有控制代碼以繼續在它們之間切換。

為了在自動化測試期間有效地處理多個視窗,可以遍歷集合並使用`switchTo().window()`方法,同時將所需的控制代碼作為引數傳遞。這使我們能夠在Selenium和Java中與特定視窗互動,從而使測試更有效。

演算法

  • 獲取當前視窗控制代碼的方法是透過`getWindowHandle()`函式。然後可以將此控制代碼儲存在一個變數中以供進一步使用。

  • 使用`getWindowHandles()`方法檢索所有視窗控制代碼,並將它們儲存在一個集合中。

  • 使用迴圈遍歷視窗控制代碼集合。

  • 要在迴圈內切換到特定視窗,可以使用`switchTo().window()`方法並將相應的控制代碼作為輸入引數。

  • 在新視窗上執行所需的操作或動作。

示例

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Set;

public class MultipleWindowsExample {

   public static void main(String[] args) {
      // Set the path to the ChromeDriver executable
      System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

      // Create an instance of ChromeDriver
      WebDriver driver = new ChromeDriver();

      // Navigate to the webpage with multiple windows
      driver.get("https://tutorialspoint.tw");

      // Perform an action that opens a new window, such as clicking a link
      driver.findElement(By.linkText("Open New Window")).click();

      // Get all window handles
      Set<String> windowHandles = driver.getWindowHandles();

      // Iterate through each window handle
      for (String windowHandle : windowHandles) {
         // Switch to the desired window
         driver.switchTo().window(windowHandle);
         // Perform actions on the window
         System.out.println("Title of the window: " + driver.getTitle());
      }

      // Close the driver instance
      driver.quit();
   }
}

輸出

Title of the window: Online Courses and eBooks Library
Title of the window: New Window

方法2:使用`switchTo().window(handle)`

在使用Java的Selenium處理多個視窗時,`switchTo().window(handle)`方法是基礎。首先,我們透過`getWindowHandles()`檢索視窗控制代碼。從那裡,遍歷每個控制代碼並應用此技術,我們可以執行各種操作並與屬於特定視窗的特定元素進行互動。將相關的視窗控制代碼作為引數傳遞將切換驅動程式的上下文;因此,僅在需要的地方執行操作。

演算法

  • 要啟動新視窗的開啟,必須首先確定觸發操作或事件,例如單擊連結或按鈕。

  • 要檢索視窗控制代碼,請使用`getWindowHandles()`方法。此方法檢索所有開啟的視窗並將其作為集合返回。

  • 在迴圈內,可以透過使用`switchTo().window(handle)`方法平滑地切換到每個可用視窗。“handle”在此處的上下文是指

  • 程式碼可能需要使用`switchTo().window(originalHandle)`切換回原始視窗,然後才能繼續進行下一個迭代。

示例

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Set;

public class MultipleWindowsExample {

   public static void main(String[] args) {
      // Set the path to the ChromeDriver executable
      System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

      // Create an instance of ChromeDriver
      WebDriver driver = new ChromeDriver();

      // Navigate to the main window
      driver.get("https://tutorialspoint.tw");

      // Perform an action that opens a new window, such as clicking a link
      driver.findElement(By.linkText("Open New Window")).click();

      // Get all window handles
      Set<String> windowHandles = driver.getWindowHandles();

      // Switch to the new window
      for (String windowHandle : windowHandles) {
         driver.switchTo().window(windowHandle);
         // Check if the window title matches the new window
         if (driver.getTitle().equals("New Window Title")) {
            // Perform actions on the new window
            System.out.println("Title of the new window: " + driver.getTitle());
            break;
         }
      }

      // Close the driver instance
      driver.quit();
   }
}

輸出

Title of the window: Online Courses and eBooks Library
Title of the window: New Window

方法3:使用`quit()`

要使用Java的Selenium處理多個視窗,`quit()`方法對於瀏覽器的正確清理和關閉非常重要。在對所有視窗執行所需的操作後,必須在驅動程式例項上呼叫`quit()`方法。此方法關閉所有瀏覽器視窗並終止WebDriver會話。它釋放相關的系統資源並確保乾淨退出。

演算法

  • 可以使用`getWindowHandle()`方法檢索當前視窗的控制代碼。只需將此控制代碼儲存在一個變數中即可稍後訪問它。

  • 要在迴圈內切換到特定視窗,可以使用`switchTo().window()`方法,並將控制代碼作為引數傳遞。

  • 如果需要,可以使用`switchTo().window()`方法並傳遞原始視窗控制代碼作為引數返回到原始視窗。

  • 完成所有視窗上的所有必要操作後,透過在驅動程式例項上呼叫`quit()`方法來關閉瀏覽器。

  • 透過執行`quit()`方法,WebDriver會話優雅地結束,確保關閉所有瀏覽器視窗並釋放資源。

程式

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class QuitExample {

   public static void main(String[] args) {
      // Set the path to the ChromeDriver executable
      System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

      // Create an instance of ChromeDriver
      WebDriver driver = new ChromeDriver();

      // Navigate to a webpage
      driver.get("https://tutorialspoint.tw");

      // Perform actions on the webpage

      // Close all windows opened by the WebDriver instance
      driver.quit();
   }
}

輸出

Closes the webpage

結論

在本教程中,我們瞭解到,使用Java在Selenium中處理多個視窗是Web自動化測試的一個重要方面。透過使用`getWindowHandles()`和`switchTo().window(handle)`等方法,測試人員可以有效地在不同視窗之間導航,對特定視窗執行操作,並根據需要檢索視窗控制代碼。`quit()`方法對於關閉WebDriver例項開啟的所有視窗以及確保WebDriver會話的正確清理和終止非常有價值。

更新於:2023年7月25日

3K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告