Test Overview
This test uses Selenium with Python to open a webpage and run JavaScript code inside the browser. It verifies that the JavaScript executor correctly returns the page title.
This test uses Selenium with Python to open a webpage and run JavaScript code inside the browser. It verifies that the JavaScript executor correctly returns the page title.
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options # Setup Chrome driver options = Options() options.add_argument('--headless') service = Service() driver = webdriver.Chrome(service=service, options=options) try: # Open example page driver.get('https://example.com') # Execute JavaScript to get document title title = driver.execute_script('return document.title;') # Assert the title is 'Example Domain' assert title == 'Example Domain', f"Title was '{title}' instead of 'Example Domain'" finally: driver.quit()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser is launched in headless mode | Chrome browser is open but not visible, ready to navigate | - | PASS |
| 2 | Browser navigates to 'https://example.com' | Page loads with title 'Example Domain' visible in DOM | - | PASS |
| 3 | Execute JavaScript 'return document.title;' using Selenium's execute_script method | JavaScript runs inside browser context and returns the page title string | Check that returned title equals 'Example Domain' | PASS |
| 4 | Assert that the returned title is exactly 'Example Domain' | Title string matches expected value | assert title == 'Example Domain' | PASS |
| 5 | Close the browser and end the test | Browser closed, resources released | - | PASS |