CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-seleniumhq-selenium--selenium-firefox-driver

Selenium Firefox WebDriver implementation for automating Firefox browser interactions

Pending
Overview
Eval results
Files

context-management.mddocs/

Context Management

Firefox-specific context switching between content and chrome contexts for advanced browser automation scenarios. This feature allows automation of both web content and browser UI elements.

Capabilities

HasContext Interface

Interface for managing Firefox command execution context.

/**
 * Interface for managing Firefox command context.
 * Allows switching between content context (web pages) and chrome context (browser UI).
 */
@Beta
public interface HasContext {
    
    /**
     * Sets the current command execution context.
     * @param context FirefoxCommandContext specifying the target context
     */
    void setContext(FirefoxCommandContext context);
    
    /**
     * Gets the current command execution context.
     * @return Current FirefoxCommandContext
     */
    FirefoxCommandContext getContext();
}

Usage Examples:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.HasContext;
import org.openqa.selenium.firefox.FirefoxCommandContext;

FirefoxDriver driver = new FirefoxDriver();
HasContext contextManager = driver;  // FirefoxDriver implements HasContext

// Get current context
FirefoxCommandContext currentContext = contextManager.getContext();
System.out.println("Current context: " + currentContext);

// Switch to chrome context for browser UI automation
contextManager.setContext(FirefoxCommandContext.CHROME);

// Switch back to content context for web page automation
contextManager.setContext(FirefoxCommandContext.CONTENT);

FirefoxCommandContext Enum

Enumeration defining valid Firefox command execution contexts.

/**
 * Valid values for Firefox command context.
 * Determines whether commands target web content or browser chrome.
 */
public enum FirefoxCommandContext {
    
    /**
     * Content context - targets web page content.
     * Default context for standard WebDriver operations.
     */
    CONTENT("content"),
    
    /**
     * Chrome context - targets browser UI elements.
     * Used for automating Firefox browser interface.
     */
    CHROME("chrome");
    
    /**
     * Returns string representation of the context.
     * @return Context name as string
     */
    public String toString();
    
    /**
     * Creates FirefoxCommandContext from string representation.
     * @param text String representation ("content" or "chrome")
     * @return Corresponding FirefoxCommandContext enum value
     * @throws IllegalArgumentException if text is not valid context name
     */
    public static FirefoxCommandContext fromString(String text);
}

Usage Examples:

// Create contexts from strings
FirefoxCommandContext contentCtx = FirefoxCommandContext.fromString("content");
FirefoxCommandContext chromeCtx = FirefoxCommandContext.fromString("chrome");

// Convert to string
String contextName = FirefoxCommandContext.CHROME.toString();  // "chrome"

// Use in context switching
driver.setContext(FirefoxCommandContext.CONTENT);
driver.setContext(FirefoxCommandContext.CHROME);

Content Context Operations

The content context (default) is used for standard web page automation.

Content Context Example:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxCommandContext;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

FirefoxDriver driver = new FirefoxDriver();

// Ensure we're in content context (default)
driver.setContext(FirefoxCommandContext.CONTENT);

// Standard web page automation
driver.get("https://example.com");
WebElement element = driver.findElement(By.id("search-box"));
element.sendKeys("test query");
element.submit();

// All standard WebDriver operations work in content context
String title = driver.getTitle();
String currentUrl = driver.getCurrentUrl();

Chrome Context Operations

The chrome context allows automation of Firefox browser UI elements.

Chrome Context Example:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxCommandContext;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.JavascriptExecutor;

FirefoxDriver driver = new FirefoxDriver();

// Switch to chrome context for browser UI automation
driver.setContext(FirefoxCommandContext.CHROME);

// Access browser UI elements using XUL/XPath selectors
try {
    // Example: Access address bar (this is browser-specific and may vary)
    WebElement urlBar = driver.findElement(By.id("urlbar-input"));
    
    // Example: Access browser menus or buttons
    WebElement bookmarkButton = driver.findElement(
        By.cssSelector("toolbarbutton[data-l10n-id='navbar-bookmarks']")
    );
    
    // Execute JavaScript in chrome context (browser context)
    JavascriptExecutor js = (JavascriptExecutor) driver;
    Object result = js.executeScript(
        "return Components.classes['@mozilla.org/preferences-service;1']" +
        ".getService(Components.interfaces.nsIPrefBranch)" +
        ".getBoolPref('browser.tabs.remote.autostart');"
    );
    
    System.out.println("Remote tabs enabled: " + result);
    
} catch (Exception e) {
    System.err.println("Chrome context operation failed: " + e.getMessage());
} finally {
    // Always switch back to content context for normal operations
    driver.setContext(FirefoxCommandContext.CONTENT);
}

Context Switching Patterns

Common patterns for context management in automation scenarios.

Safe Context Switching:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxCommandContext;

public class ContextSwitchingHelper {
    private FirefoxDriver driver;
    
    public ContextSwitchingHelper(FirefoxDriver driver) {
        this.driver = driver;
    }
    
    /**
     * Executes code in chrome context, then returns to previous context.
     */
    public void executeInChromeContext(Runnable chromeOperation) {
        FirefoxCommandContext originalContext = driver.getContext();
        
        try {
            driver.setContext(FirefoxCommandContext.CHROME);
            chromeOperation.run();
        } finally {
            driver.setContext(originalContext);
        }
    }
    
    /**
     * Executes code in content context, then returns to previous context.
     */
    public void executeInContentContext(Runnable contentOperation) {
        FirefoxCommandContext originalContext = driver.getContext();
        
        try {
            driver.setContext(FirefoxCommandContext.CONTENT);
            contentOperation.run();
        } finally {
            driver.setContext(originalContext);
        }
    }
}

// Usage
FirefoxDriver driver = new FirefoxDriver();
ContextSwitchingHelper helper = new ContextSwitchingHelper(driver);

// Execute browser UI operations safely
helper.executeInChromeContext(() -> {
    // Chrome context operations
    WebElement menuButton = driver.findElement(By.id("PanelUI-menu-button"));
    menuButton.click();
});

// Context automatically restored to previous state
driver.get("https://example.com");  // Works in content context

Browser Configuration via Chrome Context:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxCommandContext;
import org.openqa.selenium.JavascriptExecutor;

FirefoxDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;

// Configure browser settings via chrome context
driver.setContext(FirefoxCommandContext.CHROME);

try {
    // Set preferences through chrome context
    js.executeScript(
        "Components.classes['@mozilla.org/preferences-service;1']" +
        ".getService(Components.interfaces.nsIPrefBranch)" +
        ".setBoolPref('dom.webnotifications.enabled', false);"
    );
    
    // Get current preference values
    Boolean notificationsEnabled = (Boolean) js.executeScript(
        "return Components.classes['@mozilla.org/preferences-service;1']" +
        ".getService(Components.interfaces.nsIPrefBranch)" +
        ".getBoolPref('dom.webnotifications.enabled');"
    );
    
    System.out.println("Notifications enabled: " + notificationsEnabled);
    
} finally {
    // Return to content context for normal web automation
    driver.setContext(FirefoxCommandContext.CONTENT);
}

// Now use the browser with modified settings
driver.get("https://example.com");

Extension Management via Chrome Context:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxCommandContext;
import org.openqa.selenium.JavascriptExecutor;

FirefoxDriver driver = new FirefoxDriver();

// Switch to chrome context for extension management
driver.setContext(FirefoxCommandContext.CHROME);

try {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    
    // Get list of installed extensions via chrome context
    Object extensions = js.executeScript(
        "let extensions = [];" +
        "for (let addon of AddonManager.getAllAddons()) {" +
        "  extensions.push({" +
        "    id: addon.id," +
        "    name: addon.name," +
        "    enabled: addon.isActive" +
        "  });" +
        "}" +
        "return extensions;"
    );
    
    System.out.println("Installed extensions: " + extensions);
    
} finally {
    driver.setContext(FirefoxCommandContext.CONTENT);
}

Complete Context Management Example:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxCommandContext;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.JavascriptExecutor;

public class AdvancedFirefoxAutomation {
    private FirefoxDriver driver;
    
    public AdvancedFirefoxAutomation() {
        this.driver = new FirefoxDriver();
    }
    
    public void demonstrateContextSwitching() {
        // Start in default content context
        System.out.println("Current context: " + driver.getContext());
        
        // 1. Perform standard web automation
        driver.get("https://example.com");
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("test search");
        
        // 2. Switch to chrome context for browser configuration
        driver.setContext(FirefoxCommandContext.CHROME);
        
        JavascriptExecutor js = (JavascriptExecutor) driver;
        
        // Configure browser via chrome context
        js.executeScript(
            "Components.classes['@mozilla.org/preferences-service;1']" +
            ".getService(Components.interfaces.nsIPrefBranch)" +
            ".setIntPref('security.tls.version.max', 4);"
        );
        
        // 3. Return to content context
        driver.setContext(FirefoxCommandContext.CONTENT);
        
        // Continue with web automation
        searchBox.submit();
        
        // Verify we're back in content context
        String title = driver.getTitle();
        System.out.println("Page title: " + title);
    }
    
    public void cleanup() {
        if (driver != null) {
            driver.quit();
        }
    }
    
    public static void main(String[] args) {
        AdvancedFirefoxAutomation automation = new AdvancedFirefoxAutomation();
        try {
            automation.demonstrateContextSwitching();
        } finally {
            automation.cleanup();
        }
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-firefox-driver

docs

browser-configuration.md

context-management.md

driver-management.md

extension-management.md

index.md

profile-management.md

screenshot-capabilities.md

service-management.md

tile.json