Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20



Using Python Selenium to Write Tests

Users can write test cases using Python's unittest module. In this article, we use unittest as the test framework for Python Selenium WebDriver. This test is for login functionality. 

Test Case: authorized users are able to login Mahara Demo site. Here are test steps. 

1. Navigate to and verify that web page title "Home-Mahara" displays. 

2. Enter "student2" in the Username filed. 

3. Enter "Testing1" in the Password field. 

4. Click on the Login button

5. Assert that Logout link is present. 

Python Code for Test Implementation

Run Python GUI (IDLE) and write the following code. 

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class DemoMaharaOrgLogin(unittest.TestCase):

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

    def test_login_in_demo_mahara_org(self):
        driver = self.driver
        self.assertIn("Home - Mahara", driver.title)
        username = driver.find_element_by_id("login_login_username")
        self.assertTrue(driver.find_element_by_link_text("Logout"),"Logout link")

    def tearDown(self):

if __name__ == "__main__":

Save the code as and then click on "Run Module F5" under Run menu to execute the test. Here is the test result. This result indicated that the test passed. 

Ran 1 test in 14.662s


You can also run the above test case from a shell like this: