python-3.x - 如何将 WebDriver 转移到导入的测试?

标签 python-3.x selenium-webdriver pycharm python-unittest

主模块测试和导入的模块测试必须在同一浏览器窗口中执行。但出了点问题:

ma​​in_tests.py:

import unittest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import import_test_set

class MainTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.url = "https://www.google.com/"
        s = Service("../chromedriver/chromedriver")
        cls.driver = webdriver.Chrome(service=s)
        cls.driver.maximize_window()

    def test_00_01_open_site(self):
        print('test_00_01_open_site')

        self.driver.get(self.url)
        self.assertIn("Google", self.driver.title)

    def test_00_02_imported_tests(self):
        print('test_00_02_imported_tests')

        imported_tests = import_test_set.ImportTestSet()
        imported_tests.set_driver(self.driver)
        suite = unittest.TestLoader().loadTestsFromModule(import_test_set)

        result = unittest.TextTestRunner(verbosity=2).run(suite)
        self.assertEqual(result.failures, [])

import_test_set.py:

import unittest
from selenium.webdriver.common.by import By

class ImportTestSet(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.i_wait = 5

    def set_driver(self, driver):
        self.driver = driver

    def test_00_02_01_enter(self):
        print('test_00_02_01_enter')

        self.driver.implicitly_wait(self.i_wait)
        a_enter = self.driver.find_element(By.LINK_TEXT, 'Log in')
        a_enter.click()
        page_title = self.driver.title
        self.assertEqual(page_title, 'Google')
   
    def runTest(self):
        pass

执行在模块 import_test_set.py 中实现的测试方法 test_00_02_01_enter 会产生以下错误:

File "C:\Users...\import_test_set.py", line 17, in test_00_02_01_enter self.driver.implicitly_wait(self.i_wait) AttributeError: 'ImportTestSet' object has no attribute 'driver'

我不明白为什么驱动程序没有通过导入测试。 代码有什么问题?

最佳答案

要解决有关 driver 属性的问题,请尝试进行以下修改。
在文件import_test_set.py中:

  • 添加类属性driver
  • 方法set_driver()可以被删除(停止使用它就足够了)
class ImportTestSet(unittest.TestCase):

    # ----> add the following attribute driver
    driver = None

    @classmethod
    def setUpClass(cls):
        cls.i_wait = 5

    # ----> remove or stop to use the method set_driver()
    #def set_driver(self, driver):
    #    self.driver = driver

    ...

在文件ma​​in_tests.py中修改方法test_00_02_imported_tests()如下:

def test_00_02_imported_tests(self):
    print('test_00_02_imported_tests')

    # ---> remove the following instructions
    #imported_tests = import_test_set.ImportTestSet()
    #imported_tests.set_driver(self.driver)

    # ---> add the following instruction to set the attribute driver of ImportTestSet
    import_test_set.ImportTestSet.driver = self.driver

    suite = unittest.TestLoader().loadTestsFromModule(import_test_set)
    result = unittest.TextTestRunner(verbosity=2).run(suite)
    self.assertEqual(result.failures, [])

关于python-3.x - 如何将 WebDriver 转移到导入的测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76135464/

相关文章:

python - 如何在 PyCharm + python2.7 中指定通用返回类型

python - 分组时应用自定义函数返回 NaN

python-3.x - 使用python3检查网站是否存在

java - POPUP 窗口关闭后面临的问题,父窗口元素无法单击

java - Selenium 无法找到该元素

python - 压缩标准库时如何运行pycharm单元测试?

python - 在 Python 和 PyCharm 2020.2 中重新加载模块时发出警告

python-3.x - MatplotlibDeprecationWarning 与 Pyinstaller .exe

python - PyQt4 到 PyQt5 -> mainFrame() 已弃用,需要修复才能加载网页

testing - 如何处理 HTML 中的动态 ID