Unit testing with selenium-python

What is selenium?
Selenium automates browser. It performs all actions which human can do. Like form submission, button clicks, mouse actions, checking page content.

What is Unit tests?
It’s a small test case, which tests single function or small code. With this, we are sure that small part of code/functionality works as we expected.

Selenium + Unit Tests
We use selenium to write unit tests, to test user interface functionalities like user login, check page content, etc.

Selenium Installation:
Pip install selenium

Example 1:
Test if we have at least one link with name “Web Development” in https://micropyramid.com


import unittest
from selenium import webdriver

GECKO_DRIVER_PATH = '/home/ravi/earth/autods/autoorders/geckodriver'

class FindLink(unittest.TestCase):

   def setUp(self):
       self.driver = webdriver.Firefox(executable_path=GECKO_DRIVER_PATH)
   def test_link(self):
       web_dev_link = self.driver.find_elements_by_partial_link_text('Web Development')
       # Test atleast we have one link with name  "Web Development"

   def tearDown(self):

if __name__ == '__main__':


Set Up Method: This method will initially called before calling your test method. You can write text fixtures or set up environment to run tests, etc. In above set up method, I've initialized firefox web browser. 

TearDown Method: called immediately after every test case finish execution. You can write any cleanup code. In above method, we closed opened firefox browser.

Test_link: Every test case name should start with test_ name. Here we simply calling selenium method to find all elements with a name. “Web Development”.

Example 2:

class TestLogin(unittest.TestCase):

   def setUp(self):
       self.driver = webdriver.Firefox(executable_path=GECKO_DRIVER_PATH)

   def test_search_by_text(self):
       email = self.driver.find_element_by_name("email")
       email = self.driver.find_element_by_name("password")
       sbmt_button = self.driver.find_element_by_xpath('.//button[@class="btn btn-default login_form_button"]')
       self.assertTrue("hello@example.com" in self.driver.find_element_by_id("account_email"))

   def tearDown(self):

Posted On 15 February 2019 By MicroPyramid

Need any Help in your Project?Let's Talk

Latest Comments
Understanding Python Properties

Python Properties is a class for managing class attributes in Python. Property( ) is a built-in function that creates and returns a property object

Continue Reading...

Generating PDF Files in Python using xhtml2pdf

There are many ways for generating PDF in python. In this post I will be presenting PDF files generation with xhtml2pdf.

xhtml2pdf: xhtml2pdf is a ...

Continue Reading...
Create excel file, Insert image, Draw Bar Graphs in excel files in python using xlsxwriter

Xlsxwriter is a python module through which we can write data to Excel 2007+ XLSX file format. In this blog post we learn to write ...

Continue Reading...

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties