[Selenium+java] How to take screenshot in Selenium Webdriver

Source: Internet
Author: User
Tags guru99

Original url:https://www.guru99.com/take-screenshot-selenium-webdriver.html

Screenshots is desirable for the bug analysis. Selenium can automatically take screenshots during execution. You need to type cast Webdriver instance to Takesscreenshot.

Taking screenshot in Selenium is a 3 Step process

Step 1) Convert Web Driver object to Takescreenshot

Takesscreenshot Scrshot = ((takesscreenshot) webdriver);

Step 2) Call Getscreenshotas method to create image file

File Srcfile=scrshot.getscreenshotas (Outputtype.file);

Step 3) Copy file to desired location

Example:in This Example we'll take screenshot of http://demo.guru99.com/V4/& save it as C:/test.png

Package Guru99takescreenshot;import Java.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.test;public class    guru99takescreenshot {@Test public void Testguru99takescreenshot () throws Exception{webdriver driver;    System.setproperty ("Webdriver.firefox.marionette", "C:\\geckodriver.exe");        Driver = new Firefoxdriver ();        Goto URL Driver.get ("http://demo.guru99.com/V4/");         Call Take screenshot function this.takesnapshot (driver, "c://test.png");      }/** * This function would 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);                Move image file to new destination file Destfile=new file (Filewithpath);    Copy file at Destination fileutils.copyfile (srcfile, destfile); }}

[Selenium+java] How to take screenshot in Selenium Webdriver

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.