Test Overview
This test opens a web page, retrieves its title and URL, and verifies they match expected values.
This test opens a web page, retrieves its title and URL, and verifies they match expected values.
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By import unittest class TestPageTitleAndURL(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(service=Service()) def test_title_and_url(self): driver = self.driver driver.get('https://example.com') title = driver.title url = driver.current_url self.assertEqual(title, 'Example Domain') self.assertEqual(url, 'https://example.com/') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open, ready for commands | — | PASS |
| 2 | Browser navigates to 'https://example.com' | Browser displays the Example Domain homepage | — | PASS |
| 3 | Retrieve page title using driver.title | Title retrieved: 'Example Domain' | Check if title equals 'Example Domain' | PASS |
| 4 | Retrieve current URL using driver.current_url | URL retrieved: 'https://example.com/' | Check if URL equals 'https://example.com/' | PASS |
| 5 | Assertions for title and URL pass | Test confirms title and URL match expected values | Both assertions are true | PASS |
| 6 | Browser closes and test ends | Browser window closed, resources released | — | PASS |
driver.titledriver.title property returns the title of the current page as a string.driver.page_source returns the HTML source, driver.current_url returns the URL, and driver.get_title() is not a valid method in Selenium Python.driver.current_url is a property that returns the current page URL as a string.driver.get_current_url(), driver.url(), and driver.getUrl use invalid method names or syntax. Only url = driver.current_url is correct without parentheses.print(driver.title) print(driver.current_url)
driver.title returns the page title string, here "Welcome Page".driver.current_url returns the current URL string, here "https://example.com/home".print(driver.get_title()) print(driver.get_current_url())
get_title() or get_current_url() in Selenium Python WebDriver.driver.title and driver.current_url without parentheses as they are properties, not methods.driver.title to get the page title and driver.current_url to get the URL. Use == to check exact title and 'in' to check substring in URL.Pass the test and this pattern moves unknown