Practice Set 2
Practice Set 2
Problem Statement:
Here's a complete solution to automate browser navigation controls (back, forward, refresh)
with verification:
python
Copy
import unittest
class BrowserNavigationTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.driver.maximize_window()
# 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):
self.driver.get(self.url1)
self.assertIn("google", self.driver.current_url.lower())
self.driver.get(self.url2)
self.assertIn("github", self.driver.current_url.lower())
self.driver.get(self.url3)
self.assertIn("python", self.driver.current_url.lower())
self.driver.back()
self.wait.until(EC.url_contains("github"))
self.driver.forward()
self.wait.until(EC.url_contains("python"))
# 3. Test REFRESH
original_title = self.driver.title
self.driver.refresh()
self.wait.until(EC.title_is(original_title))
self.assertIn("python", self.driver.page_source.lower())
def test_multiple_navigation(self):
self.driver.get(self.url1)
self.driver.get(self.url2)
self.driver.back()
self.assertIn("google", self.driver.current_url.lower())
self.driver.forward()
self.assertIn("github", self.driver.current_url.lower())
self.driver.refresh()
self.assertIn("github", self.driver.current_url.lower())
@classmethod
def tearDownClass(cls):
if __name__ == "__main__":
unittest.main()
Robust Implementation:
Best Practices:
• Clean setup/teardown
• Comprehensive assertions
Customization Guide
python
Copy
cls.url1 = "https://your-app.com/page1"
cls.url2 = "https://your-app.com/page2"
cls.url3 = "https://your-app.com/page3"
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)
self.assertGreaterEqual(history_length, 3)
3. Handling Authentication
python
Copy
self.driver.refresh()
Best Practices