EdgeOptions configuration in Selenium Java - Build an Automation Script
import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.edge.EdgeOptions; import org.openqa.selenium.Dimension; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.awt.Toolkit; public class EdgeOptionsTest { private WebDriver driver; @BeforeEach public void setUp() { // Create EdgeOptions object EdgeOptions options = new EdgeOptions(); // Start browser maximized options.addArguments("--start-maximized"); // Disable notifications options.addArguments("--disable-notifications"); // Initialize EdgeDriver with options driver = new EdgeDriver(options); } @Test public void testEdgeOptionsConfiguration() { // Navigate to example.com driver.get("https://www.example.com"); // Get screen size for comparison Dimension screenSize = new Dimension( Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height ); // Get browser window size Dimension windowSize = driver.manage().window().getSize(); // Assert window is maximized (width and height at least screen size minus some OS taskbar) Assertions.assertTrue(windowSize.getWidth() >= screenSize.getWidth() - 50, "Browser width should be maximized"); Assertions.assertTrue(windowSize.getHeight() >= screenSize.getHeight() - 50, "Browser height should be maximized"); // Assert page title contains 'Example Domain' String title = driver.getTitle(); Assertions.assertTrue(title.contains("Example Domain"), "Page title should contain 'Example Domain'"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test class uses Selenium WebDriver with Java and JUnit 5.
setUp(): Creates an EdgeOptions object and adds arguments to start the browser maximized and disable notifications. Then it initializes the EdgeDriver with these options.
testEdgeOptionsConfiguration(): Navigates to https://www.example.com. It then compares the browser window size to the screen size to verify the window is maximized (allowing a small margin for OS taskbars). It also asserts the page title contains 'Example Domain'.
tearDown(): Ensures the browser closes after the test to free resources.
This approach uses best practices like configuring browser options explicitly, verifying UI state with assertions, and cleaning up resources.
Now add data-driven testing with 3 different URLs and verify the page title contains expected text for each.
Practice
EdgeOptions in Selenium Java tests?Solution
Step 1: Understand EdgeOptions role
EdgeOptionsis used to set browser preferences and configurations before starting Edge.Step 2: Differentiate from other test tasks
Assertions, locating elements, and reporting are separate tasks not handled byEdgeOptions.Final Answer:
To customize Microsoft Edge browser settings before launching it -> Option AQuick Check:
EdgeOptions = browser setup [OK]
- Confusing EdgeOptions with assertion or locator classes
- Thinking EdgeOptions generates reports
- Using EdgeOptions to find elements
--headless to an EdgeOptions object in Java?Solution
Step 1: Recall method to add arguments
The correct method to add command-line arguments isaddArguments(String... args).Step 2: Check argument format
The argument must be "--headless" with two dashes, matching browser flags.Final Answer:
options.addArguments("--headless"); -> Option AQuick Check:
Use addArguments("--headless") [OK]
- Using setArguments instead of addArguments
- Missing dashes in argument string
- Confusing addArgument (singular) which doesn't exist
EdgeOptions options = new EdgeOptions();
options.addArguments("--disable-gpu");
options.setCapability("acceptInsecureCerts", true);
EdgeDriver driver = new EdgeDriver(options);Solution
Step 1: Analyze addArguments call
Adding "--disable-gpu" disables GPU hardware acceleration in Edge.Step 2: Analyze setCapability call
Setting "acceptInsecureCerts" to true allows the browser to accept insecure SSL certificates.Final Answer:
The browser disables GPU hardware acceleration and accepts insecure SSL certificates -> Option CQuick Check:
Disable GPU + accept insecure certs = The browser disables GPU hardware acceleration and accepts insecure SSL certificates [OK]
- Confusing headless mode with disable-gpu
- Assuming default SSL behavior without checking capability
- Ignoring the effect of setCapability on SSL
EdgeOptions options = new EdgeOptions();
options.addArguments("headless");
options.setCapability("acceptInsecureCerts", "true");
EdgeDriver driver = new EdgeDriver(options);Solution
Step 1: Check argument format
"headless" argument must be "--headless" with two dashes to work properly.Step 2: Check capability value type
"acceptInsecureCerts" expects a boolean value, not a string "true".Final Answer:
The argument "headless" is missing dashes and acceptInsecureCerts should be boolean, not string -> Option BQuick Check:
Arguments need dashes + boolean capability [OK]
- Using string "true" instead of boolean true
- Missing dashes in command-line arguments
- Thinking setArguments exists instead of addArguments
Solution
Step 1: Verify argument syntax
Arguments must have double dashes and be passed correctly. EdgeOptions options = new EdgeOptions(); options.addArguments("--headless", "--disable-extensions"); options.setCapability("acceptInsecureCerts", true); uses "--headless" and "--disable-extensions" properly.Step 2: Check capability for insecure certs
Capability "acceptInsecureCerts" must be boolean true to accept insecure certificates, as in EdgeOptions options = new EdgeOptions(); options.addArguments("--headless", "--disable-extensions"); options.setCapability("acceptInsecureCerts", true);.Step 3: Confirm no syntax errors
EdgeOptions options = new EdgeOptions(); options.addArguments("--headless", "--disable-extensions"); options.setCapability("acceptInsecureCerts", true); uses correct methods and types; others have missing dashes, wrong method names, or wrong boolean values.Final Answer:
EdgeOptions options = new EdgeOptions(); options.addArguments("--headless", "--disable-extensions"); options.setCapability("acceptInsecureCerts", true); -> Option DQuick Check:
Correct args + boolean capability = EdgeOptions options = new EdgeOptions(); options.addArguments("--headless", "--disable-extensions"); options.setCapability("acceptInsecureCerts", true); [OK]
- Missing dashes in arguments
- Passing string instead of boolean for capabilities
- Using non-existent setArguments method
Ready to prove it?
Pass the test and this pattern moves unknown
