Test Overview
This test opens a web page, executes JavaScript to change the page title, and verifies the title was updated correctly.
This test opens a web page, executes JavaScript to change the page title, and verifies the title was updated correctly.
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class ExecuteJavaScriptTest { private WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } @Test public void testChangeTitleWithJavaScript() { driver.get("https://example.com"); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.title = 'New Title';"); String title = driver.getTitle(); assertEquals("New Title", title); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open but no page loaded yet | - | PASS |
| 2 | Navigates to https://example.com | Page https://example.com is loaded with default title 'Example Domain' | - | PASS |
| 3 | Executes JavaScript to change document title to 'New Title' | Page title is now 'New Title' | - | PASS |
| 4 | Retrieves the page title using driver.getTitle() | Page title retrieved as 'New Title' | Check if title equals 'New Title' | PASS |
| 5 | Test ends and browser closes | Browser window closed | - | PASS |