Skip to main content

Posts

Selenium- PageFactory not required @FindBy for ID and NAME

Did you know that if the element variable name is the same with the element id or element name, it's not mandatory to use FindBy annotation as these are known as Direct Locators? Like this: public class FindByExample { private WebDriver driver; @FindBy(id = "username") //here the text box has the locator id=username. private WebElement uname; @FindBy(name = "password") //here the text box has the locator name=password. private WebElement pwd; public FindByExample(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); } The above code can be written with NO FindBy annotations if the web element variables have the same name or ID: public class FindByExample { private WebDriver driver; private WebElement username; private WebElement password; public FindByExample(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); } hashta

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

Disabling images - Selenium

One of the ways to increase the speed and performance of your Selenium test is to execute in the headless mode. Also, as we know that disabling images helps to speed up the page load times which make execution faster- so sometimes during run (though NOT recommended for regular test run OR in the case where you flow requires interaction with images like clicking on the button which is an image), if we are not bothered too much whether images are loading on the page and want our execution to be faster- we can disable the images. public static void disableChromeImages(ChromeOptions options) { HashMap images = new HashMap(); images.put("images", 2); // 0-default, 1-Allow, 2-Block HashMap prefs = new HashMap(); prefs.put("profile.default_content_setting_values", images); options.setExperimentalOption("prefs", prefs); } public static void disableFireFoxImages(FirefoxOptions options) { FirefoxProfile profile = new FirefoxProfile(); profi

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