0
0
Selenium Pythontesting~10 mins

Back, forward, and refresh in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - Selenium
Selenium Python
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()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open with no page loaded-PASS
2Navigate to 'https://example.com'Page loads showing 'Example Domain' titleCheck page title contains 'Example Domain'PASS
3Navigate to 'https://www.iana.org/domains/reserved'Page loads showing 'IANA' in titleCheck page title contains 'IANA'PASS
4Browser navigates back to previous pagePage shows 'Example Domain' title againCheck page title contains 'Example Domain'PASS
5Browser navigates forward to next pagePage shows 'IANA' title againCheck page title contains 'IANA'PASS
6Browser refreshes current pagePage reloads and shows 'IANA' titleCheck page title contains 'IANA'PASS
7Browser closes and test endsBrowser window closed-PASS
Failure Scenario
Failing Condition: Page title does not match expected after navigation or refresh
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after calling driver.back()?
AThe browser closes
BThe page title contains 'Example Domain'
CThe page URL changes to 'https://www.iana.org/domains/reserved'
DThe page refreshes automatically
Key Result
Always verify page navigation by checking page titles or unique elements after back, forward, and refresh actions to ensure the browser state is as expected.