我遇到了Selenium和Python的问题。我不知道为什么我的代码中包含的异常或断言都没有被采纳。这意味着遵循默认的异常/断言行为,而不是采取我已经定义的行为。
例如,在以下情况中,我希望当找不到某个元素时,应用程序会显示一条消息,然后关闭浏览器。我删除了该消息,因为Selenium从未显示过我的消息,因此,我已经保留了关闭浏览器的代码段,但每次都显示标准错误。 如何定义自定义异常或使用Assertion Raise处理它?</ p>
con = set_up.Connect()
driver = con.setUp()
def element_fail(self): **#This function is never called**
self.con.tearDown() **#I 've defined this piece of code in another #module but it is just self.driver.quit**
def test_login(self):
#there is login but there is not user validation functionality.
# So Basic login test cases without assert has been included
try:
WebDriverWait(self.driver, 10) \
.until(expected_conditions.visibility_of_all_elements_located((By.NAME, "userName")))
WebDriverWait(self.driver, 10) \
.until(expected_conditions.visibility_of_element_located((By.NAME, "password")))
**WebDriverWait(self.driver, 10) \
.until(expected_conditions.visibility_of_element_located((By.NAME, "nada")))**
WebDriverWait(self.driver, 10) \
.until(expected_conditions.visibility_of_element_located((By.NAME, "login")))
**except TimeoutError:
print("element not found")
element fail() # same result if I use self.con.tearDown() instead #this blocked of code is never called, since my browser is not closed**
响应:
Testing started at 3:25 PM ...
/home/osboxes/PycharmProjects/Automation/venv/bin/python /home/osboxes/Desktop/Emi/pycharm-community-2018.1.3/helpers/pycharm/_jb_unittest_runner.py --path /home/osboxes/PycharmProjects/Automation/unit_test/login.py
Launching unittests with arguments python -m unittest /home/osboxes/PycharmProjects/Automation/unit_test/login.py in /home/osboxes/PycharmProjects/Automation/unit_test
Ran 1 test in 11.036s
FAILED (errors=1)
Error
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/lib/python3.6/unittest/case.py", line 605, in run
testMethod()
File "/home/osboxes/PycharmProjects/Automation/unit_test/login.py", line 32, in test_login
**.until(expected_conditions.visibility_of_element_located((By.NAME, "nada")))**
File "/home/osboxes/PycharmProjects/Automation/venv/lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
**selenium.common.exceptions.TimeoutException: Message:
Process finished with exit code 1**
答案 0 :(得分:0)
我发现自己就是这个问题。当我在代码中间放置一个预期条件时,我刚刚发现了一些事件,但我没有做任何有关该错误的事情。所以,我已经用这种方式定义了一个函数:
import unittest
from selenium.common.exceptions import NoSuchElementException
from connect import set_up
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
class HomeLinks(unittest.TestCase):
def setUp(self):
con = set_up.Connect()
self.driver = con.setUp()
def test_home_link(self):
self.assertTrue(self.is_element_present(By.LINK_TEXT, "Home"), "Element is not found")
self.home = self.driver.find_element_by_link_text("Home")
self._event_test(self.home, By.LINK_TEXT, "Home")
def test_flight_link(self):
self.assertTrue(self.is_element_present(By.LINK_TEXT, "Flights"), "Element is not found")
self.home = self.driver.find_element_by_link_text("Flights")
self._event_test(self.home, By.LINK_TEXT, "Flights")
def test_destinations_link(self):
self.assertTrue(self.is_element_present(By.LINK_TEXT, "Destinations"), "Element is not found")
self.destinations = self.driver.find_element_by_link_text("Destinations")
self._event_test(self.home, By.LINK_TEXT, "Destinations")
def test_contact_link(self):
self.assertTrue(self.is_element_present(By.LINK_TEXT, "CONTACT"), "Element is not found")
self.contact = self.driver.find_element_by_link_text("CONTACT")
self._event_test(self.home, By.LINK_TEXT, "CONTACT")
def test_signOn_link(self):
self.assertTrue(self.is_element_present(By.LINK_TEXT, "SIGN-ON"), "Element is not found")
self.sign_on = self.driver.find_element_by_link_text("SIGN-ON")
self._event_test(self.home, By.LINK_TEXT, "SIGN-ON")
def test_register_link(self):
self.assertTrue(self.is_element_present(By.LINK_TEXT, "REGISTER"), "Element is not found")
self.register = self.driver.find_element_by_link_text("REGISTER")
self._event_test(self.home, By.LINK_TEXT, "REGISTER")
def test_support_link(self):
self.assertTrue(self.is_element_present(By.LINK_TEXT, "SUPPORT"), "Element is not found")
self.support = self.driver.find_element_by_link_text("SUPPORT")
self._event_test(self.home, By.LINK_TEXT, "SUPPORT")
def test_hotels_link(self):
self.assertTrue(self.is_element_present(By.LINK_TEXT, "Hotels"), "Element is not found")
self.hotels = self.driver.find_element_by_link_text("Hotels")
self._event_test(self.home, By.LINK_TEXT, "Hotels")
def test_carRentals_link(self):
self.assertTrue(self.is_element_present(By.LINK_TEXT, "Car Rentals"), "Element is not found")
self.car_rentals = self.driver.find_element_by_link_text("Car Rentals")
self._event_test(self.home, By.LINK_TEXT, "Car Rentals")
def test_cruises_link(self):
self.assertTrue(self.is_element_present(By.LINK_TEXT, "Cruises"), "Element is not found")
self.cruises = self.driver.find_element_by_link_text("Cruises")
self._event_test(self.home, By.LINK_TEXT, "Cruises")
def is_element_present(self, how, what):
'''
:param how: By Locator Type
:param what: Locator value
:return: Exception(if it is not found), otherwise, return True
'''
try:
self.driver.find_element(how, what)
except NoSuchElementException:
return False
return True
def _event_test(self, element, how, what):
WebDriverWait(self.driver, 5)\
.until(expected_conditions.element_to_be_clickable((how, what)))
element.click()
self.driver.back()