学习 python+selenium+unittest 有一段时间了,这个遇到的问题实在是令人费解,希望有遇到过类似问题的大佬,可以指点迷津🙏 🙏

遇到的问题:我希望我在断言失败之后,自动截图,并且关闭弹窗,所以我把这个方法封装到了一个基类中。然后就出现问题了。当我在 test_case.py 文件中直接运行,就只会打开一个浏览器窗口,如果我在 test.runner.py 文件中运行就会出现两个浏览器窗口。代码如下:

test_case.py 文件
import unittest
import time

from Base.web_base_driver import WebBaseDriver
from Page.zn_brain import YlfManage
from Page.zn_console import UserConsole


class BaseClass(unittest.TestCase):
    driver = WebBaseDriver("Chrome")
    driver.implicitly_wai(5)
    driver.maximize_window()
    driver.quit_browser()
    screens_path = "../Screenshots/%d-%s失败.png"

    def error_screen(self, e_name):
        self.driver.driver.save_screenshot(self.screens_path % (int(time.time()), e_name))

    def check_assert(self, result_ele, expect, e_name):
        close_button = "s,[class='ivu-icon ivu-icon-ios-close-empty']"
        self.driver.sleep(1)
        try:
            get_result = self.driver.get_element(result_ele)
            self.assertEqual(get_result.text, expect, "%s失败" % e_name)
        except AssertionError:
            self.error_screen(e_name)
            self.driver.sleep(1)
            self.driver.click(close_button)
            raise AssertionError

class BrainTest(BaseClass):
    URL = "xxxxxxxxx"
    ALL_NAME = "香港"
    device_assert = "s,[class='nav-text ivu-tabs-nav']>div"

    @classmethod
    def setUpClass(cls):
        cls.driver.navigate(cls.URL)
        cls.zn_rm = YlfManage(cls.driver)
        cls.zn_rm.brain_login()

    @classmethod
    def tearDownClass(cls):
        cls.driver.sleep(5)
        cls.driver.quit_browser()

    def setUp(self):
        self.result_ele = 's,.ivu-table-row:nth-child(1)>td:nth-child(1)'
        self.driver.sleep(1)

    def test_01_c_dc(self):
        """xxxxxxxxx"""
        self.zn_rm.click_resource_mg()
        self.zn_rm.click_center_mg()
        self.zn_rm.create_dc(self.ALL_NAME)
        self.check_assert(self.result_ele, "%sIDC" % self.ALL_NAME, "xxxxxxxxx")

class ZnConsoleTest(BaseClass):
    CONSOLE_URL = "xxxxxxxxxxx"

    @classmethod
    def setUpClass(cls):
        cls.driver.navigate(cls.CONSOLE_URL)
        cls.zn_rm = UserConsole(cls.driver)
        cls.zn_rm.console_login()

    @classmethod
    def tearDownClass(cls):
        cls.driver.sleep(5)
        cls.driver.quit_browser()

    def setUp(self):
        self.result_ele = 's,.ivu-table-row:nth-child(1)>td:nth-child(1)'
        self.driver.sleep(1)

    def test_21_c_console_source(self):
        """xxxxxxxxx"""
        self.zn_rm.click_ylf_business()
        self.zn_rm.add_source_ip("1.1.1.1")
test_runner.py 文件
import unittest
import datetime

from Base import html_test_runner


class ZNTestRunner(object):
    CASE_PATH = "E:\\OneDrive\\zhian_antu\\Test_cases"

    def run(self):
        time_stamp = datetime.datetime.now()
        time = time_stamp.strftime('%Y.%m.%d_%H.%M.%S')
        discover = unittest.defaultTestLoader.discover(self.CASE_PATH, pattern="test_zn*.py")
        test_suite = unittest.TestSuite()
        test_suite.addTest(discover)
        print("本次执行测试用例数量:", test_suite.countTestCases())
        report_path = "E:\\OneDrive\\zhian_antu\\report\\zn_test_report_%s.html" % time
        report_file = open(report_path, mode="wb")
        test_runner = html_test_runner.HtmlTestRunner(stream=report_file,
                                                      title="自动化测试报告",
                                                      description="测试详情")
        test_runner.run(test_suite)


if __name__ == '__main__':
    aa = ZNTestRunner()
    aa.run()


↙↙↙阅读原文可查看相关链接,并与作者交流