Recent developments in Selenium4 are mentioned in the Selenium4 Frontline Bulletin. Along with various enhancements to Selenium4 features, recent releases also include changes and enablement of some of the older apis. If you’re planning to upgrade from Selenium 3 to Selenium 4, it’s best to keep an eye out for these updates.
The apis listed in this article seem to say goodbye to all Seleniumers.
Deprecated DesiredCapabilities
In Selenium 3, we use DesiredCapabilities extensively when using RemoteWebDriver. This is a necessary step to set up the browser functionality so that the tests can run on the cloud-based Selenium Gird. But in Selenium 4, we say goodbye to DesiredCapabilities.
The Capabilities object is now replaced with Options, and we need to create an Options object to use the Driver class. Using Selenium 4, we need to set up the necessary test requirements (that is, a combination of browser and operating system) and pass the objects to the Driver constructor.
Here’s an example of a different browser.
Chrome
cloud
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.openqa.selenium.WebDriver;
import java.net.MalformedURLException;
import org.openqa.selenium.remote.RemoteWebDriver;
public void testSetUp(a) throws Exception
{
ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
options.setCapability("build"."Testing Chrome Options [Selenium 4]");
options.setCapability("name"."Testing Chrome Options [Selenium 4]");
options.setCapability("platformName"."Windows 10");
options.setCapability("browserName"."Chrome");
options.setCapability("browserVersion"."latest");
try {
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"), ((Capabilities) options));
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
}
driver.get("https://www.lambdatest.com");
}
Copy the code
local
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.openqa.selenium.WebDriver;
import java.net.MalformedURLException;
public void testSetUp(a)
{
ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
driver.get("https://www.lambdatest.com");
}
Copy the code
FirefoxDriver
cloud
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.openqa.selenium.WebDriver;
import java.net.MalformedURLException;
import org.openqa.selenium.remote.RemoteWebDriver;
public void testSetUp(a) throws Exception
{
FirefoxOptions options = new FirefoxOptions();
options.setAcceptInsecureCerts(true);
options.setCapability("build"."Testing Firefox Options [Selenium 4]");
options.setCapability("name"."Testing Firefox Options [Selenium 4]");
options.setCapability("platformName"."Windows 10");
options.setCapability("browserName"."Firefox");
options.setCapability("browserVersion"."68.0");
try {
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"), ((Capabilities) options));
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
}
driver.get("https://www.lambdatest.com");
}
Copy the code
local
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.openqa.selenium.WebDriver;
import java.net.MalformedURLException;
public void testSetUp(a)
{
FirefoxOptions options = new FirefoxOptions();
options.setAcceptInsecureCerts(true);
driver.get("https://www.lambdatest.com");
}
Copy the code
IEDriver
Omit, content basically same as above.
SafariDriver
Omit, content basically same as above.
FindsBy
The FindElement and FindElements methods implemented by the RemoteWebDriver class are used to locate individual WebElement and WebElement lists, respectively. FindsBy interface is org. Openqa. Selenium. Part of the internal package, in selenium 4 has been deprecated.
These changes are internal to the Selenium framework, and Selenium users can continue to use FindElement(By By) and FindElements(By By) used in Selenium 3.
The usage is as follows:
WebElement eid = driver.findElement(By.id("email"));
WebElement pswd = driver.findElement(By.name("password"));
WebElement sbmtBtn = driver.findElement(By.xpath("//input[@value="submit"]");
Copy the code
List<webelement> elem_signUpForm = driver.findElements(By.className("cell-body-textinput"));
List<webelement> elem_address = driver.findElements(By.name("Address"));
Copy the code
New functionality of the Actions class
The Actions class in Selenium provides multiple methods to perform individual Actions or combinations of Actions on WebElements that exist in the DOM. Operations are classified into mouse operations (such as click and double click) and keyboard operations (such as keyUp, keyDown, and sendKeys).
We demonstrate porting from Selenium 3 to Selenium 4.
Selenium in 4, the new method is added to the class Actions, it replaced the org. Openqa. Selenium. Interactions the class under the package.
As a strike
Click (WebElement) is a new method added to the Actions class. It replaces the moveToElement(onElement).click() method.
As in the alpha version of Selenium prior to Selenium 4, click(WebElement) is used to click on a Web element.
public void FunTester(a) throws InterruptedException
{
driver.navigate().to("https://www.amazon.in/");
driver.manage().window().maximize();
try {
Actions action = new Actions(driver);
WebElement elementToType = driver.findElement(By.cssSelector("#twotabsearchtextbox"));
action.sendKeys(elementToType, "iphone").build().perform();
WebElement elementToClick = driver.findElement(By.xpath("//input[@value='Go']"));
Thread.sleep(5000);
action.click(elementToClick).build().perform();
Thread.sleep(5000);
assertEquals(driver.getTitle(), "Amazon.in : iphone");
} catch(Exception e) { System.out.println(e.getMessage()); }}Copy the code
Double click and right click
In Selenium 4, the method moveToElement(Element).doubleclick () for double-clicking on WebElement is replaced with the doubleClick(WebElement) method.
The method moveToElement(onElement).ContextClick () for right-clicking has now been replaced with the contextClick(WebElement) method in Selenium 4.
Here’s an example:
public void FunTester(a) throws InterruptedException
{
driver.navigate().to("https://www.amazon.in/");
driver.manage().window().maximize();
try {
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//a[.='Mobiles']"));
action.doubleClick(element).build().perform();
Thread.sleep(5000);
assertEquals(driver.getTitle(), "Mobile Phones: Buy New Mobiles Online at Best Prices in India | Buy Cell Phones Online - Amazon.in");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
action.contextClick().build().perform();
} catch(Exception e) { System.out.println(e.getMessage()); }}Copy the code
Drag and drop
The method moveToElement(Element).clickandHold () used to click WebElement without performing the Release operation is replaced with clickAndHold(WebElement).
Used to release the mouse button release () method is org openqa. Selenium. Interactions. ButtonReleaseAction part of the class. In Selenium 4, this method is part of the Actions class.
Here’s an example:
public void test_LambdaTest_click_hold_demo(a) throws InterruptedException
{
driver.navigate().to("https://selenium08.blogspot.com/2020/01/click-and-hold.html");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
try {
Actions action = new Actions(driver);
WebElement elem_source = driver.findElement(By.xpath("//li[text()= 'C']"));
WebElement elem_destination = driver.findElement(By.xpath("//li[text()= 'A']"));
action.clickAndHold(elem_source).release(elem_destination).build().perform();
Thread.sleep(2000);
} catch(Exception e) { System.out.println(e.getMessage()); }}Copy the code
FluentWait
Selenium waits have been introduced previously: Sleep, implicit, explicit, and Fluent. FluentWait in Selenium is used to perform Selenium wait when there is uncertainty about the time it takes for an element to be visible or clickable.
As shown in the FluentWait in Selenium example (using Selenium 3), the withTimeOut() method takes two arguments — int and TimeUnit. In the following Demo, we use the withTimeOut() method as two parameters to control the total wait time.
.withTimeout(60, SECONDS)
The pollingEvery() method takes two arguments to control the frequency of polling.
.pollingEvery(2, SECONDS)
Selenium 3
Wait<webdriver> fluentWait = new FluentWait<webdriver>(driver)
.withTimeout(60, SECONDS)
.pollingEvery(2, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = fluentWait.until(new Function<webdriver, webelement=""> () {public WebElement apply(WebDriver driver)
{
return driver.findElement(By.id("foo")); }});Copy the code
Selenium 4
In Selenium 4, the withTimeout() and pollingEvery() methods that are part of the FluentWait class have been modified. The pollingEvery() method takes only one argument. Duration can be Seconds, MilliSeconds, NanoSeconds, Hours, Days, and so on. On a similar line, the withTimeOut() method also takes only one argument.
Example – FluentWait in Selenium 4
Wait<webdriver> fluentWait = new FluentWait<webdriver>(driver)
.withTimeout(Duration.ofSeconds(120))
.pollingEvery(Duration.ofMillis(2000))
.ignoring(NoSuchElementException.class);
WebElement foo = fluentWait.until(new Function<webdriver, webelement=""> () {public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo")); }});Copy the code
Have Fun ~ Tester!
FunTester“, a group of interesting souls, Tencent Cloud &Boss certified author, GDevOps official partner media.
- A preliminary study of the FunTester test framework architecture diagram
- The ultimate showdown between K6, Gatling and FunTester!
- Single-player 120,000 QPS — FunTester revenge
- FunTester’s past and present life
- Automate testing in production environment
- Tips for writing test cases
- 7 skills to Automate tests
- Iot testing
- Why do tests miss bugs
- Selenium Automation Best Practice Tips (1)
- Selenium Automation Best Practice Tips (Middle)
- Selenium Automation Best Practice Tips (Part 2)
- Asynchronous authentication of Socket interfaces
- After Selenium 4, no longer meet the API