In python (3) I want to create a derived class dynamically from several base classes.
Concrete example: In selenium
, in order to run GUI based tests, you can initiate a driver from e.g Firefox or Chrome in the following way:
driver = webdriver.Firefox()
driver = webdriver.Chrome()
Now I want to create a derived class to which additional functionalities are added. Something like
class MyDriver(webdriver.Firefox):
def find_button_and_click_on_it_no_matter_what(self, params):
...
But the base class can be either the firefox
driver or the chrome driver
. I found something related here, but it does not seem to work:
class MyDriver(object):
def __new__(cls, base_type, *args, **kwargs):
return super(HBPDriver, cls).__new__(base_type, *args, **kwargs)
def __init__(self):
pass
Calling this command
driver = mydriver.MyDriver(webdriver.Firefox())
gives an error
TypeError: object.__new__(X): X is not a type object (WebDriver)
How to do it right? And how to call __init__
on the derived class...?
I hope it is clear what I want to achieve...