gpt4 book ai didi

python - selenium webdriver python - 如果自定义编写的 python 代码中存在错误,则使 selenium 测试用例失败

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:46 25 4
gpt4 key购买 nike

我有一个使用 selenium IDE 导出到 python 的 selenium webdriver 测试用例。然后想要添加一些额外的功能,因此在 selenium 测试用例之间插入了自定义代码。

现在的问题是 - 如果我犯了一个错误或者自定义编写的 python 代码返回错误 - selenium 测试用例仍然继续执行

而如果出现此类错误,我希望停止执行 selenium 测试用例。已经通过简单的谷歌搜索复制了下面的场景,然后是使用 shutil 模块的日志移动代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
import os, shutil

class SeleniumException(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.co.in/"
self.verificationErrors = []

self.attachment = 1
self.file_source_location = "F:\\python_test\\logfiles\\"
self.file_move_location = "F:\\logfiles_back\\"

def test_selenium_exception(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("gbqfq").clear()
driver.find_element_by_id("gbqfq").send_keys("Check this out")
if self.attachment:
try:
for contents in os.listdir(self.file_source_location):
src_file = os.path.join(self.file_source_location, contents)
dst_file = os.path.join(self.file_move_location, contents)
shutil.move(src_file, dst_file)
print'files_moved_success'
driver.find_element_by_link_text("check out - definition of check out by the Free Online Dictionary ...").click()
except Exception as e:
print e

def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True

def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
unittest.main()

在这段代码中——如果从 logfiles 到 logfiles_back 的移动由于错误的路径等原因而失败......等等,catch block 被执行并且我希望 selenium 测试用例退出报告错误

现在发生的事情是报告错误并完成测试用例的执行

如何做到这一点?

最佳答案

如果你想引发实际触发的异常,你可以自己调用raise,这将引发最后一个事件异常:

try:
for contents in os.listdir(self.file_source_location):
src_file = os.path.join(self.file_source_location, contents)
dst_file = os.path.join(self.file_move_location, contents)
shutil.move(src_file, dst_file)
print'files_moved_success'
driver.find_element_by_link_text("check out - definition of check out by the Free Online Dictionary ...").click()
except Exception as e:
print e
raise # Raise the exception that brought you here

如果你不想追溯而只是想退出,你也可以在 print e 之后调用 sys.exit(1) (或者任何你想使用的错误代码) :

import sys
# ...
try:
for contents in os.listdir(self.file_source_location):
src_file = os.path.join(self.file_source_location, contents)
dst_file = os.path.join(self.file_move_location, contents)
shutil.move(src_file, dst_file)
print'files_moved_success'
driver.find_element_by_link_text("check out - definition of check out by the Free Online Dictionary ...").click()
except Exception as e:
print e
sys.exit(1)

关于python - selenium webdriver python - 如果自定义编写的 python 代码中存在错误,则使 selenium 测试用例失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13814604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com