This introduction should give you some hints about Python and Selenium WebDriver. I will use this in following tutorials as a base.
Preconditions
- Python installed
- pip (package manager) installed
- Editor or IDE installed
Preparation
As first step simply install or upgrade the Selenium package.
# install or upgrade selenium $ pip install -U selenium # get information about package $ pip show selenium
This is a fairly simple process. After the successful command execution you will have the Selenium WebDriver client library on your machine with all that is needed to create automated scripts.
The first script
Now start using the unittest library. The script comments help to describe the code.
#!/usr/bin/env python import unittest from selenium import webdriver class SearchContentOnWebsite(unittest.TestCase): """define a class that inherits the TestCase class""" def setUp(self): """perform some tasks at the start of each test""" # create a new Firefox session self.driver = webdriver.Firefox() # wait for a certain amount of time self.driver.implicitly_wait(30) # maximize browser window self.driver.maximize_window() # navigate to the start URL self.driver.get("http://softwaretester.info") def test_search_headline(self): """a very simple test case""" link_text = 'Modern Status Plugin' title = 'Jenkins - Modern Status Plugin | - Softwaretester -' # find a element with partial text elem = self.driver.find_element_by_partial_link_text(link_text) # click element elem.click() # assert that title have value assert title in self.driver.title def tearDown(self): """method to clean up any initialized values after the test""" # close the browser window self.driver.close() if __name__ == "__main__": unittest.main(verbosity=2)
Run Test
To run the test simple call your script.
./my_first_test.py # or python my_first_test.py