Back to Resources

Blog

Posted October 20, 2021

Selenium 4 - New Features For Firefox

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

quote

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.

1
@Test
2
public void installAddOnWithFirefoxOnSauce() throws MalformedURLException {
3
String userName = System.getenv("SAUCE_USERNAME");
4
String accessKey = System.getenv("SAUCE_ACCESS_KEY");
5
URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
6
FirefoxOptions firefoxOptions = new FirefoxOptions();
7
firefoxOptions.setCapability("platformName", "Windows 10");
8
firefoxOptions.setCapability("browserVersion", "latest");
9
10
Map<String, Object> sauceOptions = new HashMap<>();
11
sauceOptions.put("name", "installAddOnWithFirefoxOnSauce");
12
sauceOptions.put("username", userName);
13
sauceOptions.put("accessKey", accessKey);
14
firefoxOptions.setCapability("sauce:options", sauceOptions);
15
16
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);
17
driver.setFileDetector(new LocalFileDetector());
18
WebDriver augmentedDriver = new Augmenter().augment(driver);
19
20
// Loads SauceDemo normally
21
driver.get("https://www.saucedemo.com");
22
23
// This is an extension that switches the page images for the SauceBot Ninja
24
// Extension can be found at https://git.io/JwUry
25
String id = ((HasExtensions) augmentedDriver)
26
.installExtension(Paths.get("src/test/resources/ninja_saucebot-1.0-an+fx.xpi"));
27
// Site is loaded again
28
driver.navigate().refresh();
29
// We see that the SauceBot Ninja is present
30
Assertions.assertTrue(driver.findElements(By.className("bot_column2")).size() > 0);
31
32
// Add-on is uninstalled
33
((HasExtensions) augmentedDriver).uninstallExtension(id);
34
35
driver.navigate().refresh();
36
// The SauceBot Ninja is not present anymore
37
Assertions.assertEquals(0, driver.findElements(By.className("bot_column2")).size());
38
39
driver.quit();
40
}

Installing and uninstalling add-ons in Sauce Labs:

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.

1
@Test
2
public void changeBrowserPreferencesInFirefox() throws MalformedURLException {
3
String userName = System.getenv("SAUCE_USERNAME");
4
String accessKey = System.getenv("SAUCE_ACCESS_KEY");
5
URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
6
FirefoxOptions firefoxOptions = new FirefoxOptions();
7
firefoxOptions.setCapability("platformName", "Windows 10");
8
firefoxOptions.setCapability("browserVersion", "latest");
9
firefoxOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
10
firefoxOptions.addPreference("intl.accept_languages", "de-DE");
11
12
Map<String, Object> sauceOptions = new HashMap<>();
13
sauceOptions.put("name", "changeBrowserPreferencesInFirefox");
14
sauceOptions.put("username", userName);
15
sauceOptions.put("accessKey", accessKey);
16
firefoxOptions.setCapability("sauce:options", sauceOptions);
17
18
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);
19
20
driver.get("https://www.google.com");
21
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(90));
22
23
String langDE = driver
24
.findElement(By.id("gws-output-pages-elements-homepage_additional_languages__als"))
25
.getText();
26
Assertions.assertTrue(langDE.contains("angeboten auf"));
27
28
WebDriver augmentedDriver = new Augmenter().augment(driver);
29
((HasContext) augmentedDriver).setContext(FirefoxCommandContext.CHROME);
30
31
((JavascriptExecutor) driver)
32
.executeScript("Services.prefs.setStringPref('intl.accept_languages', 'es-ES')");
33
34
((HasContext) augmentedDriver).setContext(FirefoxCommandContext.CONTENT);
35
driver.navigate().refresh();
36
37
String langES = driver
38
.findElement(By.id("gws-output-pages-elements-homepage_additional_languages__als"))
39
.getText();
40
Assertions.assertTrue(langES.contains("Ofrecido por"));
41
42
driver.quit();
43
}

Testing different languages with Firefox on Sauce Labs:

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.

1
@Test
2
public void fullPageScreenshotWithFirefox() throws IOException {
3
String userName = System.getenv("SAUCE_USERNAME");
4
String accessKey = System.getenv("SAUCE_ACCESS_KEY");
5
URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
6
FirefoxOptions firefoxOptions = new FirefoxOptions();
7
firefoxOptions.setCapability("platformName", "Windows 10");
8
firefoxOptions.setCapability("browserVersion", "latest");
9
10
Map<String, Object> sauceOptions = new HashMap<>();
11
sauceOptions.put("name", "printPageWithFirefox");
12
sauceOptions.put("username", userName);
13
sauceOptions.put("accessKey", accessKey);
14
firefoxOptions.setCapability("sauce:options", sauceOptions);
15
16
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);
17
18
driver.get("https://www.saucedemo.com/v1/inventory.html");
19
WebDriver augmentedDriver = new Augmenter().augment(driver);
20
File file = ((HasFullPageScreenshot)augmentedDriver)
21
.getFullPageScreenshotAs(OutputType.FILE);
22
Path fullPageScreenshot =
23
Paths.get("src/test/screenshots/fullPageScreenshotFirefox.png");
24
Files.move(file.toPath(), fullPageScreenshot);
25
26
driver.quit();
27
}

Titus Fortner
Sr. Developer Experience Engineer, Sauce Labs
Diego Molina
Staff Software Engineer at Sauce Labs
Published:
Oct 20, 2021
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.