- 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.com/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.com/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.com/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.com/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.com/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 相关的其他内容,以加深您的理解并拓宽您的视野。
