- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 Python 3.6.5 和以下库:
现在我需要截屏以防测试失败,所以我故意做了一个错误的陈述 self.driver.find_element_by_css_selector('test')
.
我正在使用 sys.exc_info()
。但是,当我使用以下命令执行以下代码时:py.test untitled.py
或 python3 -m unittest untitled.py
它不会捕获它。
代码:
import sys, time, unittest2
from selenium import webdriver
class FB360(unittest2.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def test_user_can_login(self):
self.driver.get('https://www.google.co.in')
self.driver.find_element_by_css_selector('test')
def tearDown(self):
print(sys.exc_info())
if sys.exc_info()[0]:
test_method_name = self._testMethodName
self.driver.save_screenshot(test_method_name + str(time.time()) + '.png')
self.driver.quit()
if __name__ == '__main__':
unittest2.main()
O/P:
(None, None, None)
E
====================================================================== ERROR: test_user_can_login (untitled.FB360)
Traceback (most recent call last): File "/Volumes/Harry/Projects/pythonScreenshots/untitled.py", line 11, in test_user_can_login self.driver.find_element_by_css_selector('test') File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 498, in find_element_by_css_selector return self.find_element(by=By.CSS_SELECTOR, value=css_selector) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 832, in find_element 'value': value})['value'] File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute self.error_handler.check_response(response) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"test"} (Session info: chrome=67.0.3396.99) (Driver info: chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.13.5 x86_64)
我的基础:
print(sys.exc_info())
打印:(无,无,无)。这意味着即使我收到以下异常,我的测试用例也已通过:selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"test"}
sys.exc_info())
返回 (None, None, None) 在 if
条件下执行不会完成所以我主要担心的是,为什么即使我收到 NoSuchElementException,sys.exc_info())
也会返回 (None, None, None)?
最佳答案
我用它来截取失败测试的屏幕截图
@pytest.fixture()
def browser(request):
options = Options()
options.add_argument("--headless") # Runs Chrome in headless mode.
_chrome = webdriver.Chrome(options=options)
failed_before = request.session.testsfailed
yield _chrome
if request.session.testsfailed != failed_before:
test_name = request.node.name
take_screenshot(_chrome, test_name)
_chrome.close()
def take_screenshot(browser, test_name):
screenshots_dir = "path_to/functional_tests/failure_screenshots"
screenshot_file_path = "{}/{}.png".format(screenshots_dir, test_name)
browser.save_screenshot(
screenshot_file_path
)
关于python - 如何捕获 Python Unittest 测试用例失败的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51446471/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!