zoukankan      html  css  js  c++  java
  • [Selenium+Java] How to Take Screenshot in Selenium WebDriver

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

    Screenshots are desirable for bug analysis. Selenium can automatically take screenshots during execution. You need to type cast WebDriver instance to TakesScreenshot.

    How to Take Screenshot in Selenium WebDriver

    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 will 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 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);
    
                //Move image file to new destination
    
                    File DestFile=new File(fileWithPath);
    
                    //Copy file at destination
    
                    FileUtils.copyFile(SrcFile, DestFile);
    
        }
    
    }
  • 相关阅读:
    单片机中的类型转换
    vs2013CCyusb报错(CyAPI.obj)
    c/c++ 去掉空格函数
    keil关于正点原子的sys.h工程报错修改
    【C语言】华软C语言程序设计复习
    c/c++中,clock函数的用法和作用
    vs mfc出现错误“MSB8301”解决办法
    vs出现“未将对象引用到实例的错误”
    keil uv5 代码格式化
    嵌入式软件面试
  • 原文地址:https://www.cnblogs.com/alicegu2009/p/9083009.html
Copyright © 2011-2022 走看看