0
0
Selenium Javatesting~15 mins

FirefoxOptions configuration in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Configure Firefox browser with custom options and verify browser launch
Preconditions (2)
Step 1: Create a FirefoxOptions object
Step 2: Set the browser to run in headless mode
Step 3: Add an argument to disable notifications
Step 4: Set a custom profile if needed
Step 5: Initialize FirefoxDriver with these options
Step 6: Navigate to https://www.example.com
Step 7: Verify the page title is 'Example Domain'
✅ Expected Result: Firefox browser launches with the specified options, navigates to the page, and the page title matches 'Example Domain'
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify FirefoxDriver is initialized with FirefoxOptions
Verify page title equals 'Example Domain'
Best Practices:
Use explicit waits if needed
Use FirefoxOptions to configure browser settings
Use assertions from a testing framework like TestNG or JUnit
Close the browser after test execution
Automated Solution
Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class FirefoxOptionsTest {
    private WebDriver driver;

    @BeforeMethod
    public void setUp() {
        // Create FirefoxOptions object
        FirefoxOptions options = new FirefoxOptions();
        // Run browser in headless mode
        options.setHeadless(true);
        // Disable notifications
        options.addPreference("dom.webnotifications.enabled", false);

        // Initialize FirefoxDriver with options
        driver = new FirefoxDriver(options);
    }

    @Test
    public void testFirefoxOptionsConfiguration() {
        // Navigate to example.com
        driver.get("https://www.example.com");

        // Verify page title
        String title = driver.getTitle();
        Assert.assertEquals(title, "Example Domain", "Page title should be 'Example Domain'");
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test class uses Selenium WebDriver with Java and TestNG framework.

In setUp(), we create a FirefoxOptions object to configure the browser:

  • Set headless mode to run without UI.
  • Disable web notifications using preferences.

We then initialize FirefoxDriver with these options.

The test method navigates to https://www.example.com and asserts the page title is exactly 'Example Domain'.

Finally, tearDown() closes the browser to clean up.

This structure ensures the browser runs with the desired options and the test verifies the expected page loaded correctly.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not using FirefoxOptions and initializing FirefoxDriver without options', 'why_bad': "The browser will launch with default settings, so custom configurations like headless mode or disabling notifications won't apply.", 'correct_approach': 'Always create a FirefoxOptions object, configure it, and pass it to the FirefoxDriver constructor.'}
{'mistake': 'Using deprecated methods or incorrect preference keys', 'why_bad': "Deprecated methods may not work in newer versions, and wrong preference keys won't disable features as expected.", 'correct_approach': 'Use up-to-date API methods and verify preference keys from official Firefox documentation.'}
Not quitting the driver after test execution
Hardcoding waits or not using waits when needed
Bonus Challenge

Now add data-driven testing to run the test with headless mode enabled and disabled

Show Hint