Today i will tell how to Write a unit test for a test step involving checking 3 dynamic links that changes over time and the validity of those links.
For this the requirement is very easy we need Python 2.7 and selenium installed in system under test.
To install Python one can download from
https://www.python.org/download/releases/2.7.7/
After successfully installing python go to your cmd prompt and check for python by typing python and pressing enter.
If the python interpreter starts we are ready to go to next step else one needs to put the bin directory path in the system path. Google " how to set python path in system path" if python is not set in the system path.
Then we need to install selenium by simply using pip -
pip install selenium for windows
sudo pip install selenium for linux users.
After this we are ready to automate the site say "Example Site"
Next create a python file and name it as -TC_001.py
Write click on the file and open with IDLE.
For using unittest we have to use setup, test and teardown methods for the unittest to understand what we intend to do with the class.
In the teardown we have to create instance of the gecko or chrome driver which ever we are working with like this.
def setUp(self):
# create a new Firefox session
self.driver = webdriver.Firefox()
This tells the unittest to create a firefox driver session to interact with the browser.
Next we have to write the test method that has the core logic of test steps.
def test_verify_links_not_same(self):
log.warning("Starting test of verifying links are not same below search box")
self.driver.get("https://examplesite.com/")
log.warning("opening site")
Now we need to parse the links and open each page individually to confirm that the links are working and then take a screenshot for our confirmation.
temp = ""
target = self.driver.find_elements_by_xpath('//*[@id="search-bar"]/div/div[1]/span')
for paths in target:
temp = temp + paths.text
links = ' '.join(temp.split(' ')[3:])
log.warning("the links below the search box are " + str(links))
path1 = self.driver.find_element_by_xpath('//*[@id="search-bar"]/div/div[1]/span/a[1]').click()
time.sleep(3)
self.driver.save_screenshot("Link1.png")
self.driver.back()
time.sleep(3)
path2 = self.driver.find_element_by_xpath('//*[@id="search-bar"]/div/div[1]/span/a[2]').click()
time.sleep(3)
self.driver.save_screenshot("Link2.png")
self.driver.back()
time.sleep(3)
path3 = self.driver.find_element_by_xpath('//*[@id="search-bar"]/div/div[1]/span/a[3]').click()
time.sleep(3)
self.driver.save_screenshot("Link3.png")
self.driver.back()
time.sleep(3)
log.warning("test_verify_links_not_same completed")
Here a thread sleep is put for any server lag that may happen while running the test.
Note: We are not handling any negative scenarios that may arise while running the test.
we can use try catch or explicit waits or even web driver wait before each element is interacted with (driver find by element).
After this we need to clean the test after running i.e close the driver which is running in teard own method.
def tearDown(self):
# close the browser window
self.driver.quit()
This is how we can write a python unit test to automate a test step of verifying links in webpage and take a screenshot of each link after opening.