Import Selenium IDE script as unittest.TestCase and Modify Dynamically
<p>I am writing a program in python that will generate more robust data and reports from Selenium tests. I want to be able to export a test case from Selenium IDE from Firefox, and without modifying that specific exported python file, inject it into my script and allow my script to make modifications such as changing the webdriver (part of the implementation is for a Nagios server, which will use PhantomJS as the driver) and pulling request information and screenshots for reporting (Btw, I know I can change the driver in the IDE template, but it's just an example).</p>

<p>Lets say I have a file exported from the IDE <i>path/to/mytests/search.py</i>:</p>

<pre><code>import...

class Search(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url = 'http://www.example.com'

    def test_search(self):
        driver = self.driver
        driver.get(self.base_url)
        elem = self.driver.find_element_by_name('q')
        elem.send_keys('nagios')
        elem.send_keys(Keys.RETURN)
        assert 'No results found.' not in self.driver.page_source
        driver.get(self.base_url + '/status')
        assert 'OK' in self.driver.page_source

    def tearDown(self):
        self.driver.quit()
</code></pre>

<p>I want my script to be able to do this:</p>

<pre><code>python -m my_wrapper mytests.search --url 'http://www.google.com'
</code></pre>

<p>Which will somehow import <i>search.py</i>, create a new testcase from it, and allow me to override the setUp/tearDown methods (changing the driver to PhantomJS, modifying the base URL from the sys.argv), and allow for catching errors and writing information related to where a failed test might have stopped (since there are two different URL calls, a screenshot of that page will be exported for wherever the driver is currently at).</p>

<p>I'm thinking something like <i>my_wrapper.__main__</i>:</p>

<pre><code># get 'mytests.search' from argparse
dirname, module = os.path.split('mytests.search'.replace('.', '/'))
suite = unittest.TestLoader().discover(dirname, pattern='%s.py' % module)

# Modify testcases here &lt;-- THIS IS THE HARD PART
#
# Is there a way to inject the tests into another testCase class?
#
# class BaseClass(unittest.TestCase):
#    def setUp(self):
#        self.driver = webdriver.PhantomJS()
#        ...
#
#    def runTest(self): pass
#
#    def tearDown(self): ...
#
# then...
new_suite = unittest.TestSuite()
for sub_suite in master_suite:  #&lt;-- iterating over the above suite here
    for tests in sub_suite._tests:  #&lt;-- why do you have to do this twice?
        for test_name in tests._tests:
            # This doesn't work but show's my thought process
            mytest = BaseClass(methodName=test_name)
            setattr(mytest, test_func, getattr(tests, test_name))
            new_suite.addTest(mytest)

try:
    result = unittest.TextTestRunner(verbosity=2).run(new_suite)
except (SOME_SELENIUM_ERROR):
    # get the screenshot, save to file &lt;-- don't know how to do this either
</code></pre>

<p>Anyway, any help into this would be appreciated. I would really like to keep this code as simple as possible using as much of what's available as I can.</p>

<p>Note: Other than selenium and PhantomJS, I'm also wary of using other dependencies like non-standard libraries, etc, for a number of policy reasons.
    ...</p>

<p>Hi my friend i have been searchin something like this for few days. I found this question in stackoverflow</p>

<p><a href="http://stackoverflow.com/questions/11380413/python-unittest-passing-arguments">Python unittest passing arguments</a></p>

<p>so you code should be like this:</p>

<pre><code>import sys
import unittest

class Search(unittest.TestCase):
    URL = ""
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url = 'http://www.example.com'

    def test_search(self):
        driver = self.driver
        driver.get(self.base_url)
        elem = self.driver.find_element_by_name('q')
        elem.send_keys('nagios')
        elem.send_keys(Keys.RETURN)
        assert 'No results found.' not in self.driver.page_source
        driver.get(self.base_url + '/status')
        assert 'OK' in self.driver.page_source

    def tearDown(self):
        self.driver.quit()
if __name__ == "__main__":
    Search.URL=sys.argv.pop()
    unittest.main()
</code></pre>

<p>I hope this is what you was looking for.</p>

<p>to execute you should do:</p>

<pre><code>python mySearchTest.py "http://example.com"
</code></pre>

python unit-testing selenium phantomjs