1

On python 3.11, I'm trying to use a method from another class, like this:

folder name: anyfunctions

script name: 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")

Then, I'm trying to create a test using Selenium with the following code:

folder name: tests

script name: test_home.py

from seleniumbase import BaseCase
from anyfunctions import basicactions

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

That, I was expecting to run the login method, but the following error appears:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Am I forgetting to add something to call the class correctly? Thanks

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 2
    Calling the method isn't the problem. The class `AnyTestName` itself is not being defined. `basicactions` is a module, not a class. – chepner Jan 17 '23 at 20:50
  • Does this answer your question? [Resolving metaclass conflicts](https://stackoverflow.com/questions/11276037/resolving-metaclass-conflicts) – MattDMo Jan 17 '23 at 20:52

2 Answers2

1

Here's an example of the classic Page Object Model with BaseCase inheritance using SeleniumBase:

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")

(Runs with pytest)

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • Thanks Mintz. In your example, considering the class MyTests OUT of the same script page as the class LoginPage, how can we call the method LoginPage() ? – Daniel Maia Jan 17 '23 at 21:26
  • 1
    `LoginPage` is a class, not a method. `LoginPage()` is a call to the constructor to initialize it. That class has a method, `login_to_swag_labs()`, where you need to pass in `sb` (The seleniumbase instance from `self` in the test), as well as the other arg that the method takes: `username`. – Michael Mintz Jan 17 '23 at 21:37
0

You don't need to (nor can you) subclass basicactions in order to use it in the definition of the class.

You also need to create an instance of Firstfunction in order to call the login method.

class AnyTestName(BaseCase):
    def test_login(self):
        f = basicactions.Firstfunction()
        f.login()
chepner
  • 497,756
  • 71
  • 530
  • 681