Screenshot screenshot=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver) Import ru.ShootingStrategies ĭriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)
Once the jars are added into the project, all you need to do is mention the below lines of code when you intend to take the full-screen screenshot: In order to take a screenshot of the entire screen, you need to add the jar into your project. Helps capture entire screen and web elementįor more details on the utility, you can refer here.
So in order to capture screenshots of the entire screen using selenium web driver scripts, we can make use of AShot().ĪShot() is a webdriver screenshot utility to capture entire page screenshot and is natively supported from Selenium 3 onwards. But eventually even the latest versions of Firefox now only takes viewport screenshots. Earlier versions of Firefox used to take screenshots of the entire screen, unlike chrome and IE. Some browsers take a screenshot of the viewable port only whereas others take a screenshot of the entire screen.
Capturing Full Webpage Screenshot using Automated Selenium Test ScriptsĪ need may arise to take screenshots of the entire screen rather than just the viewport of the browsers. However, if I want to take a screenshot of the entire webpage, the above mentioned code is not enough. This is all good and awesome if you have to take a screenshot of the view that your code is testing. The code snippet above will take the viewable screen screenshot as below: Jse.executeScript("window.scrollBy(0,250)", "") ĭriver.findElement(By.xpath("//div")).click() ĪrrayList ta = new ArrayList (driver.getWindowHandles()) įile src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE) įpyFile(src, new File("path of the file")) JavascriptExecutor jse = (JavascriptExecutor)driver tProperty("", ".\\Driver\\chromedriver.exe") ĭriver.manage().timeouts().implicitlyWait(10, void performAction() throws InterruptedException The below example showcases Airbnb stay details page example where we are taking a screenshot of the viewable screen:
Let’s incorporate this step into a code snippet. This is it! Just two statements and you will be able to take the screenshot. In order to store the taken screenshot into a file, the below statement is used:įpyFile(screenshotFile, new File("path of the file you want to save the screenshot to")) Here’s the syntax to capture the screenshot:įile screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE) This interface provides a method known as getScreenshotAs which helps to capture the screenshot and store it in the desired location. You can check the detail of the interface here. Selenium provides an out-of-the-box capability called TakeScreenShot interface that can be used for taking screenshots of the viewable area. This is the most used approach of taking screenshots of applications under automation and the easiest one as well.
You can also refer to the below video tutorial on How To Take Screenshots In Selenium testing.Īutomated Selenium Test Scripts for Taking Screenshots of Viewable Area
You can refer to this WebDriver Tutorial to understand more about what WebDriver is, its features, how it works, and best practices. Therefore in this post, we would be looking at how you can take automated screenshots of web pages for different purposes using Selenium WebDriver automation scripts.
Now with that said, when we say screenshot we could mean capturing an image of any part of the screen including the image of an element in question or even a screenshot of the whole page. For Selenium, these screenshots help to distinguish whether the failures are due to application failure or due to script failure. These proofs are in-turn used for debugging, to find out what went wrong and the reason for failure. In automation testing, especially where a typical test run may involve hundreds of commands and test cases, automated screenshots taken at critical assertions are important for developers and testers in making sure that every test case executed as it should. This is equally true no matter the type of testing you are doing and that includes selenium automation testing. Whenever a tester finds and reports a bug, that bug would not be taken seriously without supporting screenshots or even videos of the issue. One of the most performed actions of any webpage tester is taking a screenshot of the webpage.