cppcoder
cppcoder

Reputation: 23095

Python webdriver from Selenium

I am a newbie in selenium automation. I have created a Selenium test case & test suite. I exported the test suite as a Python webdriver.

How should I execute this python code? I tried this:

./pythonwebdriver <selenium test case.html>

I get this error:

Traceback (most recent call last):
File "./pythondriver.py", line 52, in <module>
unittest.main()
File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
self.parseArgs(argv)
File "/usr/lib/python2.7/unittest/main.py", line 149, in parseArgs
self.createTests()
File "/usr/lib/python2.7/unittest/main.py", line 158, in createTests
self.module)
File "/usr/lib/python2.7/unittest/loader.py", line 128, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute '<testcasename>'

Upvotes: 0

Views: 3756

Answers (2)

jfs
jfs

Reputation: 414079

Your script calls unittest.main() that processes the command-line argument: <selenium test case.html>. unittest.main() expects name of test modules, test classes or test methods as command-line arguments, not <selenium test case.html>.

Upvotes: 1

Edu
Edu

Reputation: 2052

There is no such thing as Python webdriver. Webdriver is a component for driving webpages. It has been integrated to the Selenium 2. Natively it works in Java, but there are bindings available for many languages, including Python.

Here's an annotated example from the webdriver documentation modified a little. For creating a unittest, make a test class that inherits the class TestCase provided by unittest module.

#!/usr/bin/python

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import unittest

class GoogleTest(unittest.TestCase):
    def test_basic_search(self):
        # Create a new instance of the Firefox driver
        driver = webdriver.Firefox()
        driver.implicitly_wait(10)

        # go to the google home page
        driver.get("http://www.google.com")

        # find the element that's name attribute is q (the google search box)
        inputElement = driver.find_element_by_name("q")

        # type in the search
        inputElement.send_keys("Cheese!")

        # submit the form (although google automatically searches 
        # now without submitting)
        inputElement.submit()

        # the page is ajaxy so the title is originally this:
        original_title = driver.title

        try:
            # we have to wait for the page to refresh, the last thing 
            # that seems to be updated is the title
            WebDriverWait(driver, 10).until(lambda driver :  
                                     driver.title != original_title)
            self.assertIn("cheese!", driver.title.lower())

            # You should see "cheese! - Google Search"
            print driver.title
        finally:
            driver.quit()

if __name__ == '__main__':
    unittest.main()

One nice thing about webdriver is that you can change the driver line to be

driver = webdriver.Chrome()
driver = webdriver.Firefox()
driver = webdriver.Ie()

depending on what browsers you need to test. In addition to ChromeDriver, FirefoxDriver or InternetExplorerDriver there's also HtmlUnitDriver which is most lightweight and can run headless (but may run some javascript differently than browsers), RemoteWebDriver which allows running tests on remote machines and in parallel, and many others (iPhone, Android, Safari, Opera).

Running it can be done as running any python script. Either just with:

python <script_name.py>

or including the interpreter name on the first line like !#/usr/bin/python above. The last two lines

if __name__ == '__main__':
    unittest.main()

make the script run the test when this file is run directly like ./selenium_test.py. It is also possible to collect test cases automatically from multiple files and run them together (see unittest documentation). Another way running tests in some module or some individual test is

python -m unittest selenium_test.GoogleTest

Upvotes: 6

Related Questions