Skip to main content

Posts

Showing posts with the label Selenium4

Selenium 4.6.0 released with Selenium Manager

Implementation of Selenium Manager across bindings is one of the key features of the Selenium 4.6.0 release. The Selenium project wants to simplify how we all set up our environment. Setting up browser drivers has been for many years a task that we need to perform all the time.  To run a Selenium test with Selenium 4.6.0, we only need to have Chrome, Firefox, or Edge installed. If you already have browser drivers installed, this feature will be ignored.  Just add 4.6.0 Selenium dependency: <dependency>     <groupId>org.seleniumhq.selenium</groupId>     <artifactId>selenium-java</artifactId>     <version>4.6.0</version> </dependency> And as is use: WebDriver driver = new ChromeDriver(); Also, future releases of Selenium Manager will eventually even download browsers if necessary. :) Enjoyed reading this article? Please share the knowledge with your friends and colleagues.

Shadow DOM in Selenium 4

To access Shadow DOM elements in Selenium 4 with Chromium browsers (Microsoft Edge and Google Chrome) version 96 or greater, we can use the shadow root method: WebElement  shadowIdContent  = driver.findElement(By.cssSelector("#shadow_host")) .getShadowRoot() .findElement(By.cssSelector("#shadow_content")); What happened in v96 is that Chromium has made its shadow root values compliant with the updated W3C WebDriver specification, which now includes definitions for getting an element’s shadow root and locating elements in a shadow root.   I found this excellent video for the same : https://youtu.be/-uMLqBO2x7c Enjoyed reading this article? Please share the knowledge with your friends and colleagues.

Mock GeoLocation using Selenium 4

If there is a use case where you need to override the geolocation of the browser before you hit the website, Selenium 4 Alpha version does support that now by using the power of Chrome DevTools and Protocol (CDP) along with setGeolocationOverride method present in Emulation class. To set it back to default, use the clearGeolocationOverride method. #mockGeoLocation #testAutomation #selenium4Alpha @Test private void testGeo() throws InterruptedException { driver .get( "https://www.google.com/maps?q=28.704060,77.102493" ); Builder<String, Object> mapLatLan = new ImmutableMap.Builder<String, Object>(); mapLatLan .put( "latitude" , 40.712776); mapLatLan .put( "longitude" , -74.005974); mapLatLan .put( "accuracy" , 100); ((ChromeDriver) driver ).executeCdpCommand( "Emulation.setGeolocationOverride" , mapLatLan .build()); driver .get( "https://www.google.com/maps?q=40.712776,-74.005974&q

Installing and Uninstalling Add-ons in Firefox Browser

Almost a month back, we discussed how we can install an extension in Selenium 3: https://www.linkedin.com/posts/dheerajgambhir_seleniumautomation-chromeext-automationtesting-activity-6587948591678095361-Lr7m Or https://testersdigest.blogspot.com/2019/10/need-extension-while-running-your-se.html And now in Selenium 4, we have direct method installExtension and uninstallExtension for the firefox Driver. The installExtension installs a new addon with the current session which inturn will return an ID that may later be used to uninstallExtension the addon using uninstallAddon.

Selenium 4 Grid

Selenium 4 provides three kinds of Grid- Standalone, Traditional one (Hub, and Node), Fully Distributed (Process and Sessions) Give a shot to Standalone mode where by default, the server will detect available drivers that it can use (Chome, GeckoDriver, etc) by looking at the PATH. Make sure you place all the executable into your PATH. Here's the official link from Se where you can find all the details: https://github.com/SeleniumHQ/selenium/wiki/Selenium-Grid-4 hashtag # selenium4 hashtag # testautomation hashtag # automationtesting hashtag # seleniumgrid

Selenium 4: Chrome DevTools API

In Selenium 4, there is an Interface for Chrome DevTools API that lets you play with DevTools. Here is a list of few things that we can achieve by using DevTools API with Selenium: 1. Use Console capabilities 2. Emulate network conditions 3. Perform security operations 4. Get performance and Metrics of our Browser/Network The complete API can be found here: https://lnkd.in/f7ffwZq Showing below how we can listen to Chrome Console logs and close the browser using the "Devtools" interface. public static void chromeDevTools() throws InterruptedException, IOException { try { chromeDevTools = driver.getDevTools(); chromeDevTools.createSession(); message = "Hi everyone, this is Dheeraj."; driver.get(" https://lnkd.in/fM5JmnZ "); // execute Script to write console message driver.executeScript("console.log('" + message + "');"); } catch (Exception e) { e.printStackTrace(); } finally { // C

Full page screenshot- Selenium 4 (Firefox for now)

Full page screenshot— Using Selenium 4 (alpha 2 or greater), now we can take the full page screenshots with getFullPageScreenshotAs() method in the Firefox. But instead of typecasting it to ‘TakesScreenshot’ interface, we need to typecast it to FirefoxDriver instance. File src = ((FirefoxDriver) driver).getFullPageScreenshotAs(OutputType.FILE); I checked but it looks like this method is not available for Chrome or any other browsers yet. hashtag # seleniumautomation hashtag # automationtesting hashtag # tipsandtricks hashtag # firefox hashtag # screenshot hashtag # selenium4

Selenium: Full Screen Vs Maximize

Full Screen Vs Maximize:: Do you know that in a Selenium we have a fullscreen() method that is different from maximize()? When maximized (driver.manage().window().maximize()), the title bar etc. of the window is still displayed. In fullscreen mode (driver.manage().window().fullscreen()), the title bar is not displayed. Until Selenium 3.x, there were several bug reported that fullscreen() feature doesn’t work mostly (not always) when using ChromeDriver and it throws org.openqa.selenium.UnsupportedCommandException: unknown command. We have a workaround to use ChromeOptions options.addArguments("start-fullscreen") in that case where you get "org.openqa.selenium.UnsupportedCommandException: unknown command" error on using fullscreen(). But in Selenium 4 (alpha 3), fullscreen() works like charm without any issue. hashtag # seleniumautomation hashtag # automationtesting hashtag # Selenium4

Selenium 4 - Switch to a new window or a new tab

If you have a use case where after navigating to a site you need to open another tab or a window and then navigate to some site on the new tab/window to perform some action there- then you can use these commands from Selenium 4: driver.switchTo().newWindow(WindowType.TAB); driver.switchTo().newWindow(WindowType.WINDOW); Tried and tested- works fine. hashtag # seleniumautomation hashtag # selenium4 hashtag # automationtesting

Screenshot of a specific element:: Selenium 3.x Vs Selenium 4

getScreenshotAs for WebElement was not supported until Selenium 3.x, so we had to take a long route like taking a complete viewable area screenshot and then cropping the part where we have our element by using getLocation, getSize, getWidth, getHeight and then finally getSubimage method like shown in image 1. But from Selenium 4 (at least alpha 3) onwards, getScreenshotAs is also supported for WebElement like this: public static void eleScreenshot() throws InterruptedException, IOException { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(" https://in.linkedin.com/ "); if(driver instanceof ChromeDriver) { File src = driver.findElement(By.xpath("/html/body/nav/a[1]/li-icon")).getScreenshotAs(OutputType.FILE); FileHandler.copy(src, new File("D:\\Learning_Automation\\Practise\\Image1.png")); } } Note* I u