Test Overview
This test navigates between two web pages using back and forward browser buttons, then refreshes the page. It verifies the page titles at each step to ensure navigation works correctly.
This test navigates between two web pages using back and forward browser buttons, then refreshes the page. It verifies the page titles at each step to ensure navigation works correctly.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest class TestNavigation(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.wait = WebDriverWait(self.driver, 10) def test_back_forward_refresh(self): driver = self.driver wait = self.wait # Open first page driver.get('https://example.com') wait.until(EC.title_contains('Example Domain')) self.assertIn('Example Domain', driver.title) # Navigate to second page driver.get('https://www.iana.org/domains/reserved') wait.until(EC.title_contains('IANA')) self.assertIn('IANA', driver.title) # Go back to first page driver.back() wait.until(EC.title_contains('Example Domain')) self.assertIn('Example Domain', driver.title) # Go forward to second page driver.forward() wait.until(EC.title_contains('IANA')) self.assertIn('IANA', driver.title) # Refresh the current page driver.refresh() wait.until(EC.title_contains('IANA')) self.assertIn('IANA', driver.title) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open with no page loaded | — | PASS |
| 2 | Navigate to 'https://example.com' | Page loads showing 'Example Domain' title | Check page title contains 'Example Domain' | PASS |
| 3 | Navigate to 'https://www.iana.org/domains/reserved' | Page loads showing 'IANA' in title | Check page title contains 'IANA' | PASS |
| 4 | Browser navigates back to previous page | Page shows 'Example Domain' title again | Check page title contains 'Example Domain' | PASS |
| 5 | Browser navigates forward to next page | Page shows 'IANA' title again | Check page title contains 'IANA' | PASS |
| 6 | Browser refreshes current page | Page reloads and shows 'IANA' title | Check page title contains 'IANA' | PASS |
| 7 | Browser closes and test ends | Browser window closed | — | PASS |
driver.back() command do in Selenium?driver.back()driver.forward() moves forward, driver.refresh() reloads the page, and closing the browser is done by driver.close().driver.refresh().driver.reload() and driver.page_refresh() do not exist in Selenium Python API. driver.navigate().refresh() is Java syntax, not Python.driver.get('https://example.com/page1')
driver.get('https://example.com/page2')
driver.back()
current_url = driver.current_url
print(current_url)driver.back().driver.current_url returns 'https://example.com/page1'.driver.get('https://example.com')
driver.back
print(driver.current_url)driver.back is missing parentheses, so it does not execute the method.driver.get() is correct, driver.current_url is the right property, and order of calls is valid.driver.refresh() reloads the page. driver.back() and driver.forward() navigate history, not refresh. driver.close() closes the browser, not reload.Pass the test and this pattern moves unknown