如何通过 Selenium 和 Python unittest 断言工具提示文本
How to assert tooltip text through Selenium and Python unittest
我正在学习 Selenium。为什么这个简单的测试失败了?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains
import unittest
class ToolTipTest (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://jqueryui.com/tooltip/")
self.driver.implicitly_wait(30)
self.driver.maximize_window()
def test_tool_tip(self):
driver = self.driver
frame_elm = driver.find_element_by_class_name("demo-frame")
driver.switch_to.frame(frame_elm)
age_field = driver.find_element_by_id("age")
ActionChains(self.driver).move_to_element(age_field).perform()
tool_tip_elm = WebDriverWait(self.driver, 10).until(
expected_conditions.visibility_of_element_located(
(By.CLASS_NAME, "ui-tooltip-content")))
# verify tooltip message
self.assertEqual(
"We ask for your age only for statistical purposes.",
tool_tip_elm.text)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main(verbosity=2)
有时会给出
AssertionError: 'We ask for your age only for statistical purposes.' != "ThemeRoller: jQuery UI's theme builder app
lication"
- We ask for your age only for statistical purposes.
+ ThemeRoller: jQuery UI's theme builder application
有时:
AssertionError: 'We ask for your age only for statistical purposes.' != "That's what this widget is"
- We ask for your age only for statistical purposes.
+ That's what this widget is
所以错误并不总是相同的。有时会过去。它看起来 select 是一个随机弹出窗口。 Here's 要测试的页面。我正在使用 python 3.6 硒 3
编辑:我如何检查(在 Firefox 开发者工具中)工具提示的 html 代码以查看 class、id 等?当我 select 代码工具提示消失时,代码也...
在行前添加一些(其他动态)等待
tool_tip_elm = ...
因为您的代码一直等到元素可见,但之后弹出文本被更改。所以你的测试往往比网页变化更快。
工作版本(但有静态等待):
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import time
class ToolTipTest (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://jqueryui.com/tooltip/")
self.driver.implicitly_wait(30)
self.driver.maximize_window()
def test_tool_tip(self):
driver = self.driver
frame_elm = driver.find_element_by_class_name("demo-frame")
driver.switch_to.frame(frame_elm)
age_field = driver.find_element_by_id("age")
ActionChains(self.driver).move_to_element(age_field).perform()
time.sleep(3)
tool_tip_elm = WebDriverWait(self.driver, 10).until(
expected_conditions.visibility_of_element_located(
(By.CLASS_NAME, "ui-tooltip-content")))
# verify tooltip message
self.assertEqual(
"We ask for your age only for statistical purposes.",
tool_tip_elm.text)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main(verbosity=2)
你快到了。带有 工具提示文本 的元素不正确。要声明 工具提示文本 ,您可以使用以下优化解决方案:
代码块:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import unittest
class ToolTipTest (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://jqueryui.com/tooltip/")
self.driver.implicitly_wait(30)
self.driver.maximize_window()
def test_tool_tip(self):
driver = self.driver
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.demo-frame")))
age_field = driver.find_element_by_css_selector("input#age")
ActionChains(self.driver).move_to_element(age_field).perform()
tool_tip_elm = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ui-helper-hidden-accessible div:not([style])")))
self.assertEqual("We ask for your age only for statistical purposes.", tool_tip_elm.text)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main(verbosity=2)
控制台输出:
test_tool_tip (__main__.ToolTipTest) ... ok
----------------------------------------------------------------------
Ran 1 test in 17.618s
OK
我正在学习 Selenium。为什么这个简单的测试失败了?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains
import unittest
class ToolTipTest (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://jqueryui.com/tooltip/")
self.driver.implicitly_wait(30)
self.driver.maximize_window()
def test_tool_tip(self):
driver = self.driver
frame_elm = driver.find_element_by_class_name("demo-frame")
driver.switch_to.frame(frame_elm)
age_field = driver.find_element_by_id("age")
ActionChains(self.driver).move_to_element(age_field).perform()
tool_tip_elm = WebDriverWait(self.driver, 10).until(
expected_conditions.visibility_of_element_located(
(By.CLASS_NAME, "ui-tooltip-content")))
# verify tooltip message
self.assertEqual(
"We ask for your age only for statistical purposes.",
tool_tip_elm.text)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main(verbosity=2)
有时会给出
AssertionError: 'We ask for your age only for statistical purposes.' != "ThemeRoller: jQuery UI's theme builder app
lication"
- We ask for your age only for statistical purposes.
+ ThemeRoller: jQuery UI's theme builder application
有时:
AssertionError: 'We ask for your age only for statistical purposes.' != "That's what this widget is"
- We ask for your age only for statistical purposes.
+ That's what this widget is
所以错误并不总是相同的。有时会过去。它看起来 select 是一个随机弹出窗口。 Here's 要测试的页面。我正在使用 python 3.6 硒 3
编辑:我如何检查(在 Firefox 开发者工具中)工具提示的 html 代码以查看 class、id 等?当我 select 代码工具提示消失时,代码也...
在行前添加一些(其他动态)等待
tool_tip_elm = ...
因为您的代码一直等到元素可见,但之后弹出文本被更改。所以你的测试往往比网页变化更快。
工作版本(但有静态等待):
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import time
class ToolTipTest (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://jqueryui.com/tooltip/")
self.driver.implicitly_wait(30)
self.driver.maximize_window()
def test_tool_tip(self):
driver = self.driver
frame_elm = driver.find_element_by_class_name("demo-frame")
driver.switch_to.frame(frame_elm)
age_field = driver.find_element_by_id("age")
ActionChains(self.driver).move_to_element(age_field).perform()
time.sleep(3)
tool_tip_elm = WebDriverWait(self.driver, 10).until(
expected_conditions.visibility_of_element_located(
(By.CLASS_NAME, "ui-tooltip-content")))
# verify tooltip message
self.assertEqual(
"We ask for your age only for statistical purposes.",
tool_tip_elm.text)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main(verbosity=2)
你快到了。带有 工具提示文本 的元素不正确。要声明 工具提示文本 ,您可以使用以下优化解决方案:
代码块:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.action_chains import ActionChains import unittest class ToolTipTest (unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.get("http://jqueryui.com/tooltip/") self.driver.implicitly_wait(30) self.driver.maximize_window() def test_tool_tip(self): driver = self.driver WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.demo-frame"))) age_field = driver.find_element_by_css_selector("input#age") ActionChains(self.driver).move_to_element(age_field).perform() tool_tip_elm = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ui-helper-hidden-accessible div:not([style])"))) self.assertEqual("We ask for your age only for statistical purposes.", tool_tip_elm.text) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main(verbosity=2)
控制台输出:
test_tool_tip (__main__.ToolTipTest) ... ok ---------------------------------------------------------------------- Ran 1 test in 17.618s OK