
- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 功能
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右擊
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和標籤頁
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 IFrame
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵上/下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援功能
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 其他
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 記錄和回放
- Selenium - 框架
- Selenium - 瀏覽器上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium 與其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven 和 Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium 其他概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 關鍵字驅動框架
- Selenium - 資料驅動框架
- Selenium - 混合驅動框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium WebDriver - 驅動程式會話
WebDriver 會話主要指瀏覽器的啟動和關閉。會話透過初始化驅動程式類物件預設建立。驅動程式類有兩種型別 - 本地驅動程式和遠端驅動程式。
要退出驅動程式,使用 quit() 方法。始終建議使用 quit() 而不是 close(),因為 close() 方法只能關閉焦點所在的瀏覽器,會話 ID 將返回為無效或過期,而 quit() 方法終止驅動程式會話,會話 ID 將返回為 null。
使用 Selenium WebDriver 進行會話處理
我們可以使用 Selenium WebDriver 和 TestNG 框架進行會話處理。為了觸發不同的會話,我們將在 TestNG XML 檔案中使用 parallel 屬性。
TestNG 執行配置在 TestNG XML 中完成。要建立多個會話,我們將在 XML 檔案中新增屬性 - parallel 和 thread-count。
thread-count 屬性控制以並行模式執行測試時要建立的會話數。parallel 屬性的值設定為 methods。
在 Selenium WebDriver 中獲取會話 ID
要獲取多個會話 ID,我們將建立三個並行執行的方法。我們將使用 RemoteWebdriver 類在此測試中獲取不同的會話 ID。
RemoteWebdriver 是一個實現 webdriver 介面的類。RemoteWebdriver 廣泛用於觸發遠端機器上的執行。會話 ID 是分配給每個會話的唯一編號。它具有 UUID 格式。
示例 - 獲取會話 ID
SessionHandling.java 中的程式碼實現
package Report; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.remote.SessionId; import org.testng.annotations.*; import org.openqa.selenium.remote.RemoteWebDriver; public class SessionHandling { WebDriver driver; @Test public void method1() { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); //launch URL driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php"); //get session ID SessionId s = ((RemoteWebDriver)driver).getSessionId(); System.out.println("Session Id is for method1: " + s); } @Test public void method2() { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); //launch URL driver.get("https://tutorialspoint.tw/selenium/practice/login.php"); //get session ID SessionId s = ((RemoteWebDriver)driver).getSessionId(); System.out.println("Session Id is for method2: " + s); } @Test public void method3() { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); //launch URL driver.get("https://tutorialspoint.tw/selenium/practice/accordion.php"); //get session ID SessionId s = ((RemoteWebDriver)driver).getSessionId(); System.out.println("Session Id is for method3: " + s); } }
testng.xml 檔案中的配置。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="All Test Suite" parallel="methods" thread-count="3"> <test verbose="2" preserve-order="true" name="TestNGTest.java"> <classes> <class name="Report.SessionHandling"></class> </classes> </test> </suite>
pom.xml 檔案中的依賴項。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SeleniumJava</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>16</maven.compiler.source> <maven.compiler.target>16</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.11.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.9.0</version> <scope>test</scope> </dependency> </dependencies> </project>
上面實現的專案結構如下所示:

輸出
Session Id is for method1: 63eae5bba42f97e30e7d01ef211f51e9 Session Id is for method2: 0b23f13aee68ca7c92de1cd93ee78c93 Session Id is for method3: e06fdaa8de91f4706e81566c854d1cf1 =============================================== Default Suite Total tests run: 3, Passes: 3, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0
控制檯中的結果顯示總共執行測試:3,因為有三個帶有 @Test 註解的方法 - method1()、method2() 和 method3()。此外,我們還獲得了在控制檯中帶有訊息的三個方法的會話 ID - method1 的會話 ID:63eae5bba42f97e30e7d01ef211f51e9,method2 的會話 ID:0b23f13aee68ca7c92de1cd93ee78c93 和 method3 的會話 ID:e06fdaa8de91f4706e81566c854d1cf1。
最後,收到訊息透過:3和程序已完成,退出程式碼為 0,表示程式碼成功執行。
以下連結提供了 TestNG 的詳細說明:
https://tutorialspoint.tw/testng/index.htm.
在 Selenium WebDriver 中獲取單個會話 ID
要獲取單個會話 ID,我們將使用 RemoteWebdriver 類開啟一個會話。每個會話的會話 ID 都是唯一的。RemoteWebdriver 是一個實現 webdriver 介面的類。RemoteWebdriver 廣泛用於觸發遠端機器上的執行。
示例 - 獲取單個 ID
HandlingSessionID.java 中的程式碼實現
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class HandlingSessionID { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will get the session id driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); //get webdriver session id SessionId s = ((RemoteWebDriver) driver).getSessionId(); System.out.println("Session Id is: " + s); // Closing browser driver.quit(); } }
pom.xml 檔案中的依賴項。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SeleniumJava</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>16</maven.compiler.source> <maven.compiler.target>16</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.11.0</version> </dependency> </dependencies> </project>
輸出
Session Id is: 78eae5bba42f97e30e7d01ef234f51e9 Process finished with exit code 0
在上面的示例中,我們獲得了帶有控制檯訊息的 webdriver 的會話 ID - 會話 ID 為:78eae5bba42f97e30e7d01ef234f51e9。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼成功執行。
本教程到此結束,我們對 Selenium WebDriver - 驅動程式會話進行了全面的講解。我們首先描述了 WebDriver 會話,close() 和 quit() 方法的區別,並逐步介紹了一個示例,說明如何使用 Selenium 獲取會話 ID。
這使您掌握了 Selenium WebDriver - 驅動程式會話的深入知識。明智的做法是不斷練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。