python - 键盘选项卡导航的自动化测试

标签 python selenium selenium-webdriver webdriver uiaccessibility

我的网页支持键盘导航,按“TAB”键可以按特定顺序在网页项目之间切换焦点。在获得焦点的项目上按 Enter 键会打开弹出窗口/选择该项目。

我的自动化测试用例是: 1. 在整个网站上按 Tab 键并验证正确的项目处于焦点位置。 2. 在焦点项目上按 Enter 键并验证是否显示弹出窗口。 3. 在焦点项目上按 Enter 键并验证是否已选择它。

from selenium.webdriver.common.keys import Keys
# Qs: I want to test that the first time I press TAB key, the logo is in focus.          
# Currently. I am unable to achieve that without finding that element.
# How do I include the first element in the test?
first = self.driver.find_element_by_id("logo")
# The following code tabs to the second item on the page and brings it in focus. 
# Qs: How do I test that this item is in focus?
first.send_keys(Keys.TAB)
# How do I tab to the third item on the page without saving the second item 
# in a variable?
# Do I need to find the element in focus in order to select it by sending the 
# RETURN key?

感谢您的帮助

最佳答案

您可以使用execute_script()测试焦点,并测试document.activeElement

这样的事情将返回当前事件的网络元素:

second = self.driver.execute_script("return document.activeElement")

关于python - 键盘选项卡导航的自动化测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24048724/

相关文章:

Python 队列似乎正在消亡

python - Pymongo 中的客户端连接

java - 如何使用 Selenium 获取元素中文本的颜色?

java - 获取失败断言的描述

selenium - Selenium Remote Control 和 Selenium Server 有什么区别?

python - 类型错误 : __init__() got an unexpected keyword argument 'current_app' Django

python - 交互式数据可视化 - Python

android - 如何在 appium 中启用 chromedriver_autodownload 功能?

java - 如何在使用 Selenium WebDriver 触发的每个事件上自动测试谷歌分析

python - 如何循环浏览并点击相关项目