Python Selenium - how to access methods from different modules using the same webdriver instance
<p>I am getting very confused and was hoping someone here could help me out.</p>

<p>So here is my scenario: I have one module containing the Base class where the webdriver instance is created and this will be inherited by all tests; a separate module containing a class that will be inherited by all tests for a specific page (let's call that page engagements); and a separate module for opening a different webpage (let's call it reports). I created another module with tests that will access the engagements page and the reports page.</p>

<p>When I try to access the methods under the reports module, it always fails. The reason is when I tried to access the methods, it was not using the webdriver instance that was created by the autouse fixture (which is in the Base class).</p>

<p>I got it working by multiple inheritance here:</p>

<p>This is base.py</p>

<pre><code>class Base:
    @pytest.fixture(autouse=True):
    def _init_browser(self):
        self.browser = webdriver.PhantomJS()
</code></pre>

<p>This is base_engagements.py</p>

<pre><code>class BaseEngagements(Base):
    @pytest.fixture(autouse=True)
    def login_engagements(self):
        print("login for engagements goes here")
</code></pre>

<p>This is engagement_reports.py</p>

<pre><code>class CheckReportsPage(Base):
    def open_reports_for_engagement(self, voice_name):
        print("opening reports")
        self.browser.find_element_by_id("report_name")
</code></pre>

<p>And this is the test under test_voice_engagement.py</p>

<pre><code>class TestVoice(BaseEngagements, CheckReportsPage):
    def test_open_reports_for_voice(self, voice_name):
        self.open_reports_for_engagement(voice_name)
</code></pre>

<p>However, I would like to avoid multiple inheritance in the test module, for the main reason that more tests will have to use methods from other modules which are for specific webpages.</p>

<p>Is there a way to avoid multiple inheritance in this scenario?</p>

<p>Many thanks in advance.</p>

<p>I was able to use the methods from CheckReportsPage by adding a constructor to it and passing a browser instance when calling it. It basically looked like this:</p>

<pre><code>class CheckReportsPage(Base):
    def __init__(self, browser):
        self.browser = browser
</code></pre>

<p>So in TestVoice I did this:</p>

<pre><code>def test_open_reports_for_voice(self, voice_name):
    self.reports = CheckReportsPage(self.wd)
    self.reports.open_reports_for_engagement(voice_name)
</code></pre>

<p>This works for me, but there might be a better way, haven't found it yet :)</p>

python selenium inheritance selenium-webdriver py.test