Selenium 4 - New Features For Firefox
Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from intent to execution in minutes.
|
x
Blog
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 coverage. Examples demo 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 instance in multiple languages, check out our. This add-on, when active, swaps any image present on the site with the SauceBot Ninja. After the add-on is establish, we assert the SauceBot Ninja is present. Then, we uninstall the add-on, reload the site, and we affirm the SauceBot Ninja is gone.
1@Test2public void installAddOnWithFirefoxOnSauce () throws MalformedURLException {3String userName = System.getenv (& quot; SAUCE_USERNAME & quot;);4String accessKey = System.getenv (& quot; SAUCE_ACCESS_KEY & quot;);5URL gridUrl = new URL (& quot; https: //ondemand.us-west-1.saucelabs.com:443/wd/hub & quot;);6FirefoxOptions firefoxOptions = new FirefoxOptions ();7firefoxOptions.setCapability (& quot; platformName & quot;, & quot; Windows 10 & quot;);8firefoxOptions.setCapability (& quot; browserVersion & quot;, & quot; latest & quot;);910Map & lt; String, Object & gt; sauceOptions = new HashMap & lt; & gt; ();11sauceOptions.put (& quot; name & quot;, & quot; installAddOnWithFirefoxOnSauce & quot;);12sauceOptions.put (& quot; username & quot;, userName);13sauceOptions.put (& quot; accessKey & quot;, accessKey);14firefoxOptions.setCapability (& quot; sauce: options & quot;, sauceOptions);1516RemoteWebDriver driver = new RemoteWebDriver (gridUrl, firefoxOptions);17driver.setFileDetector (new LocalFileDetector ());18WebDriver augmentedDriver = new Augmenter () .augment (driver);1920// Loads SauceDemo unremarkably21driver.get (& quot; https: //www.saucedemo.com & quot;);2223// This is an propagation 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 (& quot; src/test/resources/ninja_saucebot-1.0-an+fx.xpi & quot;));27// Site is loaded again28driver.navigate () .refresh ();29// We see that the SauceBot Ninja is present30Assertions.assertTrue (driver.findElements (By.className (& quot; bot_column2 & quot;)) .size () & gt; 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 (& quot; bot_column2 & quot;)) .size ());3839driver.quit ();40}
Installing and uninstalling add-ons in Sauce Labs:

Updating Firefox Browser Preferences
Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.
Changing Firefox browser druthers, like prove your website with different languages, is now easier with Selenium 4. This example establish you how you can update the browser words using changeBrowserPreferencesInFirefox, yet after the session has been created. Here is an example in Java; for additional representative in multiple languages, check out our.
1@Test2publicvoidchangeBrowserPreferencesInFirefox() throws MalformedURLException{3String userName =System.getenv(& quot; SAUCE_USERNAME & quot;);4String accessKey =System.getenv(& quot; SAUCE_ACCESS_KEY & quot;);5URL gridUrl =newURL(& quot; https: //ondemand.us-west-1.saucelabs.com:443/wd/hub & quot;);6FirefoxOptionsfirefoxOptions=newFirefoxOptions();7firefoxOptions.setCapability(& quot; platformName & quot;,& quot; Windows 10 & quot;);8firefoxOptions.setCapability(& quot; browserVersion & quot;,& quot; latest & quot;);9firefoxOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);10firefoxOptions.addPreference(& quot; intl.accept_languages & quot;,& quot; de-DE & quot;);1112Map<String,Object>sauceOptions=newHashMap<>();13sauceOptions.put(& quot; name & quot;,& quot; changeBrowserPreferencesInFirefox & quot;);14sauceOptions.put(& quot; username & quot;, userName);15sauceOptions.put(& quot; accessKey & quot;, accessKey);16firefoxOptions.setCapability(& quot; sauce: options & quot;,sauceOptions);1718RemoteWebDriver driver =newRemoteWebDriver(gridUrl,firefoxOptions);1920driver.get(& quot; https: //www.google.com & quot;);21driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(90));2223String langDE = driver24.findElement(By.id(& quot; gws-output-pages-elements-homepage_additional_languages__als & quot;))25.getText();26Assertions.assertTrue(langDE.contains(& quot; angeboten auf & quot;));2728WebDriveraugmentedDriver=newAugmenter().augment(driver);29((HasContext)augmentedDriver).setContext(FirefoxCommandContext.CHROME);3031((JavascriptExecutor) driver)32.executeScript(& quot; Services.prefs.setStringPref (& # x27; intl.accept_languages & # x27;, & # x27; es-ES & # x27;) & quot;);3334((HasContext)augmentedDriver).setContext(FirefoxCommandContext.CONTENT);35driver.navigate().refresh();3637String langES = driver38.findElement(By.id(& quot; gws-output-pages-elements-homepage_additional_languages__als & quot;))39.getText();40Assertions.assertTrue(langES.contains(& quot; Ofrecido por & quot;));4142driver.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 utilitarian to add full page screenshots to bug reports, for example. Here is an example in Java; for additional examples in multiple lyric, check out our.
1@Test2public void fullPageScreenshotWithFirefox () throw IOException {3String userName = System.getenv (& quot; SAUCE_USERNAME & quot;);4String accessKey = System.getenv (& quot; SAUCE_ACCESS_KEY & quot;);5URL gridUrl = new URL (& quot; https: //ondemand.us-west-1.saucelabs.com:443/wd/hub & quot;);6FirefoxOptions firefoxOptions = new FirefoxOptions ();7firefoxOptions.setCapability (& quot; platformName & quot;, & quot; Windows 10 & quot;);8firefoxOptions.setCapability (& quot; browserVersion & quot;, & quot; latest & quot;);910Map & lt; String, Object & gt; sauceOptions = new HashMap & lt; & gt; ();11sauceOptions.put (& quot; name & quot;, & quot; printPageWithFirefox & quot;);12sauceOptions.put (& quot; username & quot;, userName);13sauceOptions.put (& quot; accessKey & quot;, accessKey);14firefoxOptions.setCapability (& quot; sauce: alternative & quot;, sauceOptions);1516RemoteWebDriver driver = new RemoteWebDriver (gridUrl, firefoxOptions);1718driver.get (& quot; https: //www.saucedemo.com/v1/inventory.html & quot;);19WebDriver augmentedDriver = new Augmenter () .augment (driver);20File file = ((HasFullPageScreenshot) augmentedDriver)21.getFullPageScreenshotAs (OutputType.FILE);22Path fullPageScreenshot =23Paths.get (& quot; src/test/screenshots/fullPageScreenshotFirefox.png & quot;);24Files.move (file.toPath (), fullPageScreenshot);2526driver.quit ();27}
Sr. Developer Experience Engineer, Sauce Labs
Staff Software Engineer at Sauce Labs
Share this post
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free
