How to create base class in selenium using python and use it for all test classes?
<p>I am new to python, not able to figure out how to create a base class which I can inherit in my test classes which contains only test method. </p>

<p>This is my base class(TestBase.py)</p>

<pre><code>import unittest
from selenium import webdriver
import time

class TestBase(unittest.TestCase):  
    browser='firefox'

    def setUp(self):
        TestBase().browserFactory(self.browser)

    def browserFactory(self, browser):        
        if browser=='firefox':
            driver=webdriver.Firefox()
        elif browser=='chrome':
            driver=webdriver.Chrome()
        else: 
            driver=webdriver.Ie()
        driver.get("http://google.com")
        time.sleep(2)
        print('Opening ', browser, ' browser')
        return driver 

    def tearDown(self):
        print('Closing '+ self.browser+ ' browser')
        self.driver.quit()
</code></pre>

<p>This is my TestClass(TestClass.py)</p>

<pre><code>import unittest
from base.TestBase import TestBase  

class TestClass(TestBase):      
    def test_method1(self):   
        print("Google Title: ", self.driver.title)

    def test_method2(self):
        print("Current URL: ", self.driver.current_url)

if __name__ == '__main__':
    unittest.main()
</code></pre>

<p>TestClass.py extends TestBase.py. Now when I execute my TestClass.py class, I get below exception.</p>

<pre><code>Opening  firefox  browser
Closing firefox browser
EEOpening  firefox  browser
EClosing firefox browser
E
======================================================================
ERROR: test_method1 (__main__.TestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\project\python_programming\SeleniumPractice\act\baseclass\TestClass.py", line 8, in test_method1
    print("Google Title: ", self.driver.title)
AttributeError: 'TestClass' object has no attribute 'driver'

======================================================================
ERROR: test_method1 (__main__.TestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\project\python_programming\SeleniumPractice\act\baseclass\TestBase.py", line 25, in tearDown
    self.driver.quit()
AttributeError: 'TestClass' object has no attribute 'driver'

======================================================================
ERROR: test_method2 (__main__.TestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\project\python_programming\SeleniumPractice\act\baseclass\TestClass.py", line 11, in test_method2
    print("Current URL: ", self.driver.current_url)
AttributeError: 'TestClass' object has no attribute 'driver'

======================================================================
ERROR: test_method2 (__main__.TestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\project\python_programming\SeleniumPractice\act\baseclass\TestBase.py", line 25, in tearDown
    self.driver.quit()
AttributeError: 'TestClass' object has no attribute 'driver'

----------------------------------------------------------------------
Ran 2 tests in 22.081s

FAILED (errors=4)
</code></pre>

<p>I am not able to get driver instance from base class to my test class. My requirement is I want to run setUp() and tearDown() method of my base class before each method of my test class. So output would be something like this </p>

<pre><code>Opening  firefox  browser
Google Title: Google
Closing firefox browser

Opening  firefox  browser
Current URL: https://google.com
Closing firefox browser
</code></pre>

<p>How to achieve the same in selenium using python code? </p>