【问题标题】:How to continue a class with another class and at the same time with same variable from first class如何继续一个班级与另一个班级,同时与第一节课的相同变量
【发布时间】:2018-04-10 10:34:55
【问题描述】:

我想使用函数和类创建一个 selenium 测试。第一个文件和第一个类是:

import unittest
import time
from selenium import webdriver

class DriverAndLogin(unittest.TestCase):

    def setUp(self):
       self.driver = webdriver.Chrome()
       self.driver.maximize_window()

    def test_login(self):
       driver = self.driver
       driver.get('https://qa.knolyx.com/')
       self.assertIn("Login to your account | Knolyx", driver.title)
       time.sleep(1)
       search_box = driver.find_element_by_name('email')
       search_box.send_keys('username@test.com')
       password = driver.find_element_by_name('password')
       password.send_keys("98765")
       button_login = driver.find_element_by_css_selector('#app > div > div > form > div.Login_Actions > button')
       button_login.click()
       time.sleep(1)
       self.assertIn("Dashboard | Knolyx", driver.title)


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

我想创建另一个文件和类来继续第一个文件和类。因此,上面的代码访问 Knolyx 站点并登录,我想从登录转发继续我的第二个文件。 我试过了:

  from SeleniumOOP import DriverAndLogin
  import unittest
  import time


  class ChangeRole(unittest.TestCase, DriverAndLogin):

但变量“self.driver”或“driver”在第二个文件中不可见。

附注: 我试过了:

 from SeleniumOOP.DriverAndLogin import DriverAndLogin
 import unittest
 from selenium import webdriver


class ChangeRole(unittest.TestCase, metaclass=DriverAndLogin):

    def setUp(self):
         self.driver = webdriver.Chrome()
         self.driver.maximize_window()

    def see_the_role(self):
         driver = self.driver
         therole = driver.find_element_by_class_name('Person_Title').text
         print(therole)


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

但我收到错误:“TypeError:元类冲突:派生类的元类必须是其所有基类的元类的(非严格)子类”

【问题讨论】:

  • "我想创建另一个文件和类以继续第一个文件和类" => 我不确定你真正是什么意思,但有一件事是当然:“继续一个文件和课程”没有意义(这是一个语法正确的句子,但与“我希望我的 rhodoid 种植拖拉机并压扁大海”一样明智)。
  • 另外我不确定你是否真的理解继承是什么以及如何使用它(更不用说元类了)。
  • @brunodesthuilliers 我的第一堂课在登录后停止了,我想创建一个类来调用第一堂课并继续这个!因此,在第二类(和文件)中,我想运行第一类 + 另一个代码,该代码从它所在的位置继续第一类。现在你明白了吗?
  • “我的第一堂课在登录后停止”和“从它停留的地方继续第一堂课的代码”没有意义。一个类不会“停止”,也不会“停留”在任何地方。我了解您想要做什么-首先测试您可以登录,然后测试您是否已登录可以“看到角色”,但这与“课程停止”或“课程继续”无关第一堂课留在哪里”。

标签: python function class selenium oop


【解决方案1】:

在尝试访问 self.driver 之前,您似乎没有调用 ChangeRole.setUp()

class ChangeRole(DriverAndLogin):
    def printDriver(self):
        print(self.driver)

if __name__ == "__main__":
    ch = ChangeRole()
    ch.setUp()
    ch.printDriver()

这应该按预期工作。

如果您之前不想调用setUp,您应该考虑将其添加到您的__init__ 函数中:

class ChangeRole(DriverAndLogin):

    def __init__(self):
        self.setUp()

    def printDriver(self):
        print(self.driver)

这样,每次创建类的实例时都会调用setUp()。根据您要对 DriverAndLogin 类执行的操作,将对 setUp() 的调用添加到 DriverAndLogin.__init__ 可能也是一个好主意。

【讨论】:

  • 我得到:“TypeError:无法为基础 TestCase、DriverAndLogin 创建一致的方法解析顺序 (MRO)”
  • @ConstantinValentin 这是因为 DriverAndLogin 已经继承自 unittest.TestCase,所以如果你从 DriverAndLogin 和 TestCase 继承 ChangeRole 会有问题。我编辑了我的答案,现在它应该可以工作了
【解决方案2】:

好的,您真正想做的(真正的问题,而不是您认为的解决方案)是 1. 测试您可以登录和 2. 测试如果您已登录,您可以“查看角色”。

这里的解决方案不是“让一个类在第一个停止的地方继续”(这根本没有意义),而是在你的 TestCase 类中有两个测试方法并排除登录部分:

class MyTestCase(unittest.TestCase):

    def setUp(self):
       self.driver = webdriver.Chrome()
       self.driver.maximize_window()

    # this is a normal method and won't be called
    # directly by the test runner
    def login(self):
       driver = self.driver
       driver.get('https://qa.knolyx.com/')
       self.assertIn("Login to your account | Knolyx", driver.title)
       time.sleep(1)
       search_box = driver.find_element_by_name('email')
       search_box.send_keys('username@test.com')
       password = driver.find_element_by_name('password')
       password.send_keys("98765")
       button_login = driver.find_element_by_css_selector('#app > div > div > form > div.Login_Actions > button')
       button_login.click()
       time.sleep(1)


    def test_login(self):
       self.login()
       self.assertIn("Dashboard | Knolyx", self.driver.title)


    def test_can_see_role(self):
       self.login()
       driver = self.driver
       therole = driver.find_element_by_class_name('Person_Title').text
       print(therole)

一个 TestCase 可以有很多你想要的测试,每个测试都将由 testrunner 单独执行,所以从技术上讲,你不需要两个不同的类,也不需要两个不同的文件。您还可以在同一个文件中包含任意数量的测试用例。

现在,如果您真的想将其拆分为不同的测试用例(并最终拆分为不同的文件),请定义一个包含公共部分的 mixin 类(此处为 setUplogin 方法并使用多重继承:

class DriverAndLoginMixin(object):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()

    # this is a normal method and won't be called
    # directly by the test runner
    def login(self):
        driver = self.driver
        driver.get('https://qa.knolyx.com/')
        self.assertIn("Login to your account | Knolyx", driver.title)
        time.sleep(1)
        search_box = driver.find_element_by_name('email')
        search_box.send_keys('username@test.com')
        password = driver.find_element_by_name('password')
        password.send_keys("98765")
        button_login = driver.find_element_by_css_selector('#app > div > div > form > div.Login_Actions > button')
        button_login.click()
        time.sleep(1)



class LoginTest(DriverAndLoginMixin, unittest.TestCase):
    def test_login(self):
       self.login()
       self.assertIn("Dashboard | Knolyx", self.driver.title)


class AnotherTest(DriverAndLoginMixin, unittest.TestCase):
    def test_can_see_role(self):
       self.login()
       driver = self.driver
       therole = driver.find_element_by_class_name('Person_Title').text
       print(therole)

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多