Browser profile management helps you control browser settings and data during automated tests. It makes tests more reliable and realistic.
0
0
Browser profile management in Selenium Java
Introduction
You want to test a website with specific browser settings like language or homepage.
You need to keep cookies or cache between test runs.
You want to simulate a fresh browser without any saved data.
You want to add browser extensions during testing.
You want to test how your site behaves with different user profiles.
Syntax
Selenium Java
FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("key", "value"); FirefoxOptions options = new FirefoxOptions(); options.setProfile(profile); WebDriver driver = new FirefoxDriver(options);
Use FirefoxProfile to create or customize a Firefox browser profile.
Set preferences to change browser behavior before starting the test.
Examples
This example sets the browser homepage to https://example.com before launching Firefox.
Selenium Java
FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("browser.startup.homepage", "https://example.com"); FirefoxOptions options = new FirefoxOptions(); options.setProfile(profile); WebDriver driver = new FirefoxDriver(options);
This disables JavaScript in the browser to test how the site behaves without it.
Selenium Java
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);This loads an existing Firefox profile from disk to reuse saved data and settings.
Selenium Java
FirefoxProfile profile = new FirefoxProfile(new File("path/to/custom/profile"));
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);Sample Program
This test creates a Firefox browser with a custom profile that sets the homepage and disables JavaScript. It opens the browser, navigates to about:home, prints the URL, and closes the browser.
Selenium Java
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; import java.io.File; public class BrowserProfileTest { public static void main(String[] args) { // Create a new Firefox profile FirefoxProfile profile = new FirefoxProfile(); // Set homepage to example.com profile.setPreference("browser.startup.homepage", "https://example.com"); // Disable JavaScript profile.setPreference("javascript.enabled", false); // Set profile in Firefox options FirefoxOptions options = new FirefoxOptions(); options.setProfile(profile); // Start Firefox with the profile WebDriver driver = new FirefoxDriver(options); // Open homepage driver.get("about:home"); // Print current URL System.out.println("Current URL: " + driver.getCurrentUrl()); // Close browser driver.quit(); } }
OutputSuccess
Important Notes
Always quit the browser after the test to free resources.
Profile settings must be set before starting the browser.
Using profiles helps simulate real user environments in tests.
Summary
Browser profiles let you customize browser settings for tests.
Set preferences before launching the browser.
Profiles improve test reliability and realism.