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

100

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, [])

100

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

执行时,我收到一个错误:

文件"C:\USERS...\IMPORT_TEST_set.py",第17行,位于 TEST_00_02_01_ENTER Self.driver.IMPLICLY_WAIT(selfI._Wait)AttributeError:‘ImportTestSet’对象没有属性‘DRIVER’

我不明白为什么司机不能通过进口考试. 代码中有什么错误?

推荐答案

To solve the problem about the driver attribute try with the following modifications.
In the file 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

    ...

在文件main_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相关问答推荐

需要使用regex匹配字符串的帮助,直到最后一次出现开闭括号,开闭括号中的值是可选的

如何创建一个polars gramme,给出列表中的列名,

如何从包含SPAN文本的标记中获取链接

根据第一个字典的值序列对第二个字典进行排序

在 sum() 中将字符串转换为 int (或 float)

Django 模型类方法使用错误的 `self`

如何统计一个值连续出现的次数?

如何在 histplot 中标记核密度估计

我应该如何调整我的变量,以便如果有任何单词符合其中的条件,程序会将其附加到新列表中?

使用 Python 截断并重新编号对应于特定 ID/组的列

pytorch 中 mps 设备的 manual_seed

通过最接近的匹配合并两个不同长度的列上的两个数据框

Python - 使用 OpenCV 将字节图像转换为 NumPy 数组

Python:遍历子列表

__new__ 方法给出错误 object.__new__() 只接受一个参数(要实例化的类型)

tensorflow 中 numpy.newaxis 的替代方案是什么?

如何获得 BeautifulSoup 标签的所有直接子代?

将行附加到 DataFrame 的最快和最有效的方法是什么?

python判断一个方法是否被调用而不模拟它

将列表列表转换为Python中的字典字典