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

selenium webdriver python—如果自定义编写的python代码中存在错误,则使selenium测试用例失败,python,selenium,exception-handling,webdriver,selenium-webdriver,Python,Selenium,Exception Handling,Webdriver,Selenium Webdriver,我有一个使用SeleniumIDE导出到python的SeleniumWebDriver测试用例。然后想添加一些附加功能,以便在selenium测试用例之间插入自定义代码 现在的问题是——如果我犯了一个错误或者自定义编写的python代码返回了错误——selenium测试用例仍然会继续执行 然而,如果出现这样的错误,我希望selenium测试用例的执行停止。通过使用shutil模块进行简单的google搜索,然后是日志移动代码,复制了下面的场景 from selenium import webd

我有一个使用SeleniumIDE导出到python的SeleniumWebDriver测试用例。然后想添加一些附加功能,以便在selenium测试用例之间插入自定义代码

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

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

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()
在这段代码中-如果由于错误路径等原因,从日志文件到日志文件的移动失败,将执行catch块,我希望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)

工作起来很有魅力-你是怎么知道这些的?如果我应该在文档中或通过谷歌搜索找到这一点-即使在知道存在一个名为raise或sys.exit的东西之前,还有什么关键字可以找到这一点?@user83969真棒,很高兴听到它:)@user83969是的,文档肯定会有一些东西。是将为您提供详细信息的特定于
的提升部分。这实际上是一个特定于Python的东西——这在Selenium文档中是不会出现的(所以不用担心看不到它:)。如果你用谷歌搜索它,我会寻找类似“用Python处理异常”的东西来了解正在发生的事情,并查看在给定情况下有哪些选项。@RocketDonkwy使用sys.exit(1)并使用来自另一个文件(如proc=subprocess.Popen)的subprocess调用来执行整个文件(“sel_except.py”,shell=True).communicate()但在打印时,程序会显示null。它应该包含1还是false?@user83969尝试检查
theproc.returncode
以获取流程的退出代码(您应该在这里找到
1
值)。要了解更多信息,请查看各种好的阅读材料:)希望对您有所帮助。