Customizing Selenium RC to take command line args

Posted December 30th, 2008 by spanky

I’ve written a few lines of code that you can use to pass a URL or the browser type you want to use from the command line, using Python.  At the bottom of your Python-based selenium test, you can replace the “if __name__ ==” part with the following code:

if __name__ == "__main__":

    ### custom
    import sys, getopt
    global testurl, browser
    testurl = "http://127.0.0.1"
    browser = "*chrome"
    try:
        opts, args = getopt.getopt(sys.argv[1:], "", \
            ["url=","browser="])
        for opt, arg in opts:
            if opt in ("--url",):
                testurl = arg
                argument = opt + "=" + arg
                sys.argv.remove(argument)
            if opt in ("--browser",):
                browser = "*" + arg
                argument = opt + "=" + arg
                sys.argv.remove(argument)

    except:
        pass
    ### /custom

    unittest.main()

and the setUp method with:

    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, \
            browser, testurl)
        self.selenium.start()

Then on the command line, you can go:

python test_myTest.py --url=http://myurl.com --browser=iexplore

I’m sure the code could be better, but I’m not that great with getopt.

~Spanky

Comments are closed.