Selenium 4 - New Features For Firefox

This article covers the new features in Selenium 4 that are specific to Firefox.

Selenium 4 exposes a few new features that can be used with Firefox. It is easier to install/uninstall add-ons, or change the browser preferences (such as the language) in the middle of the session, or take a full page screenshot for bug reporting. Examples showing how to do that are shown below.

Install/uninstall Add-ons

Here is a test in Java that installs a Firefox add-on. For additional examples in multiple languages, check out our Selenium 4 Documentation. This add-on, when active, swaps any image present on the website with the SauceBot Ninja. After the add-on is installed, we assert the SauceBot Ninja is present. Then, we uninstall the add-on, reload the website, and we assert the SauceBot Ninja is gone.

@Test
public void installAddOnWithFirefoxOnSauce() throws MalformedURLException {
	String userName = System.getenv("SAUCE_USERNAME");
	String accessKey = System.getenv("SAUCE_ACCESS_KEY");
	URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
	FirefoxOptions firefoxOptions = new FirefoxOptions();
	firefoxOptions.setCapability("platformName", "Windows 10");
	firefoxOptions.setCapability("browserVersion", "latest");

        Map<String, Object> sauceOptions = new HashMap<>();
	sauceOptions.put("name", "installAddOnWithFirefoxOnSauce");
	sauceOptions.put("username", userName);
	sauceOptions.put("accessKey", accessKey);
	firefoxOptions.setCapability("sauce:options", sauceOptions);

        RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);
	driver.setFileDetector(new LocalFileDetector());
	WebDriver augmentedDriver = new Augmenter().augment(driver);

        // Loads SauceDemo normally
	driver.get("https://www.saucedemo.com");

        // This is an extension that switches the page images for the SauceBot Ninja
	// Extension can be found at https://git.io/JwUry
	String id = ((HasExtensions) augmentedDriver)
       .installExtension(Paths.get("src/test/resources/ninja_saucebot-1.0-an+fx.xpi"));
	// Site is loaded again
	driver.navigate().refresh();
	// We see that the SauceBot Ninja is present
	Assertions.assertTrue(driver.findElements(By.className("bot_column2")).size() > 0);

        // Add-on is uninstalled
	((HasExtensions) augmentedDriver).uninstallExtension(id);

        driver.navigate().refresh();
	// The SauceBot Ninja is not present anymore
	Assertions.assertEquals(0, driver.findElements(By.className("bot_column2")).size());

        driver.quit();
}

Installing and uninstalling add-ons in Sauce Labs:

Asset > firefox add-ons

Updating Firefox Browser Preferences

Changing Firefox browser preferences, like testing your website with different languages, is now easier with Selenium 4. This example shows you how you can update the browser language using changeBrowserPreferencesInFirefox, even after the session has been created. Here is an example in Java; for additional examples in multiple languages, check out our Selenium 4 Documentation.

@Test
public void changeBrowserPreferencesInFirefox() throws MalformedURLException {
	String userName = System.getenv("SAUCE_USERNAME");
	String accessKey = System.getenv("SAUCE_ACCESS_KEY");
	URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
	FirefoxOptions firefoxOptions = new FirefoxOptions();
	firefoxOptions.setCapability("platformName", "Windows 10");
	firefoxOptions.setCapability("browserVersion", "latest");
	firefoxOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
	firefoxOptions.addPreference("intl.accept_languages", "de-DE");

        Map<String, Object> sauceOptions = new HashMap<>();
	sauceOptions.put("name", "changeBrowserPreferencesInFirefox");
	sauceOptions.put("username", userName);
	sauceOptions.put("accessKey", accessKey);
	firefoxOptions.setCapability("sauce:options", sauceOptions);

        RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);

        driver.get("https://www.google.com");
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(90));

        String langDE = driver
  	.findElement(By.id("gws-output-pages-elements-homepage_additional_languages__als"))
  	.getText();
	Assertions.assertTrue(langDE.contains("angeboten auf"));

        WebDriver augmentedDriver = new Augmenter().augment(driver);
        ((HasContext) augmentedDriver).setContext(FirefoxCommandContext.CHROME);

        ((JavascriptExecutor) driver)
        .executeScript("Services.prefs.setStringPref('intl.accept_languages', 'es-ES')");

        ((HasContext) augmentedDriver).setContext(FirefoxCommandContext.CONTENT);
	driver.navigate().refresh();

        String langES = driver
  	.findElement(By.id("gws-output-pages-elements-homepage_additional_languages__als"))
  	.getText();
	Assertions.assertTrue(langES.contains("Ofrecido por"));

        driver.quit();
}

Testing different languages with Firefox on Sauce Labs:

Asset > changeBrowserPreferencesInFirefox

Full Page Screenshot

Screenshots with WebDriver capture the viewport. It is possible to capture the whole page now with Firefox, which could be useful to add full page screenshots to bug reports, for example.  Here is an example in Java; for additional examples in multiple languages, check out our Selenium 4 Documentation.

@Test
public void fullPageScreenshotWithFirefox() throws IOException {
	String userName = System.getenv("SAUCE_USERNAME");
	String accessKey = System.getenv("SAUCE_ACCESS_KEY");
	URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
	FirefoxOptions firefoxOptions = new FirefoxOptions();
	firefoxOptions.setCapability("platformName", "Windows 10");
	firefoxOptions.setCapability("browserVersion", "latest");

        Map<String, Object> sauceOptions = new HashMap<>();
	sauceOptions.put("name", "printPageWithFirefox");
	sauceOptions.put("username", userName);
	sauceOptions.put("accessKey", accessKey);
	firefoxOptions.setCapability("sauce:options", sauceOptions);

        RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);

        driver.get("https://www.saucedemo.com/v1/inventory.html");
	WebDriver augmentedDriver = new Augmenter().augment(driver);
	File file = ((HasFullPageScreenshot)augmentedDriver)
       .getFullPageScreenshotAs(OutputType.FILE);
	Path fullPageScreenshot =
        Paths.get("src/test/screenshots/fullPageScreenshotFirefox.png");
	Files.move(file.toPath(), fullPageScreenshot);

        driver.quit();
}

Check out our comprehensive guide to Selenium 4 for more information.


Written by

Diego Molina and Titus Fortner


Topics

SeleniumAutomated testing