I'm learning Selenium. Why this simple test is failing?
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)
Sometimes it gives
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
Sometimes:
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
So the error isn't always the same. Sometimes it pass. It looks that it select a random popup. Here's the page to test. I'm using python 3.6 selenium 3
Edit: also how can I check (in Firefox Developer Tools) the html code of the tool tips to look class, id etc? When I select the code the tooltip desappear and the code too...
You were almost there. The element with tooltip text was not the correct one. To assert the tooltip text you can use the following optimized solution:
Code Block:
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)
Console Output:
test_tool_tip (__main__.ToolTipTest) ... ok
----------------------------------------------------------------------
Ran 1 test in 17.618s
OK