
- 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 - 处理IFrames
- 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 - 讨论
110+ Selenium面试问答(2024)
Selenium是一个用于自动化测试的开源工具。它主要用于测试Web应用程序和自动化浏览器交互。类似于人类用户如何与浏览器交互,Selenium允许通过创建脚本与网页进行交互,例如网页导航、输入文本、单击按钮以及验证预期结果。开发人员可以使用强大的Selenium框架跨各种浏览器和平台测试Web应用程序。
希望在Selenium方面取得成功的求职者可以使用这110多个Selenium面试问答。无论您是应届毕业生还是经验丰富的专业人士,您都可以通过这些问答来测试您对Selenium的了解,以及您批判性思维和解决问题的能力。
Selenium面试问答
以下是Selenium中最常被问到的面试问题和答案:
1. 什么是Selenium?
Selenium是一个免费的开源Web自动化测试工具。它可以用来执行诸如点击、输入、鼠标和键盘操作等操作,这些操作作用于网页上的元素,并验证执行这些操作后产生的结果。
Selenium支持多种语言,例如Java、C#、Python、JavaScript等,并且可以在Windows、Mac、iPhone等各种平台上使用。它包含以下工具:
2. Selenium的组件是什么?
Selenium是一套包含多个工具的工具套件,每个工具都用于执行基于Web的测试中的各种项目。Selenium组件包括:
- Selenium 集成开发环境 (IDE) - 此工具主要用于基于Web的测试,并因其录制和回放功能而广受欢迎。
- Selenium 远程控制 (RC) - 此工具就像一个服务器,能够生成多种语言的脚本。它接收来自测试的命令,并将它们以Selenium核心JavaScript命令的形式定向到浏览器。
- Selenium WebDriver - 此工具可用于通过直接与Web浏览器交互来设计多种语言的测试脚本。
- Selenium Grid - 此工具用于在多个设备、浏览器、操作系统等上启用测试脚本的并行执行。
3. 使用Selenium有哪些优势?
使用Selenium的优势如下:
- 它是开源的,不需要任何许可费用。
- 它可以与多个浏览器一起使用,例如Chrome、Firefox、Safari、Edge等。
- 它用于在各种平台和设备上执行测试,例如Mac、Windows、iPhone、Android。
- 它可以与其他框架集成,例如TestNG、Junit等。此外,它可以与其他构建工具一起使用,例如Maven、Ant。
- 它可以用于执行无头和并行执行。
- 它定期更新,并且配置非常容易。Selenium拥有庞大的社区支持,如果遇到问题,可以提供帮助。
- 它可以与多种编程语言一起使用,例如Java、JavaScript、C#、Ruby、Python、Kotlin等。
- 它采用用户友好的界面,帮助用户创建和维护测试脚本。
- 它不是单个工具,而是一组工具,每个工具都可用于不同的测试目的。
4. 使用Selenium有哪些缺点?
使用Selenium的缺点如下:
- 它仅支持测试基于Web的应用程序。
- 它需要技术知识和编码技能。
- 不建议将其用于测试条形码、QR码扫描、验证码等场景。
- 它必须与其他工具集成才能生成报告。
- 在舒适地使用浏览器和平台的新版本之前,它需要一些时间。
- 它没有任何默认的对象存储库。
5. 自动化测试是什么意思?它的优势是什么?
自动化测试是使用测试工具将手动测试用例转换为自动化的过程。具有大量重复和冗余步骤的手动测试通常被选择作为自动化的合适候选者。但是,必须注意的是,不可能将100%的手动测试用例自动化。此外,自动化只能在稳定的软件上采用。
自动化测试的优势如下:
- 它可用于验证软件的功能和非功能需求。
- 它具有巨大的投资回报率,因为它节省了大量的时间、金钱和人力。
- 可以通过自动化实现并行执行。
- 它通过避免在执行测试用例中的人工干预来减少人为错误的范围。
- 通过自动化节省的时间可用于手动测试无法自动化的非常复杂的场景。
- 它允许测试人员在一个测试周期中多次执行回归和重复的测试用例。
- 它提供关于软件构建方式的更快反馈。
6. 为什么Selenium被广泛用于自动化?
Selenium被广泛用于自动化,因为它与多个浏览器、平台和语言兼容。它不需要任何许可费用。
7. 什么是Selenese命令?
Selenese是一组用于执行基于Web测试的命令。这些命令广泛用于Selenium IDE。Selenese命令分为三种类型:Actions、Accessors和Assertions。
8. Selenium 3.x和Selenium 4.x有什么区别?
Selenium 最新版本为 4.x 版本。此版本的架构和通信符合 W3C 标准。Selenium 3.x 版本的架构和通信基于 JSON Wire 协议。Selenium 4.x 添加了相对定位器,例如 above()、below()、toRightOf()、toLeftOf()、near() 以及相对定位器的链式调用。它还提供了对 ChromeDevTools 的访问,ChromeDevTools 用于调试、网络流量分析以及其他有助于自动化的功能。
Selenium 3.x 扩展了 Remote WebDriver,而 Selenium 4.x 扩展了 Chrome Driver。此外,3.x 版本需要触发 Hub 和 Node jar 来运行 Selenium Grid 测试,而 4.x 版本不再需要。Selenium 4.x 版本为 Chrome 和 Firefox 提供了 Selenium IDE 插件。最新版本的 Selenium 还包含了 side runner。
9. Selenium 2 和 Selenium 3 之间有什么区别?
Selenium 2 和 Selenium 3 之间有什么区别?答案:Selenium 3.0 是 Selenium 2.0 的改进版本,它修复了 Selenium 2.0 版本中的错误。Selenium 2.0 是 Selenium RC 和 Selenium WebDriver 的组合。但是,从 Selenium 3.0 版本开始,Selenium RC 已被淘汰。
Selenium 2.0 对移动测试的范围不大,而从 Selenium 3.0 开始,可以使用 Appium 进行移动测试,并且它拥有比 Selenium 2.0 更新和改进的 API。Selenium 2.0 现在已不再使用。
10. 列举一些用于自动化测试的工具
下面列出了一些用于自动化测试的工具:
- Selenium − 主要用于基于 Web 的自动化测试。
- Cypress − 主要用于基于 Web 的自动化测试和 API 自动化测试。
- Playwright − 主要用于基于 Web 的自动化测试。
- Appium − 主要用于移动自动化测试。
- UFT − 是一款商业授权的 Web 自动化测试工具。
- JMETER − 主要用于负载测试。
11. Selenium 中有哪些不同类型的定位器?
Selenium 通过其定位策略来识别网页上的元素。Selenium 中提供的一些定位器如下所示:
- Css
- Xpath
- Link Text
- Partial Link Text
- Id
- Name
- Class Name
- Tagname
从 Selenium 的最新版本开始,还提供了其他类型的定位器,称为相对定位器,它们根据另一个元素的位置来识别元素。Selenium 中的一些 相对定位器 如下所示:
- above()
- below()
- toRightOf()
- toLeftOf()
- near()
- 定位器链式调用
12. 使用 Selenium 可以进行哪些不同类型的测试?
可以使用 Selenium 执行的不同类型的测试如下所示:
13. 什么是 XPath?
XPath 是一种查询语言,用于根据其 XML 路径来检测元素。它用于遍历 XML 文档的 Web 元素及其属性。有关 XPath 的更多信息,请访问以下链接:
XML 是一种可扩展标记语言,用于存储、管理和发送数据。数据以类似于 HTML 的键值对格式存储。XPath 分为两种类型:相对 XPath 和绝对 XPath。
14. 什么是绝对 XPath 和相对 XPath?
绝对 XPath 用双斜杠 // 表示。它从文档的起始节点开始识别所需的元素。但是,如果从根到当前节点的任何元素的路径发生更改,则现有的绝对 XPath 将无法再识别该特定元素。
示例
Absolute Xpath Used: /html/body/main/div/div//div/ul/li[2]/input
相对 XPath 用单斜杠 / 表示。它从 DOM 中的任何位置开始识别所需的元素。
示例
Relative Xpath Used: //*[@id='gg_bf_1']//parent::ul
15. driver.close() 和 driver.quit() 之间有什么区别?
driver.close() 用于关闭当前处于焦点的浏览器窗口。如果打开了多个选项卡和窗口,driver.close() 不会关闭所有选项卡和窗口。它只会关闭当前活动的选项卡或窗口。如果在 driver.close() 之后再次调用 driver 实例,则会抛出无效会话 ID 异常。
driver.quit() 用于关闭所有打开的窗口和选项卡并终止驱动程序会话。如果在 driver.quit() 之后再次调用 driver 实例,则会抛出会话 ID 为 null 的异常。
16. 如何使用 Selenium 等待页面完全加载?
我们可以使用以下方法使用 Selenium 等待页面完全加载:
使用隐式等待 − 在这种方法中,在每个页面加载时,它都会等待页面完全加载。
例如 −
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
使用 JavaScript 函数 − 在这种方法中,调用 JavaScript return document.readyState 直到页面加载后返回 complete。
例如 −
if (document.readyState == "complete"){ alert("Page is loaded completely"); }
17. Selenium 中的异常测试是什么意思?
异常测试是指预期从测试类接收异常作为输出的测试。它查找 @Test 注解,然后查找括号内的预期异常名称。
例如 −
@Test public void test1(expectedException = NoSuchElementException.class){ // description of test method }
上述异常测试用于无法识别的元素。
18. 为什么 Selenium 是测试基于 Web 的应用程序的流行选择?
Selenium 是测试基于 Web 的应用程序的流行选择,因为它是一个相对简单的工具,可以使用 JavaScript 实现。它与多种浏览器、平台和语言兼容。它可以轻松地与其他第三方工具(如 TestNG、Junit、Maven 等)集成。
19. 列举与 Selenium 一起使用的各种注解
与 Selenium 一起使用的各种注解通常是 TestNG 测试自动化框架的一部分。与 Selenium 一起使用的一些注解如下所示:
- @BeforeSuite
- @AfterSuite
- @BeforeClass
- @AfterClass
- @BeforeSuite
- @AfterSuite
- @BeforeTest
- @AfterTest
20. Selenium 中的 TestNG 是什么?
TestNG 是一个单元测试自动化框架,可以与 Selenium 集成。它允许对测试进行分组、设置优先级、并行测试等。
21. 什么是同源策略?
同源策略是应用于浏览器的安全特性,它不允许属于一个源的文档或脚本被另一个源的文档或脚本访问。它包括协议、来源和端口。
因此,如果两个网页都来自相同的源,则 Web 浏览器允许来自特定网页的脚本访问另一个网页。同源策略保护网页的安全数据免受来自不同源的另一个网页的恶意攻击。
为了避免同源策略,采用了代理注入技术。在这种方法中,Selenium 服务器充当配置为 HTTP 代理的客户端,并诱使浏览器接受 Selenium Core 和被测应用程序属于相同的源。
22. 为什么选择 Selenium 而不是 UFT?
主要由于以下原因,Selenium 比 UFT 更受欢迎:
- UFT 是一款商业授权工具,需要付费,而 Selenium 是免费的开源工具。
- UFT 仅用于测试 Windows 应用程序,而 Selenium 可以在多个平台上进行测试,例如 Windows、Linux、Mac、iPhone、Blueberry 等。
- UFT 是一个桌面应用程序,而 Selenium 是一组 API。
- UFT 仅支持 Visual Basic 脚本,而 Selenium 支持多种编程语言,例如 Java、C#、Python、JavaScript、Ruby、Kotlin 等。
- 如果测试团队正在寻找免费的开源工具,并且精通编程语言,那么 Selenium 始终比 UFT 更好。但是,如果测试团队只了解 VB 脚本,并且需要在 Windows 平台上进行验证,则可以选择 UFT。
23. 什么是 POM 或页面对象模型,以及它的好处是什么?
POM,也称为 页面对象模型,是一种设计模式,可用于通过将元素和测试用例流程分离到各个类文件中来创建 Selenium 测试脚本。POM 的好处是,通过分离 Web 元素和测试用例,它可以提高代码的可读性、可重用性和易维护性。
24. Selenium 中有哪些不同类型的等待?
Selenium 中可用的不同类型的等待如下所示:
隐式等待
它是一种默认类型,Selenium 在抛出异常之前会等待特定时间。
例如 −
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
显式等待
此等待用于通知 Webdriver 在抛出异常之前等待特定条件特定时间。
例如 −
WebDriverWait w = new WebDriverWait(driver, 15); wt.until(ExpectedConditions.invisibilityOfElementLocated (By.xpath("//*[@class='btn']")));
流畅等待
此等待用于通知 Webdriver 在抛出异常之前,以给定的间隔等待特定条件,在此期间检查该条件特定时间。
例如 −
Wait w = new FluentWait(driver).withTimeout(25, TimeUnit.SECONDS) .pollingEvery(15, TimeUnit.SECONDS) .ignoring(ElementNotInteractableException.class); wt.until(ExpectedConditions.titleIs("Online Tutorialspoint"));
25. Selenium 中 findElement() 和 finElements() 方法之间有什么区别?
Selenium 中 findElement() 和 findElements() 方法之间的区别如下所示:
- findElement() 返回一个 WebElement,而 findElements() 返回 WebElement 列表。
- 如果没有匹配的元素,findElement() 将抛出 NoSuchElementException,而 findElements() 将返回大小为 0 的数组列表。
26. 什么是 Selenium WebDriver?
Selenium WebDriver 是 Selenium 工具套件的一部分,从 Selenium 2 版本开始提供。它可用于使用多种语言、平台和浏览器来自动化 Web 应用程序的测试。
27. Selenium WebDriver 是库的一部分吗?
Selenium WebDriver 是一个免费的开源库,用于创建基于 Web 的应用程序的自动化测试脚本。
28. Selenium 中使用了哪些不同类型的导航命令?
Selenium 中使用的不同类型的导航命令如下所示:
driver.navigate().to("应用程序的 URL") − 它将导航到应用程序的 URL。
driver.navigate() .to("https://tutorialspoint.com/selenium/practice/login.php");
driver.get("应用程序的 URL") − 它将启动浏览器以打开应用程序的 URL。
driver.get("https://tutorialspoint.com/selenium/practice/login.php");
driver.navigate().back() − 它将根据浏览器历史记录上下文跳转到上一页。
driver.navigate().back();
driver.navigate().forward() − 根据浏览器历史上下文,跳转到下一页。
driver.navigate().forward();
driver.navigate().refresh() − 重新加载网页。
driver.navigate().refresh();
29. 什么是数据驱动框架?
数据驱动框架是一种测试数据与测试脚本分离的框架。它允许你使用多个数据集执行测试用例。此外,如果需要对测试用例进行更改,可以在不接触测试数据的情况下完成。
30. Selenium支持哪些编程语言?
Selenium可以与多种编程语言一起使用,例如Java、C#、Python、JavaScript、Ruby、Kotlin、Perl等。
31. driver.get()和driver.navigate.to()有什么区别?
driver.get()和driver.navigate.to()的区别如下:
- driver.get()方法启动URL并等待页面完全加载,而driver.navigate.to()方法启动URL而无需等待页面完全加载。
- driver.get()方法是WebDriver接口的一部分,而driver.navigate.to()方法是Navigation接口的一部分。
- driver.get()方法不允许浏览器在浏览器历史记录中向前或向后移动以及刷新,而driver.navigate.to()方法允许所有这些操作。
32. Selenium无法自动化哪些场景?
Selenium无法自动化的场景如下:
- 它无法自动化包含位图比较的场景。
- 它无法自动化验证码场景。
- 它无法自动化条形码场景。
- Selenium可以自动化性能测试,但这并不是推荐的做法。
- 它无法自动化视频流场景。
33. 列举一些可以与Selenium集成的开源框架
一些可以与Selenium集成的开源框架如下:
34. 在Selenium中使用driver.get()方法会发生什么?
driver.get()方法启动浏览器,打开应用程序URL,并等待页面完全加载。
语法
driver.get("https://tutorialspoint.com/selenium/index.htm");
要了解更多关于driver.get()的信息,请访问链接 Selenium WebDriver浏览器导航
35. 打开应用程序URL的driver.get()方法的替代方法是什么?
打开应用程序URL的driver.get()方法的替代方法是driver.navigate.to()方法。它启动URL而无需等待页面完全加载。
语法
driver.navigate() .to("https://tutorialspoint.com/selenium/index.htm");
36. 什么是关键字驱动框架?
关键字驱动框架是一种实现逻辑和代码被隐藏的框架。它是数据驱动框架的一种扩展。在关键字驱动框架中,通过按特定顺序排列一组关键字来创建测试用例。
37. 如何使用Selenium上传文件?
我们可以使用Selenium上传文件。如果文件在同一台机器上或放置在映射驱动器中,我们需要将文件位置传递给sendKeys()方法。
语法
WebDriver driver = new ChromeDriver(); // identify element WebElement m = driver.findElement(By.xpath("value of xpath locator")); // obtain file path to be uploaded File file = new File("./Pictures.png"); System.out.println("File path to be uploaded: " + file.getAbsolutePath()); // uploading file with path m.sendKeys(file.getAbsolutePath());
38. getWindowHandle()方法和getWindowHandles()方法有什么区别?
getWindowHandle()方法和getWindowHandles()方法的区别如下:
- getWindowHandles()方法返回当前打开的所有窗口和标签页的句柄ID,而getWindowHandle()方法返回当前处于焦点的窗口或标签页的句柄ID。
- getWindowHandles()方法使用集合Set返回当前打开的所有窗口和标签页的句柄ID,而getWindowHandle()方法返回当前处于焦点的窗口或标签页的句柄ID(字符串)。
39. WebDriver driver = new FireFoxDriver()是什么意思?
在代码语句WebDriver driver = new FireFoxDriver()中,WebDriver是一个接口,FirefoxDriver是一个类。在这里,我们使用Java中的new关键字创建了FirefoxDriver类的对象。然后将该对象赋值给WebDriver接口的引用变量driver。
此语句主要用于替代FireFoxDriver driver = new FireFoxDriver(),因为在自动化框架中,我们提供了在多个浏览器中执行测试的功能。为了实现此功能,只需要将浏览器名称传递给可重用的方法,该方法将创建正确类的对象,最后将其赋值给WebDriver接口。
也可以使用语句FireFoxDriver driver = new FireFoxDriver(),但是为了优化代码,我们通常使用WebDriver driver = new FireFoxDriver()。此外,此语句只能使用并调用适用于Firefox浏览器的FireFoxDriver的方法。
我们不能创建特定驱动程序(例如FirefoxDriver)的对象并将其赋值给另一个浏览器驱动程序(例如ChromeDriver)。因此,语句ChromeDriver driver = new FireFoxDriver()是错误的。但是我们可以将FirefoxDriver、ChromeDriver等的对象赋值给WebDriver接口。语句WebDriver driver = new WebDriver()不被使用,因为WebDriver是一个接口,我们不能创建接口的对象。
40. WebDriver的超接口是什么?
根据类和接口的层次结构,SearchContext是WebDriver的超接口。Selenium中类和接口的层次结构如下图所示:

41. 如何在Selenium中向文本框输入文本?
我们可以使用以下方法在Selenium中向文本框输入文本:
使用sendKeys()方法
Webdriver driver = new ChromeDriver(); driver.findElement(By.id("value of id")).sendKeys("value to be entered");
使用JavaScript执行器
Webdriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id")); JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("arguments[0].setAttribute('value', 'Selenium')", elm);
使用Actions
Webdriver driver = new ChromeDriver(); Actions a = new Actions(driver); a.sendKeys(e, "value entered").perform();
42. 我们能否使用Selenium测试API和Web服务?
Selenium主要用于测试Web应用程序,不用于API测试。只有Selenium库本身无法支持API或Web服务测试,为此需要额外的库,例如Rest Assured。
43. 我们是否需要使用Selenium Server来执行Selenium Webdriver脚本?
Selenium Server用于执行Selenium Webdriver脚本,借助Selenium Grid在多个平台、浏览器和机器之间触发并行执行。如果要在远程机器上执行测试,则需要远程服务器来执行测试脚本。
44. Selenium中的WebElement是什么意思?
一个WebElement是Selenium中的一个接口,它指的是网页上的一个元素。它用于对页面上的这些元素执行操作。
45. 如何使用xpath定位元素的第n个子元素?
我们可以使用以下方法使用xpath定位元素的第n个子元素:
使用xpath表达式 - //parent/childElement[n]
//div/a[5]
上面的xpath标识父div的第五个锚点子元素。
在xpath表达式中使用子轴 - //parent/child::tagname[n]
Webdriver driver = new ChromeDriver(); WebElement parnt = driver.findElement(By.xpath("//ul[@name='toc']")); //identify all children nodes with ./child::* expression in xpath List<WebElement> c = parnt.findElements(By.xpath("./child::*"));
46. Selenium中有对象库吗?
Selenium中没有默认的对象库。但是,所有元素定位器都可以在单独的类文件、属性文件等中定义,以便于维护和从测试用例中访问。
47. 如何在Selenium IDE中插入断点?
我们在Selenium IDE中插入断点来调试测试步骤。可以通过以下方法应用断点:
- 单击测试中的行号以添加断点。
- 按“禁用断点”禁用所有断点。再次按下相同的按钮即可启用断点。
- 右键单击任何步骤,然后按“切换断点”。
48. 如何在Selenium中单击链接?
我们可以使用以下方法在Selenium中单击链接:
使用click()方法
Webdriver driver = new ChromeDriver(); driver.findElement(By.linkText("Home")).click();
使用JavaScript执行器
Webdriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.xpath("//div/ul/li[2]/a")); JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("arguments[0].click();", elm);
49. 如何在Selenium中处理警告框和弹出窗口?
我们可以使用Alerts接口在Selenium中处理警告框和弹出窗口。最初,驱动程序焦点位于主网页上,要访问警告框,我们需要使用switchTo().alert()方法将驱动程序焦点从主页面切换到警告框。
Alert接口的方法如下:
- driver.switchTo().alert().accept() − 接受警告框并单击“确定”。
- driver.switchTo().alert().dismiss() − 关闭警告框并单击“取消”。
- driver.switchTo().alert().getText() − 获取警告框中的文本。
- driver.switchTo().alert().sendKeys("value") − 在警告框中的文本字段中输入文本。
50. Selenium IDE中assert和verify命令有什么区别?
assert和verify命令用于在测试用例中添加验证点。如果assert命令中检查的条件不满足,则执行在不匹配点停止。否则继续执行。如果verify命令中检查的条件不满足,则执行仍在不匹配点之后继续。
51. 如何在Selenium中向下滚动网页?
我们可以使用JavaScript执行器在Selenium中向下滚动网页。要向下滚动到特定高度,可以使用scrollBy方法。
语法
JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("window.scrollBy(0,700)", "");
要向下滚动到页面底部,同样可以使用scrollBy方法。
语法
JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript ("window.scrollBy(0,document.body.scrollHeight)");
要向下滚动到网页上元素可见。
语法
WebElement t = driver.findElement (By.xpath("<value of xpath>")); // scrolling to view of an element JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("arguments[0].scrollIntoView();", t);
52. Selenium中getText()和getAttribute()方法有什么区别?
getText()方法用于获取网页上元素的文本。getAttribute()方法用于获取DOM中元素的运行时值,要获取的值作为参数传递。两者都被称为Selenium中的WebElement命令。
语法
WebDriver driver = new ChromeDriver(); // Identify the element WebElement elm = driver.findElement (By.xpath("<value of xpath>")); // text of the element String text = elm.getText()); // Identify another element WebElement elm1 = driver.findElement (By.xpath("<value of id>")); // enter text in input box elm1.sendKeys("Selenium Interview"); // Obtain the value entered String text = elm1.getAttribute("value");
53. 如何获取元素的css属性?
我们可以使用getCSSValue()方法获取元素的css属性。要获取像color或border-color这样的css属性,我们需要将它们作为参数传递给此方法。
语法
WebDriver driver = new ChromeDriver(); // Identify the element WebElement element = driver.findElement (By.xpath("<value of xpath>")); // Get background color String color = element.getCssValue("background-color"));
54. Page Factory中@FindBy注解有什么作用?
在Page Factory中,@FindBy注解用于使用xpath、css、tagname、link text、partial link text、name、class name等定位器标识、声明和对网页上的web元素执行操作。
语法
FindBy(id = "name") WebElement elm;
55. 混合框架是什么意思?
混合框架结合了数据驱动和关键字驱动框架的功能。使用混合框架构建的测试脚本更健壮,并且更容易维护。
56. 如何在Selenium中处理框架?
我们可以处理Selenium中的框架。最初,驱动程序焦点位于主网页上,要访问框架,我们需要使用switchTo().frame()方法将驱动程序焦点从主页面切换到框架。
框架上一些基本的重载方法如下:
switchTo.frame(args) − 将框架索引号作为参数传递。框架的起始索引为零。
driver.switchTo.frame(0), switching to the first frame.
switchTo.frame(args) − 将框架ID或名称作为参数传递。
driver.switchTo.frame(“id-value”), switching to the frame having id or name value as id-value.
switchTo.frame(args) − 将框架webelement作为参数传递。
driver.switchTo.frame(“id-name”), switching to the frame having webelement value as id-name.
driver.switchTo.defaultContent() − 将驱动程序上下文从框架切换到主页面。
57. 如何清除 Selenium 中输入框的文本?
我们可以使用 clear() 方法清除 Selenium 输入框 中的文本。首先,使用 sendKeys() 方法在输入框中输入文本,然后借助 clear() 方法清除。
58. 如何获取 Selenium 中元素属性的值?
我们可以使用 getAttribute() 方法获取 Selenium 中元素属性的值。例如,我们可以使用 getAttribute() 方法获取文本框中输入的值,然后将值作为参数传递给该方法。
语法
// Identify element WebElement textBox = driver.findElement (By.className("<value of class name>")); // Enter text in text box textBox.sendKeys("Interview Questions"); // Obtain the value entered String text1 = textBox.getAttribute("value");
59. 如何在 Selenium 中提交表单?
我们可以使用 Selenium 提交表单。表单元素的标签名为 form,其 type 属性的值等于 submit。要提交表单,我们可以采用以下方法:
使用 submit() 方法
WebDriver driver = new ChromeDriver(); // Identify element within form WebElement inputBox = driver.findElement (By.xpath("<value of xpath>")); inputBox.sendKeys("Name"); // Submitting form with submit() WebElement button = driver.findElement (By.xpath("<value of xpath>")); button.submit();
使用 click() 方法
WebDriver driver = new ChromeDriver(); // Identify element within form WebElement inputBox = driver.findElement (By.xpath("<value of xpath>")); inputBox.sendKeys("Name"); // Submitting form with click() WebElement button = driver.findElement (By.xpath("<value of xpath>")); button.click();
60. Selenium WebDriver 的架构是什么?
Selenium WebDriver 4.x 及更高版本完全基于 W3C WebDriver 协议。早期,客户端与服务器之间的 HTTP 请求和响应是通过 JSON Wire 协议进行的。但是,从 4.x 版本开始,Selenium 客户端库与浏览器驱动程序的通信通过 W3C WebDriver 协议进行。
Web 浏览器及其驱动程序已经兼容 W3C,因此 Selenium WebDriver 采用了相同的协议,以便在整个架构中遵循标准的通用协议。这也为测试带来了稳定性,并且可以将通用代码用于多个浏览器。此外,Selenium 中使用的 Actions API 现在提供了额外的功能,例如双击按键、放大/缩小等。
61. 列举一些 Selenium WebDriver 中遇到的异常。
在使用 Selenium 时,我们会遇到许多异常。当找不到元素,或者预期结果与实际结果不符,或者由于其他原因时,就会发生异常。代码会生成异常以逻辑的方式结束测试。
62. 列举一些常用的用于非功能测试的自动化测试工具。
常用的用于非功能测试的自动化测试工具如下:
- LoadRunner
- JMeter
- Silk Performer
- Gatling
- Apache (此处应指具体的工具,例如 Apache JMeter,原文略显模糊)
- Neoload
63. XPath 定位器中 / 和 // 的区别是什么?
单个斜杠 / 指的是绝对 XPath,双斜杠 // 指的是相对 XPath。绝对路径从根节点开始定位元素,而相对路径可以从 DOM 中的任何位置开始查找元素。
示例
Absolute Xpath Used: /html/body/main/div/div/ul/li[3]/a Relative Xpath Used: //ul[@id='bn_jn_w']//child::li[3]/a
64. 列举 Selenium 中使用的各种类型的 WebDriver API。
Selenium 中使用的各种类型的 WebDriver API 如下:
- ChromeDriver
- FirefoxDriver
- InternetExplorerDriver
- HTMLUnitDriver
- SafariDriver
- iPhoneDriver
- RemoteWebDriver
- AndroidDriver
- iPhoneSimulatorDriver
- EventFiringWebDriver
65. 如何处理 Selenium 中的同步问题?
我们可以通过对测试应用隐式等待、显式等待和流畅等待来处理 Selenium 中不同的同步问题。这些是包含在测试中的等待,以便 Selenium 工具及其与 Web 元素的交互并行进行,并且这两个实体之间没有延迟。
66. 编写在 Chrome 浏览器中启动应用程序的代码。
在 Chrome 浏览器中启动应用程序的代码如下:
代码实现
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 LaunchChromeBrowser { 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 driver.get("https://tutorialspoint.com/selenium/practice/radio-button.php"); System.out.println("Get the page URL: " + driver.getTitle()); // Closing browser driver.quit(); } }
67. 编写在 Firefox 浏览器中启动应用程序的代码。
在 Firefox 浏览器中启动应用程序的代码如下:
代码实现
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; public class LaunchFirefoxBrowser { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new FirefoxDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage driver.get("https://tutorialspoint.com/selenium/practice/radio-button.php"); System.out.println("Get the page URL: " + driver.getTitle()); // Closing browser driver.close(); } }
68. 如何获取 Selenium 中元素的文本?
我们可以使用 getText() 方法获取 Selenium 中元素的文本。
语法
// Identify element WebElement textBox = driver.findElement (By.className("<value of class name>")); // Obtain the text String text1 = textBox.getText();
69. 我们能否使用 Selenium WebDriver 测试移动应用程序?
不能,我们不能使用 Selenium WebDriver 测试移动应用程序。通常我们使用 Appium(它建立在 Selenium 之上)来测试移动应用程序。
70. 如何在 Selenium 中执行拖放操作?
我们可以借助 Actions 类中的 dragAndDrop() 和 dragAndDropBy() 方法在 Selenium 中执行拖放操作。dragAndDrop 方法有两个参数:源和目标定位器。dragAndDropBy 方法有三个参数:源定位器、水平方向的 x 偏移值和垂直方向的 y 偏移值。
71. 如何在 Selenium 中处理 Cookie?
我们可以处理 Selenium 中的Cookie。我们可以获取、添加和删除 Cookie。Cookie 的基本方法如下:
- addCookie() - 将 Cookie 添加到当前会话。
- getCookieNamed() - 获取所有已创建 Cookie 中具有特定名称的 Cookie。
- getCookies() - 获取当前会话的所有 Cookie。
- deleteCookieNamed() - 删除具有特定名称(作为参数传递)的 Cookie。
- deleteCookie() - 删除具有 Cookie 对象(作为参数传递)的 Cookie。
- deleteAllCookies() - 删除当前浏览器会话中的所有 Cookie。
72. 如何在 Selenium 中处理多个窗口?
我们可以处理 Selenium 中的多个窗口。最初,驱动程序的焦点在主窗口上,要访问另一个窗口,我们需要使用 switchTo().window("子窗口句柄 ID") 方法将驱动程序焦点从主窗口切换到另一个窗口。
与窗口交互的方法如下:
- getWindowHandles():它使用集合 Set 返回当前打开的所有窗口的句柄 ID。
- getWindowHandle() 方法返回作为字符串的当前焦点窗口的句柄 ID。
73. 如何在 Selenium 中不使用 sendKeys() 方法向文本框输入文本?
我们可以不使用 sendKeys() 方法向文本框输入文本。这也可以使用 JavaScript Executor 完成。
语法
Webdriver driver = new ChromeDriver(); WebElement element = driver.findElement(By.name("value of name")); JavascriptExecutor j = (JavascriptExecutor) driver; j.executeScript("arguments[0].setAttribute('value', 'Interview')", element);
74. 如何在 Selenium 中执行鼠标悬停操作?
我们可以使用Actions 类的 moveToElement() 方法在 Selenium 中执行鼠标悬停操作。
语法
WebDriver driver = new ChromeDriver(); // identify element WebElement m1 = driver.findElement (By.xpath("//div/div[3]/nav/div/a")); Actions actions = new Actions(driver); actions.moveToElement(m1).click().build().perform();
75. 什么是 Page Factory?
Page Factory 是一个辅助实现页面对象模型的类。它是页面对象模型的扩展,可从 WebDriver 类获得。
76. 如何在 Selenium 中选择下拉列表中的选项?
我们可以使用 Select 类的各种方法来选择下拉列表中的选项。网页中的下拉列表由 select 标签名表示,其选项由 options 标签名表示。Selenium WebDriver 中有多种 Select 类方法。它们列在下面:
getOptions() - 返回所有选项的列表。
WebDriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id”)) Select sel = new Select(elm); List<WebElement> elm1 = sel.getOptions();
getFirstSelectedOption() - 返回下拉列表中选定的选项。
WebDriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id”)) Select sel = new Select(elm); options = sel.getFirstSelectedOption();
isMultiple() - 如果下拉列表允许多选,则返回 true 值。
WebDriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id”)) Select sel = new Select(elm); boolean result = sel.isMultiple();
selectByIndex() - 将要选择的下拉列表选项的索引作为参数传递。索引从 0 开始。
WebDriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id”)) Select sel = new Select(elm); sel.selectByIndex(0);
selectByValue() - 将要选择的下拉列表选项的 value 属性作为参数传递。
WebDriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id”)) Select sel = new Select(elm); sel.selectByValue("option 5");
selectByVisibleText() - 将要选择的下拉列表选项的可视文本作为参数传递。
WebDriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id”)) Select sel = new Select(elm); sel.selectByVisibleText("Interview");
deselectByVisibleText() - 将要取消选择的下拉列表选项的可视文本作为参数传递。
WebDriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id”)) Select sel = new Select(elm); sel.deselectByVisibleText("Interview");
deselectByValue() - 将要取消选择的下拉列表选项的 value 属性作为参数传递。
WebDriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id”)) Select sel = new Select(elm); sel.deselectByValue("option 5");
deselectByIndex() - 将要取消选择的下拉列表选项的索引作为参数传递。
WebDriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id”)) Select sel = new Select(elm); sel.deselectByIndex(0);
deselectAll() - 取消选择所有选定的选项。
WebDriver driver = new ChromeDriver(); WebElement elm = driver.findElement(By.id("value of id”)) Select sel = new Select(elm); sel.deselectAll();
getAllSelectedOptions() - 返回所有选定的选项。
77. 如何处理 Selenium 中的隐藏元素?
我们可以使用 JavaScript Executor 来处理 Selenium 中的隐藏元素。
语法
WebDriver driver = new ChromeDriver(); // Javascript executor class JavascriptExecutor j = (JavascriptExecutor) driver; // identify hidden element and set value j.executeScript ("document.getElementById('hidden-field').value='Interview';"); String st = (String) j.executeScript("return document.getElementById('hidden-field').value");
78. 在 Selenium 中同时使用 Thread.sleep() 和 WebDriver 等待会发生什么?
Thread.sleep() 会停止测试执行一段时间,WebDriver 等待也会在满足条件之前暂停执行一段时间。如果我们同时使用两种等待,则会增加总的执行时间。
79. 在 Selenium 中同时使用隐式等待和显式等待会发生什么?
不建议在同一个 Selenium 测试中同时添加隐式等待和显式等待。在一个驱动程序会话中同时添加两者会导致不确定的等待时间,例如,将 15 秒的显式等待和 10 秒的隐式等待组合在一起,可能会在 20 秒后导致超时错误。
隐式等待在一个测试脚本中只使用一次,并适用于每个测试步骤。但是,显式等待用于等待特定条件。当同时应用两种等待时,显式等待会覆盖隐式等待。
80. 什么是 StaleElementReferenceException 以及如何避免它?
当 Selenium WebDriver 尝试查找以前可用的但当前已不可用或已从 DOM 中删除的 Web 元素时,会抛出 StaleElementReferenceException。这可能是由于页面刷新,或者元素或其父元素已被删除。因此,当 WebDriver 无法与该元素交互时,就会抛出此异常。
可以通过以下措施避免 StaleElementReferenceException:
- 重新刷新页面
- 等待该元素
- 再次尝试访问相同的元素
81. 如何使用 Selenium 启动不同的浏览器?
Selenium 用于启动不同的浏览器,例如 Chrome、Firefox、Edge、Safari 等。
语法
// For Chrome WebDriver driver = new ChromeDriver(); // For Firefox WebDriver driver = new FirefoxDriver(); // For Edge WebDriver driver = new EdgeDriver(); // For Safari WebDriver driver = new SafariDriver();
82. Selenium 中的 Actions 类是什么?
Selenium 中的Actions 类用于执行键盘和鼠标操作。这些操作包括双击、右击、单击、按键、拖放等。Actions 类包含高级用户交互 API。
83. Selenium 在测试方面还能做出哪些贡献?
Selenium 在测试方面还能做出更多贡献的领域如下:
- 移动测试
- 性能测试
- API 测试
- 默认报告生成
- 视觉测试
- 跨浏览器测试
84. 什么是 testng.xml 文件?
testng.xml 文件是 TestNG 单元测试框架的一部分。它用作配置文件来运行TestNG 测试。借助 testng.xml 文件,可以对测试进行分组、包含或排除、并行执行等。
85. Selenium 中的断言是什么?
断言是添加到测试用例中的验证点,用于验证软件是否按预期正确工作。断言添加到自动化测试用例中,以确认测试用例是否通过或失败。断言方法可从 TestNG 和 JUnit 库中获得。
86. 如何在 Selenium Webdriver 中捕获屏幕截图?
Selenium Webdriver 用于在测试用例失败时捕获屏幕截图。它有助于调试失败的测试步骤。getScreenShotAs 方法(TakeScreenshot 接口的一部分)用于捕获屏幕截图。
语法
Webdriver driver = new ChromeDriver(); WebElement l = driver.findElement(By.id("id")); l.click(); // take screenshot of link and store in a location File s = l.getScreenshotAs(OutputType.FILE); FileUtils.copyFile(s, new File("location of screenshot"));
87. 如何使用 Selenium 在文本框中按下 ENTER 键?
我们可以使用 sendKeys() 方法和 Key 类在文本框中按下 ENTER 键。Keys.ENTER 将作为参数传递给 sendKeys() 方法以执行此操作。
语法
WebDriver driver = new ChromeDriver(); // Identify element WebElement m = driver.findElement(By.name("huj")); m.sendKeys("Interview"); // Type enter with sendKeys() m.sendKeys(Keys.ENTER);
88. 如何在 Selenium 中刷新网页?
我们可以在 Selenium 中通过以下几种方式刷新网页:
使用刷新方法
driver.get("https://tutorialspoint.com/selenium/index.htm"); driver.navigate().refresh();
使用 sendKeys 方法
driver.get("https://tutorialspoint.com/selenium/practice/text-box.php"); WebElement t = driver.findElement(By.id("fullname")); t.sendKeys(Keys.F5);
使用 get 方法
driver.get("https://tutorialspoint.com/selenium/practice/text-box.php"); driver.get(driver.getCurrentUrl());
使用 navigate.to() 方法
driver.get("https://tutorialspoint.com/selenium/practice/text-box.php"); driver.navigate().to(driver.getCurrentUrl());
使用带 ASCII 的 sendKeys 方法
driver.get("https://tutorialspoint.com/selenium/practice/text-box.php"); WebElement t = driver.findElement(By.id("fullname")); t.sendKeys("\uE035");
89. 如何在 Selenium 中断言页面的标题?
我们可以借助 TestNG 断言在 Selenium 中断言页面的标题。
代码实现
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.testng.Assert; public class AssertBrowserTitle { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new FirefoxDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage driver.get("https://tutorialspoint.com/selenium/practice/alerts.php"); System.out.println("Get the page URL: " + driver.getTitle()); // obtain page title and compare String actTitle = driver.getTitle(); String title = “Selenium Practice - Alerts"; Assert.assertEquals (actualTitle, expectedTitle); // Closing browser driver.close(); } }
90. 是否应该使用 Selenium 验证验证码?
不,不应该使用 Selenium 验证验证码。
91. 什么是 Selenium Maven 项目?
Selenium Maven 项目是指使用 Maven 作为构建工具并获取运行 Selenium 项目所需正确依赖项的项目。它还有助于为源代码、可重用文件、测试代码等创建正确的项目结构。因此,Maven 有助于实现更轻松的代码维护、可读性和可靠性。
92. 为什么在 Selenium 中使用 switchTo() 方法?
switchTo() 方法用于将驱动程序焦点从主页面切换到另一个窗口、选项卡、警报、框架等。
93. 如何在 Selenium 中设置浏览器窗口大小?
我们可以通过以下方法在 Selenium 中设置浏览器窗口大小:
使用最大化方法
driver.manage().window().maximize();
使用 setSize 方法
Dimension dimension = new Dimension(500, 500); driver.manage().window().setSize(dimension);
使用 JavaScript 执行器方法
((JavascriptExecutor)driver).executeScript("window.resizeTo(500, 400)");
94. 列出可以与 Selenium 集成以进行持续集成的自动化测试工具
一些可以与 Selenium 集成以进行持续集成的自动化测试工具如下:
- Jenkins
- CircleCI
- AWS CodePipeline
- Azure DevOps
- Bitbucket
95. 隐式等待的缺点是什么?
隐式等待是应用于测试用例中每个测试步骤的全局等待。它的默认等待时间为零,会减慢测试的执行速度。此外,如果在隐式等待指定的给定时间后检测到元素,则可能导致错误的失败。
96. 编写在 Internet Explorer 浏览器中启动应用程序的代码
在 Internet Explorer 浏览器中启动应用程序的代码如下:
代码实现
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; import java.util.concurrent.TimeUnit; public class LaunchIEBrowser { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new InternetExplorerDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage driver.get("https://tutorialspoint.com/selenium/practice/modal-dialogs.php"); System.out.println("Get URL: " + driver.getTitle()); // Closing browser driver.quit(); } }
97. 如何在 Selenium 中返回浏览器?
我们可以使用 navigate().back() 方法在 Selenium 中返回浏览器。它可以帮助浏览器返回到浏览器历史记录。
98. 解释 Selenium Grid 的工作原理
Selenium Grid 用于支持跨多个浏览器、平台、设备和机器的并行执行。它有助于减少执行时间,并通过在多台机器上分配执行来允许有效利用资源。它基于中心-节点架构。
99. 如何在 Selenium 中识别动态元素?
我们可以通过以下方法在 Selenium 中识别动态元素:
在定位器中使用正则表达式
WebDriver driver = new ChromeDriver(); // identify element with partial class match with * in css WebElement element = driver.findElement(By.cssSelector("input[name*='inputTxt']"));
在 xpath 定位器中使用正则表达式
WebDriver driver = new ChromeDriver(); // Identify element with contains in xpath WebElement element = = driver.findElement(By.xpath("//input[contains(@name,'inputTxt')]"))
100. 如何在 TestNG 中为测试用例设置优先级?
我们可以通过为具有 @Test 注解的测试方法设置优先级参数来在 TestNG 中为测试用例设置优先级。设置的数字越小,其优先级越高。每个测试方法的默认优先级为 0。
101. 如何从 Excel 读取数据?
我们可以使用名为Apache POI 的第三方库从 Excel 读取数据。它有助于在遍历工作簿内工作表中的行和列后访问各个单元格。
102. 如何在 Selenium 中执行右键单击?
我们可以借助 Actions 类的 contextClick() 方法对元素执行右键单击。
WebDriver driver = new ChromeDriver(); // Identify element WebElement element = driver.findElement (By.xpath("//div/div/a")); Actions actions = new Actions(driver); actions.moveToElement(element ).contextClick().build().perform();
103. 命名不同类型的框架
可用的不同类型的框架如下:
104. 命名 TestNG 中不同类型的监听器。
TestNG 中使用的不同类型的监听器如下:
- ITestListener
- IReporter
- IHookable
- IExecutionListener
- IConfigurationListener
- IAnnotationTransformer
- IConfigurationListener
- IInvokedMethodListener
105. 如何在 Selenium 中处理基于 Windows 的弹出窗口?
Selenium 仅用于测试基于 Web 的应用程序。要在 Selenium 中处理基于 Windows 的弹出窗口,我们必须将 Selenium 与 AutoIt 或 Robot 类集成。
106. Selenium 中的 ExpectedConditions 类是什么?
ExpectedConditions 类是 Selenium 中的一个实用程序类,它提供在移动到下一步之前应满足的特定条件。这些与显式等待一起使用,有助于在测试中实现同步。ExpectedConditions 类与 WebdriverWait 类一起使用,以等待特定时间内满足给定条件,之后再次开始与页面上的 Web 元素进行通信。
语法
WebDriverWait wt = new WebDriverWait(driver, 15); wt.until(ExpectedConditions.invisibilityOfElementLocated (By.id("id value")));
107. 如何在 Selenium 中执行跨浏览器测试?
使用 Selenium 执行跨浏览器测试 以验证软件是否与多个浏览器兼容。为此,针对各种浏览器执行相同的测试,以检查是否存在任何问题。在 Selenium 中,跨浏览器测试主要借助 TestNG 单元测试自动化框架实现。
108. 如何使用 Selenium Webdriver 测试应用程序的登录功能?
我们可以使用 Selenium Webdriver 识别应用程序的登录功能。首先,我们必须在浏览器中启动应用程序 URL。然后,我们必须使用 Selenium 中提供的任何定位器(例如 id、class、name、css、xpath 等)来识别用户名、密码和登录按钮。最后,我们必须使用 sendKeys() 方法输入用户名和密码,并使用 click() 方法单击登录按钮。
代码实现
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.concurrent.TimeUnit; public class Login { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 10 secs driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Opening the webpage driver.get("https://tutorialspoint.com/selenium/practice/login.php"); // identify element WebElement username = driver.findElement(By.id("email")); username.sendKeys("Selenium Java"); WebElement password = driver.findElement(By.id("password")); password.sendKeys("Automation"); // identify element WebElement loginBtn = driver.findElement (By.id("//*[@id='signInForm']/div[3]/input")); loginBtn.click(); // Closing browser driver.quit(); } }
109. Selenium 中 type 和 typeAndWait 命令有什么区别?
type 命令用于在文本框中输入键盘键值。它还有助于在组合框中选择选项。typeAndWait 命令用于在完成输入且网页开始加载时。它会等到页面加载完毕后再继续执行下一步。
110. 通过测试描述 Selenium 的用法
在开始 Selenium 测试之前,应满足以下先决条件:
- 下载并安装 Java 和 Maven
- 下载并安装任何 IDE,例如 IntelliJ
- 添加 Selenium 依赖项
代码实现
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 FirstTest { 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 driver.get("https://tutorialspoint.com/selenium/practice/login.php"); // identify element to enter email address and password WebElement email = driver.findElement(By.id("email")); email.sendKeys("Interview"); WebElement p = driver.findElement(By.id("password")); p.sendKeys("Automation"); // identify login element WebElement submitBtn = driver.findElement (By.id("//*[@id='signInForm']/div[3]/input")); submitBtn.click(); // Closing browser driver.close(); } }
111. 如何实现 Selenium Webdriver 监听器?
可以通过以下方法实现 Selenium Webdriver 监听器:
- WebDriverEventListener
- AbstractWebDriverEventListener
112. 如何在 Selenium Webdriver 中添加恢复方案?
我们可以通过合并 try-catch 块在 Selenium Webdriver 中添加恢复方案。这是处理代码中异常的一种方法。
语法
try { // Perform some tasks } catch(Exception e1) { // Code for the catch block }
113. 是否可以仅使用 build() 而不用 perform()?
是的,可以仅使用 build() 而不用 perform()。build() 方法构造并返回 Actions 类的对象,它是操作序列的集合。如果不使用 build() 方法,则在调用 perform() 时会立即执行操作。