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.