Selenium 4 New features with Practical Examples #2
Selenium is the most popularly used freeware and open source automation tool. The benefits of Selenium for Test Automation are immense. Importantly, it enables record and playback for testing web applications and can run multiple scripts across various browsers.
Recently selenium4 launched with new features.
- Capture screenshot of specific web element
- Open the new tab on the browser
- Open a new window on the browser
- Object Location
- Relative Locators
- Chrome Dev tools
- Capture screenshot of specific web element
Till Selenium 3 we have an option that to take the screenshot for an entire webpage. From selenium 4 onwards they have introduced an option that to capture a web element
Code Explanation:
- Webdrivermanager is used, it will help to download browser binaries/executables in an automated way, we have used chrome driver to launch chrome browser
- Launch the browser and navigate to -> https://www.google.com
- Write XPath for the ‘google logo ’ and assig to a web element
- Pass web element reference variable to getScreenshotas method
- Pass the image name in fileUtils
After running the code successfully, it will generate an image with the given name in the project folder structure. Please find the below image
Code:-import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.openqa.selenium.By;import org.openqa.selenium.OutputType;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import io.github.bonigarcia.wdm.WebDriverManager;public class ScreenshotTest {public static void main(String[] args) {WebDriverManager.chromedriver().setup();WebDriver driver = new ChromeDriver();driver.get("https://www.google.com");WebElement logo = driver.findElement(By.xpath("//img[@alt='Google']"));File file = logo.getScreenshotAs(OutputType.FILE);try {FileUtils.copyFile(file, new File("googlelogo.png"));} catch (IOException e) {e.printStackTrace();}driver.quit();}}
2. Open the new tab on the browser
Before selenium 4 we used to use robot class /javascript or some other library to create a new tab or window and then switch to that new window to perform browser actions
But with Selenium 4, with one line of code, we can create a new tab or window and switch to that window and perform any operation like loading a URL
Code Explanation:
- Webdrivermanager is used, it will help to download browser binaries/executables in an automated way, we have used chrome driver to launch chrome browser
- Launch the browser and navigate to -> https://www.google.com
- With the below-mentioned code, A new tab is opened and switched to it
driver.switchTo().newWindow(WindowType.TAB);
4.facebook.com will open in new tab of the same browser
Code:import org.openqa.selenium.WebDriver;import org.openqa.selenium.WindowType;import org.openqa.selenium.chrome.ChromeDriver;import io.github.bonigarcia.wdm.WebDriverManager;public class feature_NewTab {public static void main(String[] args) {WebDriverManager.chromedriver().setup();WebDriver driver = new ChromeDriver();driver.get("https://www.google.com");//To open new tabdriver.switchTo().newWindow(WindowType.TAB);driver.navigate().to("https://www.fb.com/");}}
3. Open a new window on the browser
Before selenium 4 we used to use robot class /javascript or some other library to create a new tab or window and then switch to that new window to perform browser actions
But with Selenium 4, with one line of code, we can create a new tab or window and switch to that window and perform any operation like loading a URL
Code Explanation:
- Webdrivermanager is used, it will help to download browser binaries/executables in an automated way, we have used chrome driver to launch chrome browser
- Launch the browser and navigate to -> https://www.google.com
- With the below-mentioned code, A new window is opened and switched to it
driver.switchTo().newWindow(WindowType.WINDOW);
4.facebook.com will open in new window
Code:import org.openqa.selenium.WebDriver;import org.openqa.selenium.WindowType;import org.openqa.selenium.chrome.ChromeDriver;import io.github.bonigarcia.wdm.WebDriverManager;public class feature_NewWindow {public static void main(String[] args) {WebDriverManager.chromedriver().setup();WebDriver driver = new ChromeDriver();driver.get("https://www.google.com");//To open new windowdriver.switchTo().newWindow(WindowType.WINDOW);driver.navigate().to("https://www.fb.com/");}}
4. Object Location
Now with Selenium 4, users can achieve the coordinates, dimension, height, width, etc. as the location of the web elements or object.
Code Explanation:
- Webdrivermanager is used, it will help to download browser binaries/executables in an automated way, we have used chrome driver to launch chrome browser
- Launch the browser and navigate to -> https://www.google.com
- Write XPath for the ‘google logo ’ and assig to a webelement
- Using above mentioned methods able to the height, width,x, and Y location of the google logo.
In the below console output able to get the height, width,x, and Y location of the google logo.
5. Relative Locators
This topic I have explained in my preview blog, Please find the below
Happy coding !!