How To Take Screenshot In Selenium Webdriver Using Java
Why take screenshots in the first identify?
Automated testing has go a pregnant part of the entire software testing lifecycle. The power to execute a script and become results without interference or monitoring has resulted in significantly higher levels of efficiency in much shorter timelines – something every agile squad needs.
However, the whole point of automatic testing is defeated if one has to re-run an entire exam every fourth dimension a script fails. If something goes wrong, it helps non but to have the bug pointed out in the code, but also take some visual representation of the exact anomaly. Similarly, a tester has to check if the catamenia of the awarding is as intended. Once again, visual representation helps in this instance.
This is where Selenium screenshots come up in.
Hither are some typical cases in which Selenium screenshots would exist required:
- When application bug occur
- When an assertion failure occurs
- When at that place is some difficulty in finding spider web elements on a folio
- Where there is a Timeout in finding web elements on a web folio
Thank you to sure new functionalities incorporated in Selenium WebDriver, testing has become much simpler. Since the WebDriver architecture allows a user to interact outside the Javascript sandbox, it is possible, amidst other things, to take screenshots in Selenium.
How does a user take a screenshot in Selenium?
In order to capture a screenshot in Selenium, one has to utilize the method TakesScreenshot. This notifies WebDrive that information technology should have a screenshot in Selenium and store it.
Syntax:
File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); String screenshotBase64 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
In the above snippet, OutputType defines the output blazon for the required screenshot.
If the user intends to take a Selenium screenshot and shop information technology in a designated location, the method to apply is getScreenshotAs.
Hither's an example of this method in usage:
X getScreenshotAs(OutputType(X). target) throws WebDriverException
In the to a higher place snippet
- Ten is the return type of the method
- target holds the destination address
- throws WebDriverException is activated if screenshot capturing is non supported.
Depending on the browser existence used, the TakesScreenshot method tin return the following:
- The entire page
- The current open up window
- The visible segment of the current frame
- The whole brandish containing the browser
- The complete content of the HTML element. This essentially refers to the visible portion of the HTML element.
Test Screenshots in Selenium for Free
Here is a Comprehensive Guide to Run your Selenium Exam using BrowserStack Automate
Please find the Github repo here for the example cited in the video.
Screenshot for test failure
If a exam fails, the ability to take screenshots in Selenium is particularly helpful to understand what went wrong. An easy way to do this would be to use TestNG annotations.
Hither are the steps to capture a screenshot in selenium in this instance:
- Create a grade. Implement TestNG 'ITestListener'.
- Call the method 'onTestFailure'.
- Add the code to take a screenshot with this method.
- Get the Examination method proper name and take a screenshot with the test name. And then identify it in the desired destination binder.
How to get the driver object in TestListeners using TestNG?
Taking Selenium screenshots requires a user to have a driver object. In order to do so, one has to get the driver from ITestContext which, in turn, must be configured in the base setup. This makes it piece of cake to create a driver instance and proceed appropriately.
Example of Selenium Screenshot
Permit'south explore this part with a three-step procedure –
The lawmaking detailed below will take a screenshot of https://www.browserstack.com & save it equally C:/Test.png
Pace #1 – Convert web driver object to TakeScreenshot
TakesScreenshot scrShot =((TakesScreenshot)webdriver);
Step #2 – Telephone call getScreenshotAs method to create prototype file
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
Footstep #3 – Copy file to Desired Location
package BrowserstackScreenShot; import coffee.io.File; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Exam; public class BStackTakeScreenshot { @Exam public void testBStackTakeScreenShot() throws Exception{ WebDriver commuter ; System.setProperty("webdriver.firefox.marionette","c:\\geckodriver.exe"); driver = new FirefoxDriver(); //goto url driver.get("https://www.browserstack.com"); //Call take screenshot part this.takeSnapShot(driver, "c://test.png") ; } /** * This function will take screenshot * @param webdriver * @param fileWithPath * @throws Exception */ public static void takeSnapShot(WebDriver webdriver,String fileWithPath) throws Exception{ //Convert web driver object to TakeScreenshot TakesScreenshot scrShot =((TakesScreenshot)webdriver); //Call getScreenshotAs method to create image file File SrcFile=scrShot.getScreenshotAs(OutputType.FILE); //Motility epitome file to new destination File DestFile=new File(fileWithPath); //Copy file at destination FileUtils.copyFile(SrcFile, DestFile); } }
Try to have a screenshot in Selenium using the process detailed above. Once mastered, it is quite uncomplicated and requires just a niggling forethought to set and execute. The benefits this role provides far outweigh any effort required to execute the few bones commands.
To keep things even simpler, a user tin can employ a tool that allows for capturing screenshots without using code. A tool such as BrowserStack'due south screenshot generator, requires the user to only input a URL, click a button and receive screenshots beyond a host of selected existent devices and browsers. The Local Testing option allows users to practise the aforementioned with websites that are locally hosted and not publically accessible.
Try Screenshot Tool for Free
BrowserStack Screenshots Tool Dashboard
Either style, the ability to take screenshots is an important part of the tester's skillset. Being equipped with the right tools and commands volition go a long way towards ensuring that the testing process is non hindered past a lack of cognition or foresight.
How To Take Screenshot In Selenium Webdriver Using Java,
Source: https://www.browserstack.com/guide/take-screenshots-in-selenium
Posted by: morenoknestagave36.blogspot.com
0 Response to "How To Take Screenshot In Selenium Webdriver Using Java"
Post a Comment