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.
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@Test2public void installAddOnWithFirefoxOnSauce() throws MalformedURLException {3String userName = System.getenv("SAUCE_USERNAME");4String accessKey = System.getenv("SAUCE_ACCESS_KEY");5URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");6FirefoxOptions firefoxOptions = new FirefoxOptions();7firefoxOptions.setCapability("platformName", "Windows 10");8firefoxOptions.setCapability("browserVersion", "latest");910Map<String, Object> sauceOptions = new HashMap<>();11sauceOptions.put("name", "installAddOnWithFirefoxOnSauce");12sauceOptions.put("username", userName);13sauceOptions.put("accessKey", accessKey);14firefoxOptions.setCapability("sauce:options", sauceOptions);1516RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);17driver.setFileDetector(new LocalFileDetector());18WebDriver augmentedDriver = new Augmenter().augment(driver);1920// Loads SauceDemo normally21driver.get("https://www.saucedemo.com");2223// This is an extension that switches the page images for the SauceBot Ninja24// Extension can be found at https://git.io/JwUry25String id = ((HasExtensions) augmentedDriver)26.installExtension(Paths.get("src/test/resources/ninja_saucebot-1.0-an+fx.xpi"));27// Site is loaded again28driver.navigate().refresh();29// We see that the SauceBot Ninja is present30Assertions.assertTrue(driver.findElements(By.className("bot_column2")).size() > 0);3132// Add-on is uninstalled33((HasExtensions) augmentedDriver).uninstallExtension(id);3435driver.navigate().refresh();36// The SauceBot Ninja is not present anymore37Assertions.assertEquals(0, driver.findElements(By.className("bot_column2")).size());3839driver.quit();40}
Installing and uninstalling add-ons in Sauce Labs:
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@Test2public void changeBrowserPreferencesInFirefox() throws MalformedURLException {3String userName = System.getenv("SAUCE_USERNAME");4String accessKey = System.getenv("SAUCE_ACCESS_KEY");5URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");6FirefoxOptions firefoxOptions = new FirefoxOptions();7firefoxOptions.setCapability("platformName", "Windows 10");8firefoxOptions.setCapability("browserVersion", "latest");9firefoxOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);10firefoxOptions.addPreference("intl.accept_languages", "de-DE");1112Map<String, Object> sauceOptions = new HashMap<>();13sauceOptions.put("name", "changeBrowserPreferencesInFirefox");14sauceOptions.put("username", userName);15sauceOptions.put("accessKey", accessKey);16firefoxOptions.setCapability("sauce:options", sauceOptions);1718RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);1920driver.get("https://www.google.com");21driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(90));2223String langDE = driver24.findElement(By.id("gws-output-pages-elements-homepage_additional_languages__als"))25.getText();26Assertions.assertTrue(langDE.contains("angeboten auf"));2728WebDriver augmentedDriver = new Augmenter().augment(driver);29((HasContext) augmentedDriver).setContext(FirefoxCommandContext.CHROME);3031((JavascriptExecutor) driver)32.executeScript("Services.prefs.setStringPref('intl.accept_languages', 'es-ES')");3334((HasContext) augmentedDriver).setContext(FirefoxCommandContext.CONTENT);35driver.navigate().refresh();3637String langES = driver38.findElement(By.id("gws-output-pages-elements-homepage_additional_languages__als"))39.getText();40Assertions.assertTrue(langES.contains("Ofrecido por"));4142driver.quit();43}
Testing different languages with Firefox on Sauce Labs:
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@Test2public void fullPageScreenshotWithFirefox() throws IOException {3String userName = System.getenv("SAUCE_USERNAME");4String accessKey = System.getenv("SAUCE_ACCESS_KEY");5URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");6FirefoxOptions firefoxOptions = new FirefoxOptions();7firefoxOptions.setCapability("platformName", "Windows 10");8firefoxOptions.setCapability("browserVersion", "latest");910Map<String, Object> sauceOptions = new HashMap<>();11sauceOptions.put("name", "printPageWithFirefox");12sauceOptions.put("username", userName);13sauceOptions.put("accessKey", accessKey);14firefoxOptions.setCapability("sauce:options", sauceOptions);1516RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);1718driver.get("https://www.saucedemo.com/v1/inventory.html");19WebDriver augmentedDriver = new Augmenter().augment(driver);20File file = ((HasFullPageScreenshot)augmentedDriver)21.getFullPageScreenshotAs(OutputType.FILE);22Path fullPageScreenshot =23Paths.get("src/test/screenshots/fullPageScreenshotFirefox.png");24Files.move(file.toPath(), fullPageScreenshot);2526driver.quit();27}