Handle Browser Navigation Controls Using Selenium WebDriver
Problem Statement:
Implement Selenium WebDriver to automate browser navigation actions like forward,
backward, and refresh in a web application.
Automated Browser Navigation Testing Using Selenium WebDriver
Here's a complete solution to automate browser navigation controls (back, forward, refresh)
with verification:
python
Copy
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 BrowserNavigationTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Initialize Chrome WebDriver
cls.driver = webdriver.Chrome()
cls.driver.maximize_window()
cls.wait = WebDriverWait(cls.driver, 10)
# Test URLs
cls.url1 = "https://www.google.com"
cls.url2 = "https://www.github.com"
cls.url3 = "https://www.python.org"
def test_navigation_controls(self):
"""Test browser back, forward, and refresh functionality"""
# Navigate to first page
self.driver.get(self.url1)
self.assertIn("google", self.driver.current_url.lower())
# Navigate to second page
self.driver.get(self.url2)
self.assertIn("github", self.driver.current_url.lower())
# Navigate to third page
self.driver.get(self.url3)
self.assertIn("python", self.driver.current_url.lower())
# 1. Test BACK button
self.driver.back()
self.wait.until(EC.url_contains("github"))
print(f"After BACK: {self.driver.current_url}")
# 2. Test FORWARD button
self.driver.forward()
self.wait.until(EC.url_contains("python"))
print(f"After FORWARD: {self.driver.current_url}")
# 3. Test REFRESH
original_title = self.driver.title
self.driver.refresh()
self.wait.until(EC.title_is(original_title))
print("Page refreshed successfully")
# 4. Verify page content persists after refresh
self.assertIn("python", self.driver.page_source.lower())
def test_multiple_navigation(self):
"""Test chained navigation actions"""
self.driver.get(self.url1)
self.driver.get(self.url2)
# Back to first page
self.driver.back()
self.assertIn("google", self.driver.current_url.lower())
# Forward to second page
self.driver.forward()
self.assertIn("github", self.driver.current_url.lower())
# Refresh and verify
self.driver.refresh()
self.assertIn("github", self.driver.current_url.lower())
@classmethod
def tearDownClass(cls):
# Close the browser
cls.driver.quit()
if __name__ == "__main__":
unittest.main()
Key Features of the Solution
Complete Navigation Testing:
• Back/Forward/Refresh operations with verification
• URL and content validation after each action
Robust Implementation:
• Explicit waits (WebDriverWait) for reliable navigation
• Page content verification
• Multiple test scenarios
Best Practices:
• Clean setup/teardown
• Descriptive print statements
• Comprehensive assertions
Customization Guide
1. For Your Web Application
python
Copy
# Replace these with your application URLs
cls.url1 = "https://your-app.com/page1"
cls.url2 = "https://your-app.com/page2"
cls.url3 = "https://your-app.com/page3"
2. Additional Test Cases
python
Copy
def test_navigation_history(self):
"""Test navigation history length"""
self.driver.get(self.url1)
self.driver.get(self.url2)
self.driver.get(self.url3)
# JavaScript to check history length
history_length = self.driver.execute_script("return window.history.length")
self.assertGreaterEqual(history_length, 3)
3. Handling Authentication
python
Copy
# For pages requiring login (store cookies first)
self.driver.add_cookie({"name": "session", "value": "12345"})
self.driver.refresh()
Best Practices
✔ Always verify navigation results (URL/title/content)
✔ Use explicit waits after navigation commands
✔ Test edge cases (empty history, rapid navigation)
✔ Combine with page objects for maintainable code
Common Issues & Fixes
Issue: StaleElementReferenceException after navigation
Fix: Re-locate elements after page changes
Issue: Timing issues with page loads
Fix: Use EC.url_to_be() or EC.title_is()
Issue: Browser cache affecting tests
Fix: Clear cache with driver.delete_all_cookies()