这是我的代码。我写了可以测试多个测试用例的脚本。当错误出现时,此脚本引发异常,但是我希望它保留错误计数及其描述,并在测试执行成功或失败后记录日志。更重要的是应以任何方式运行完整的测试脚本。
from selenium import webdriver
from generic_functions.FindElement import HandyWrappers
from generic_functions.takescreenshots import Screenshot
from generic_functions.error_handle import CatchExceptions
import os
import time
import unittest
class TestEnrollment(unittest.TestCase):
driverLocation =
"C:\\Users\\Sales\\Desktop\\Automation\\Trendz\\libs\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = driverLocation
driver = webdriver.Chrome(driverLocation)
driver.maximize_window()
driver.implicitly_wait(10)
def test_login(self):
try:
baseURL = "https://portal.ABCD.com"
driver = self.driver
driver.get(baseURL)
hw = HandyWrappers(driver)
username = hw.getElement(".//form[@id='login-form']/fieldset/section[1]/label[2]/input[@name='email']",
locatorType="xpath")
username.send_keys("ABCD@live.com")
time.sleep(2)
password = hw.getElement(".//form[@id='login-form']/fieldset/section[2]/label[2]/input[@name='password']",
locatorType="xpath")
password.send_keys("ABCD09")
signIn = hw.getElement(".//form[@id='login-form']/footer/button[contains(text(),'Sign in')]",
locatorType="xpath")
signIn.click()
self.assertTrue(driver.find_element_by_xpath(".//header[@id='header']/section/div/div[@id='logout']/span/a12"), "Sign Out")
except Exception as err:
raise err
def test_menu_enr(self):
driver = self.driver
hw = HandyWrappers(driver)
find_enrollment = hw.getElement(".//aside[@id='left-panel']/nav/ul[14]/li/a/span", locatorType="xpath")
find_enrollment.click()
if __name__ == '__main__':
unittest.main()
答案 0 :(得分:0)
不要举起,请使用pass
或continue
def test_login():
try:
...
except Exception:
# log the exception here
pass # or you could use 'continue'
答案 1 :(得分:0)
收集某些列表中的所有错误,然后在测试结束时声明错误列表
sqldf(paste0("select ",
names(sqldf("select * from file where 1 = 0")[2]),
" from file"))
RN2
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
from selenium import webdriver
from generic_functions.FindElement import HandyWrappers
from generic_functions.takescreenshots import Screenshot
from generic_functions.error_handle import CatchExceptions
import os
import time
import unittest
class TestEnrollment(unittest.TestCase):
driverLocation = "C:\\Users\\Sales\\Desktop\\Automation\\Trendz\\libs\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = driverLocation
driver = webdriver.Chrome(driverLocation)
driver.maximize_window()
driver.implicitly_wait(10)
def setUp(self):
self.errors = []
def tearDown(self):
self.assertEqual([], self.errors)
def test_login(self):
try:
baseURL = "https://portal.ABCD.com"
driver = self.driver
driver.get(baseURL)
hw = HandyWrappers(driver)
username = hw.getElement(".//form[@id='login-form']/fieldset/section[1]/label[2]/input[@name='email']",
locatorType="xpath")
username.send_keys("ABCD@live.com")
time.sleep(2)
password = hw.getElement(".//form[@id='login-form']/fieldset/section[2]/label[2]/input[@name='password']",
locatorType="xpath")
password.send_keys("ABCD09")
signIn = hw.getElement(".//form[@id='login-form']/footer/button[contains(text(),'Sign in')]",
locatorType="xpath")
signIn.click()
self.assertTrue(driver.find_element_by_xpath(
".//header[@id='header']/section/div/div[@id='logout']/span/a12"), "Sign Out")
except Exception as err:
self.errors.append(err)
def test_menu_enr(self):
driver = self.driver
hw = HandyWrappers(driver)
find_enrollment = hw.getElement(
".//aside[@id='left-panel']/nav/ul[14]/li/a/span", locatorType="xpath")
find_enrollment.click()
if __name__ == '__main__':
unittest.main()
方法将在最后运行,并显示所有错误消息(如果存在)
据我所知,这不是理想的解决方案。这种黑客修复程序
答案 2 :(得分:0)
如果您将Selenium Webdriver与Python unittest一起使用(看起来像您一样),则SeleniumBase框架内置了漂亮的delayed_assert_text
和delayed_assert_element
方法,用于在以下位置处理测试失败测试结束。您可以使用pip安装seleniumbase,或从github克隆存储库。
以下代码摘自this example。
from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_delayed_asserts(self):
self.open('http://xkcd.com/993/')
self.wait_for_element('#comic')
self.delayed_assert_element('img[alt="Brand Identity"]')
self.delayed_assert_element('img[alt="Rocket Ship"]') # Will Fail
self.delayed_assert_element('#comicmap')
self.delayed_assert_text('Fake Item', '#middleContainer') # Will Fail
self.delayed_assert_text('Random', '#middleContainer')
self.delayed_assert_element('a[name="Super Fake !!!"]') # Will Fail
self.process_delayed_asserts()
在脚本中,有多个断言将失败,但仅在调用self.process_delayed_asserts()
之后。这是运行后的输出:
pytest delayed_assert_test.py
=============================== test session starts ===============================
platform darwin -- Python 3.6.5, pytest-3.7.3, py-1.5.4, pluggy-0.7.1
delayed_assert_test.py F
==================================== FAILURES =====================================
________________________ MyTestClass.test_delayed_asserts _________________________
E Exception:
E *** DELAYED ASSERTION FAILURES FOR: examples.delayed_assert_test.MyTestClass.test_delayed_asserts
E CHECK #2: (https://xkcd.com/993/)
E Element {img[alt="Rocket Ship"]} was not visible after 1 second!
E CHECK #4: (https://xkcd.com/993/)
E Expected text {Fake Item} for {#middleContainer} was not visible after 1 second!
E CHECK #6: (https://xkcd.com/993/)
E Element {a[name="Super Fake !!!"]} was not visible after 1 second!
Exception
============================ 1 failed in 7.53 seconds =============================