如何调用从另一个类导入的方法没有元类错误?



在python 3.11上,我试图使用另一个类的方法,像这样:

文件夹名称:anyfunctions

脚本名称:basicactions.py

from seleniumbase import BaseCase

class Firstfunction(BaseCase):
def login(self):
self.open("https://randomsite/authentication/signin")
self.wait_for_element("#username")
self.click(".d-grid")

然后,我试图用以下代码创建一个使用Selenium的测试:

文件夹名称:<测试/em>

脚本名称:test_home.py

from seleniumbase import BaseCase
from anyfunctions import basicactions
class AnyTestName(BaseCase, basicactions):
def test_login(self):
basicactions.Firstfunction.login()

那,我期望运行登录方法,但出现以下错误:

TypeError:元类冲突:派生类的元类必须是其所有基类的元类的(非严格的)子类

我忘记添加一些东西来正确调用类吗?由于

下面是一个使用SeleniumBase的经典页面对象模型与BaseCase继承的例子:

from seleniumbase import BaseCase
class LoginPage:
def login_to_swag_labs(self, sb, username):
sb.open("https://www.saucedemo.com")
sb.type("#user-name", username)
sb.type("#password", "secret_sauce")
sb.click('input[type="submit"]')
class MyTests(BaseCase):
def test_swag_labs_login(self):
LoginPage().login_to_swag_labs(self, "standard_user")
self.assert_element("div.inventory_list")
self.assert_element('div:contains("Sauce Labs Backpack")')
self.js_click("a#logout_sidebar_link")
self.assert_element("div#login_button_container")

(running withpytest)

为了在类的定义中使用它,您不需要(也不能)子类basicactions

您还需要创建Firstfunction实例,以便调用login方法。

class AnyTestName(BaseCase):
def test_login(self):
f = basicactions.Firstfunction()
f.login()

最新更新