• Selenium Video Tutorials

Selenium WebDriver - 多視窗測試



Selenium Webdriver 可用於處理多視窗測試。所有啟動的視窗都由會話的唯一識別符號標識。當另一個視窗開啟時,驅動程式的上下文將繼續在父視窗上。要在子視窗上執行任務,需要將驅動程式的上下文從父視窗切換到子視窗。

在 Selenium 中處理多個視窗的基本方法

Selenium 中有各種方法可用於自動化處理多個視窗的測試。要在子視窗上工作,需要將驅動程式上下文從父視窗切換到子視窗

示例 1

在下面的頁面中,單擊新標籤頁

Selenium Multi Windows 1

單擊新標籤頁後,我們將切換到另一個包含文字新標籤頁的標籤頁。

Selenium Multi Windows 2

程式碼實現

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class TabsHandling {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with new tab
      driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php");

      // click link then navigate to next tab
      WebElement b = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[1]"));
      b.click();

      // Get original window handle id
      String oW = driver.getWindowHandle();
      
      // get every windows handle ids
      Set<String> windows = driver.getWindowHandles();

      // loop through all window handles
      for (String w : windows) {
         if(!oW.equalsIgnoreCase(w)) {
         
            // switch to the child tab
            driver.switchTo().window(w);
            
            // get element in new tab
            WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/h1"));
            System.out.println("Text in new tab: " + e.getText());
            break;
         }
      }
      // quit the browser
      driver.quit();
   }
}

輸出

Text in new tab is: New Tab

Process finished with exit code 0

在這裡,我們獲取了新開啟的標籤頁上的文字,並在控制檯中獲得了訊息 - 新標籤頁中的文字:新標籤頁

最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。

示例 2

在下面的頁面中,單擊新視窗訊息

Selenium Multi Windows 3

單擊新視窗訊息後,我們將切換到另一個包含文字新視窗訊息的視窗。

Selenium Multi Windows 4

關閉新視窗,切換回原始視窗,並在那裡獲取文字 - 瀏覽器視窗。最後,退出會話。

Selenium Multi Windows 5

程式碼實現

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class WindowsOpen {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver();

      // adding implicit wait of 20 secs
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

      // Opening the webpage to open a new window
      driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php");

      // click link to next window
      WebElement b =  driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[3]"));
      b.click();

      // Obtain original window handle id
      String oW = driver.getWindowHandle();

      // obtain all opened windows handle ids
      Set<String> windows = driver.getWindowHandles();

      // Loop through all window handles
      for (String w : windows) {
         if(!oW.equalsIgnoreCase(w)) {
         
            // switch to child window
            driver.switchTo().window(w);
            
            // get element in new window
            WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/h1"));
            System.out.println("Text in new window: " + e.getText());
            driver.close();
            break;
         }
      }

      // switch to parent window
      driver.switchTo().window(oW);

      // get element in parent window
      WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));
      System.out.println("Text in parent window: " + e1.getText());

      // quit browser session
      driver.quit();
   }
}

輸出

Text in new window: New Window Message
Text in parent window: Browser Windows

在這裡,我們捕獲了新視窗中的文字,並在控制檯中獲得了訊息 - 新視窗中的文字:新視窗訊息。然後,我們結束子視窗並返回到父視窗。最後,我們在控制檯中獲取了父視窗中的文字 - 父視窗中的文字:瀏覽器視窗

因此,close() 和 quit() 方法之間存在細微差別。close() 方法僅關閉活動瀏覽器視窗,而 quit() 方法同時終止所有開啟的瀏覽器視窗。

示例 3

從版本 4 開始,我們可以使用以下方法開啟一個新視窗 -

driver.switchTo().newWindow(WindowType.WINDOW)

在瀏覽器視窗中開啟一個應用程式,並獲取文字 - 如下圖所示的複選框 -

Selenium Multi Windows 6

然後開啟另一個新視窗並啟動一個包含文字 - 單選按鈕的不同應用程式。

Selenium Multi Windows 7

程式碼實現

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;

public class NewWindowOpen {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver();

      // adding implicit wait of 12 secs
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);

      // Open a webpage in first window
      driver.get("https://tutorialspoint.tw/selenium/practice/check-box.php");

      // get text in first window 
      WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));
      System.out.println("Text: " + e.getText());

      // Initiate the another Webdriver
      WebDriver newDriver = driver.switchTo().newWindow(WindowType.WINDOW);

      // Opening another webpage in second window
      driver.get("https://tutorialspoint.tw/selenium/practice/radio-button.php");

      // get text in second window 
      WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/h1"));
      System.out.println("Text in new window: " + e1.getText());

      // quit the browser session
      driver.quit();
   }
}

輸出

Text: Check Box

Text in new window: Radio Button

在這裡,我們捕獲了第一個視窗中的文字,並在控制檯中獲得了訊息 - 文字:複選框。然後開啟另一個新視窗,並在其中啟動一個應用程式。最後,我們在新視窗中獲取了文字,並在控制檯中獲得了一條訊息 - 新視窗中的文字:單選按鈕

示例 4

從版本 4 開始,我們可以使用以下方法開啟一個新標籤頁 -

driver.switchTo().newWindow(WindowType.TAB)

在瀏覽器中開啟一個應用程式,並獲取文字 - 如下圖所示的複選框 -

Selenium Multi Windows 8

然後開啟另一個新標籤頁並啟動一個包含文字 - 單選按鈕的不同應用程式。

Selenium Multi Windows 9

程式碼實現

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;

public class NewTabsOpen {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Open a webpage in first window
      driver.get("https://tutorialspoint.tw/selenium/practice/check-box.php");

      // get text in first window 
      WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));
      System.out.println("Text: " + e.getText());

      //  Initiate the another Webdriver
      WebDriver newDriver = driver.switchTo().newWindow(WindowType.TAB);

      // Open a webpage in new tab
      driver.get("https://tutorialspoint.tw/selenium/practice/radio-button.php");

      // obtain text in other tab
      WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/h1"));
      System.out.println("Text in other tab: " + e1.getText());

      // quit the browser session
      driver.quit();
   }
}

輸出

Text: Check Box
Text in other tab: Radio Button

在這裡,我們捕獲了第一個視窗中的文字,並在控制檯中獲得了訊息 - 文字:複選框。然後開啟另一個新標籤頁,並在其中啟動一個應用程式。最後,我們在新標籤頁中獲取了文字,並在控制檯中獲得了一條訊息 - 其他標籤頁中的文字:單選按鈕

結論

這總結了我們關於 Selenium Webdriver 多視窗測試教程的全面介紹。我們從描述在 Selenium 中處理多個視窗的基本方法開始,並逐步介紹瞭如何使用 Selenium Webdriver 處理多個視窗的示例。這使您深入瞭解 Selenium Webdriver - 多視窗測試。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。

廣告

© . All rights reserved.