0
0
Selenium Javatesting~15 mins

Browser profile management in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify browser profile settings are applied correctly in Selenium
Preconditions (2)
Step 1: Create a new Firefox profile with a custom preference (e.g., disable images loading)
Step 2: Launch Firefox browser using Selenium WebDriver with the created profile
Step 3: Navigate to a website that contains images (e.g., https://example.com)
Step 4: Verify that images are not loaded due to the profile setting
✅ Expected Result: Firefox browser launches with the custom profile settings applied, and images on the website do not load
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the browser is launched with the custom profile
Verify that images are not loaded on the webpage
Best Practices:
Use FirefoxProfile to set preferences before launching the browser
Use explicit waits if needed to ensure page load
Use Page Object Model if expanding tests
Close the browser properly after test
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class BrowserProfileTest {
    public static void main(String[] args) {
        // Set system property for geckodriver path if needed
        // System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");

        // Create Firefox profile
        FirefoxProfile profile = new FirefoxProfile();
        // Disable image loading: set preference to 2 (block images)
        profile.setPreference("permissions.default.image", 2);

        // Set Firefox options with profile
        FirefoxOptions options = new FirefoxOptions();
        options.setProfile(profile);

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

        try {
            // Navigate to example.com
            driver.get("https://example.com");

            // Wait for page to load
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
            wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("body")));

            // Verify images are not loaded
            // Check if any <img> elements have naturalWidth > 0 (means loaded)
            boolean imagesLoaded = (Boolean) ((org.openqa.selenium.JavascriptExecutor) driver).executeScript(
                    "return Array.from(document.images).some(img => img.naturalWidth > 0);"
            );

            if (!imagesLoaded) {
                System.out.println("Test Passed: Images are blocked as per profile settings.");
            } else {
                System.out.println("Test Failed: Images are loaded despite profile settings.");
            }

        } finally {
            // Close browser
            driver.quit();
        }
    }
}

This test script creates a Firefox profile and sets a preference to block images from loading. It then launches Firefox with this profile using Selenium WebDriver.

After navigating to a website with images, it uses JavaScript execution to check if any images have loaded by checking their naturalWidth property.

If no images are loaded, the test prints a pass message; otherwise, it prints a fail message.

The script uses explicit waits to ensure the page body is present before checking images.

Finally, the browser is closed properly in a finally block to avoid resource leaks.

Common Mistakes - 3 Pitfalls
Not setting the Firefox profile in FirefoxOptions before creating the driver
Using Thread.sleep() instead of explicit waits
Checking image presence by counting <img> tags instead of verifying if images actually loaded
Bonus Challenge

Now add data-driven testing with 3 different Firefox profile preferences to block images, block JavaScript, and block CSS.

Show Hint