from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import unittest
class TestPageTitleAndURL(unittest.TestCase):
def setUp(self):
options = Options()
options.add_argument('--headless') # Run in headless mode for testing
self.driver = webdriver.Chrome(service=Service(), options=options)
def test_title_and_url(self):
self.driver.get('https://example.com')
title = self.driver.title
current_url = self.driver.current_url
self.assertEqual(title, 'Example Domain', f"Expected title 'Example Domain' but got '{title}'")
self.assertEqual(current_url, 'https://example.com/', f"Expected URL 'https://example.com/' but got '{current_url}'")
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()The setUp method initializes the Chrome WebDriver in headless mode to run tests without opening a visible browser window.
The test_title_and_url method navigates to 'https://example.com', retrieves the page title and current URL, then asserts both match the expected values exactly.
The tearDown method ensures the browser closes after the test, freeing resources.
This structure uses Python's unittest framework for clear setup, test, and cleanup phases, making the test reliable and easy to maintain.