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

About Micropyramid

Micropyramid is a software development and cloud consulting partner for enterprise businesses across the world. We work on python, Django, Salesforce, Angular, Reactjs, React Native, MySQL, PostgreSQL, Docker, Linux, Ansible, git, amazon web services. We are Amazon and salesforce consulting partner with 5 years of cloud architect experience. We develop e-commerce, retail, banking, machine learning, CMS, CRM web and mobile applications.

Need any Help in your Project?Let's Talk


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