Upgrade to Selenium 4
Selenium 4 uses the W3C WebDriver standard. This mainly affects capabilities and the actions class. The Selenium 4 documentation at www.selenium.dev has all the details.
Upgrading to Selenium 4 from an earlier version of Selenium requires changes to the dependencies in your test project's pom.xml file and to your test scripts. This article provides detailed instructions, including some Perfecto-specific changes.
We suggest that you start by watching the video, which explains how to use Selenium 4 for desktop web testing, and then read through the procedures for step-by-step instructions.
Prerequisites
For desktop web testing, make sure you work with:
-
Selenium 4.0.0 or later
For testing on real mobile devices, make sure you work with:
-
Selenium 4.4.0 or later
-
Appium Java client 8.1.1 or later
Upgrade dependencies
The pom.xml file handles all of your project's dependencies. This is where you need to update the Selenium version.
To update the pom.xml file:
-
Open your project's pom.xml file.
-
For the following dependencies, change the Selenium version to 4.x.x. For example:
Copy<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0></version
<dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>4.0.0></version
<dependency> -
(Optional) Change the version of the Appium Java client to 8.x. For example:
Copy<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>8.1.0></version
<dependency> -
Save your file.
Update the test scripts
In Selenium 4, DesiredCapabilities and capabilities.setCapability have changed. You need to update your test scripts accordingly. The changes slightly differ for each browser, operating system version, or mobile device.
To view the steps, expand the relevant section.
-
In your Windows test scripts:
-
Change
Desired.Capabilitiesdepending on the browser.Browser
Old notation
New notation
Chrome
CopyDesiredCapabilities capabilities = new DesiredCapabilities();CopyChromeOptions browserOptions = new ChromeOptions();Firefox
CopyFirefoxOptions browserOptions = new FirefoxOptions();Edge
CopyEdgeOptions browserOptions = new EdgeOptions(); -
Add
browserOptions. There is a 1:1 ratio betweencapabilities.setCapabilityandbrowserOptions.set<option-name>. The following table provides an example.Old notation
New notation
Copycapabilities.setCapability("platformName", "Windows");
capabilities.setCapability("browserName", "Chrome");CopybrowserOptions.setPlatformName("Windows");
browserOptions.setBrowserVersion("latest"); -
For capabilities that are not included in this Selenium-specified list, add the vendor prefix
perfectoOptions. Any Perfecto-built function for browser- or cloud-specific parameters requires this notation.The following table provides an example.
Old notation
New notation
Copycapabilities.setCapability("platformVersion", "11");
capabilities.setCapability("resolution", "1920x1080");
capabilities.setCapability("securityToken", "<your-token>");
capabilities.setCapability("scriptName", "Windows-Chrome-Webtest");CopyperfectoOptions.put("platformVersion", "11");
perfectoOptions.put("resolution", "1920x1080");
perfectoOptions.put("securityToken", "<your-token");
perfectoOptions.put("scriptName", "Windows-Chrome-Webtest-S4");
browserOptions.setCapability("perfecto:options", perfectoOptions); -
For Egde, you also need to specify the browser name, as shown in the following code snippet. This is necessary because unlike the other capabilities,
EdgeOptionstries to pass thebrowserNameasMicrosoftEdge, which is not supported in Perfecto. Perfecto expects the valueEdge. By adding this additional line, you override the default value passed on by Selenium.CopyperfectoOptions.put("browserName", "Edge");
-
-
In your MacOS test scripts:
-
Change
Desired.Capabilitiesfor Safari.Old notation
New notation
CopyDesiredCapabilities capabilities = new DesiredCapabilities();CopySafariOptions browserOptions = new SafariOptions(); -
Add
browserOptions. There is a 1:1 ratio betweencapabilities.setCapabilityandbrowserOptions.set<option-name>. The following table provides an example.Old notation
New notation
Copycapabilities.setCapability("platformName", "MAC");
capabilities.setCapability("browserName", "Safari");CopybrowserOptions.setPlatformName("MAC");
browserOptions.setBrowserVersion("15"); -
For capabilities that are not included in this Selenium-specified list, add the vendor prefix
perfectoOptions. Any Perfecto-built function for browser- or cloud-specific parameters requires this notation.The following table provides an example.
Old notation
New notation
Copycapabilities.setCapability("platformVersion", "macOS Monterey");
capabilities.setCapability("resolution", "1440x900");
capabilities.setCapability("securityToken", "<your-token>");
capabilities.setCapability("scriptName", "MAC-Monterey-Webtest");CopyperfectoOptions.put("platformVersion", "macOS Monterey");
perfectoOptions.put("resolution", "1440x900");
perfectoOptions.put("securityToken", "<your-token");
perfectoOptions.put("scriptName", "MAC-Monterey-Webtest-S4");
browserOptions.setCapability("perfecto:options", perfectoOptions); -
For Safari, you also need to specify the browser name, as shown in the following code snippet. Without this declaration, you may get an 'Invalid capability" message.
CopyperfectoOptions.put("browserName", "Safari");
-
-
In your iOS test scripts:
-
Change
Desired.Capabilitiesfor XCUITest.Old notation
New notation
CopyDesiredCapabilities capabilities = new DesiredCapabilities();CopyXCUITESTOptions xcuiTestOptions = new XCUITestOptions(); -
Add
xcuiTestOptions. There is a 1:1 ratio betweencapabilities.setCapabilityandxcuiTestOptions.setCapability<option-name>. The following table provides an example.Old notation
New notation
Copycapabilities.setCapability("platformName", "ios");CopyxcuiTestOptions.setCapability("platformName", "ios"); -
For capabilities that are not included in this Selenium-specified list, add the vendor prefix
perfectoOptions. Any Perfecto-built function for device- or cloud-specific parameters requires this notation.The following table provides an example.
Old notation
New notation
Copycapabilities.setCapability("securityToken", "<your-token>");
capabilities.setCapability("deviceName", "<device-name>");
capabilities.setCapability("bundleId", "<bundleID>");
capabilities.setCapability("appiumVersions", "2.0.0");
capabilities.setCapability("automationVersion", "3.52.0");CopyMap<String, Object> perfectoOptions = new HashMap<>();
perfectoOptions.put("securityToken", SECURITYTOKEN);
perfectoOptions.put("deviceName", "DEVICENAME");
//perfectoOptions.put("bundleId", "");
perfectoOptions.put("appiumVersion", "2.0.0");
perfectoOptions.put("automationVersion", "3.52.0");
xcuiTestOptions.setCapability("perfecto:options", perfectoOptions); -
Initialize the driver as follows.
Old notation
New notation
CopyIOSDriver driver = new IOSDriver<>(new URL("<cloud-url"), capabilities);Copydriver = new IOSDriver(<cloud-url>, xcuiTestOptions);
-
-
In your Android test scripts:
-
Change
Desired.Capabilitiesfor UiAutomator2.Old notation
New notation
CopyDesiredCapabilities capabilities = new DesiredCapabilities();CopyUiAutomator2Options uiAutomator2Options = new UiAutomator2Options(); -
Add
uiAutomator2Options. There is a 1:1 ratio betweencapabilities.setCapabilityanduiAutomator2Options.setCapability<option-name>. The following table provides an example.Old notation
New notation
Copycapabilities.setCapability("platformName", "android");CopyuiAutomator2Options.setCapability("platformName", "android"); -
For capabilities that are not included in this Selenium-specified list, add the vendor prefix
perfectoOptions. Any Perfecto-built function for device- or cloud-specific parameters requires this notation.The following table provides an example.
Old notation
New notation
Copycapabilities.setCapability("securityToken", "<your-token>");
capabilities.setCapability("deviceName", "<device-name>");
capabilities.setCapability("appPackage", "<appPackage>");
capabilities.setCapability("appActivity", "<app-activity>");
capabilities.setCapability("appiumVersions", "2.0.0");
capabilities.setCapability("automationVersion", "3.52.0");CopyperfectoOptions.put("securityToken", SECURITYTOKEN);
perfectoOptions.put("deviceName", "DEVICENAME");
//perfectoOptions.put("appPackage", "");
//perfectoOptions.put("appActivity", "");
perfectoOptions.put("appiumVersion", "2.0.0");
perfectoOptions.put("automationVersion", "3.52.0");
uiAutomator2Options.setCapability("perfecto:options", perfectoOptions); -
Initialize the driver as follows.
Old notation
New notation
Copydriver = new AndroidDriver<>(new URL("<cloud-url"), capabilities);Copydriver = new AndroidDriver(url, uiAutomator2Options);
-
Identify Selenium 4 in reports
You can identify a test's execution framework by using the Details option inside the single test report (STR).
To identify the execution framework in a report:
-
In the Report Library, click the report in the table to open the STR.
-
In the STR, click the Report Details button
. -
In the Report Details dialog box, on the Device tab, click Show Desired Capabilities.
-
Scroll down to verify the seleniumVersion field. It should display a value of 4.0.0.