Course Content
Selenium Python First Script
Selenium Python First Script
0/2
HTML Form elements
HTML Form elements
0/1
Handle HTML Frames
Handle HTML Frames
0/2
Handling Dropdown Lists
Handling Dropdown Lists
0/2
Selenium Action class
Selenium Action class
0/1
Selenium Python for Beginners [ 2024 ]
About Lesson

Google Homepage Example

In this lesson, you will test Google search page. The test will launch Google home page, search with a sample keyword and clicks the search button.

The test verifies the browser title if that contains the search word. The test implicitly waits for 15 secs before performing or executing the statements.

Google Test

 

# Google Homepage Test Example
# www.TestingDocs.com 

import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By

class GoogleTest(unittest.TestCase): 
    
    def setUp(self):
        self.driver = webdriver.Chrome()
        
        
    def test_title(self):
        driver=self.driver 
        driver.get("https://google.com")
        driver.implicitly_wait(15)
        search_box = driver.find_element(By.NAME, "q")
        search_box.send_keys("test")
        search_button = driver.find_element(By.NAME, "btnK")
        search_button.click()
        self.assertIn("test", driver.title)
        
    
    def tearDown(self):
        self.driver.quit()
        
            
if __name__ == '__main__': 
    unittest.main()

 

Run the test and verify the test output.

Explanation

The import statements import the unit test and selenium modules. The test class inherits from the unittest.TestCase

The setUp() method initializes the Chrome browser. The driver object will created.

test_method()
The test method navigates to the Google homepage, performs a search, and validates the search page title after the search.

The tearDown() closes the browser after the test. The unit test is executed by calling the unittest.main() method.

Join the conversation