How can I take a screenshot with Selenium WebDriver?

后端 未结 30 2575
不知归路
不知归路 2020-11-21 07:48

Is it possible to take a screenshot using Selenium WebDriver?

(Note: Not Selenium Remote Control)

相关标签:
30条回答
  • 2020-11-21 08:31

    Java

    Using RemoteWebDriver, after augmenting the Node with screenshot capability, I would store the screenshot like so:

    void takeScreenShotMethod(){
        try{
            Thread.sleep(10000);
            long id = Thread.currentThread().getId();
            BufferedImage image = new Robot().createScreenCapture(new Rectangle(
                Toolkit.getDefaultToolkit().getScreenSize()));
            ImageIO.write(image, "jpg", new File("./target/surefire-reports/"
                + id + "/screenshot.jpg"));
        }
        catch( Exception e ) {
            e.printStackTrace();
        }
    }
    

    You may use this method wherever required. Then, I assume you can customize the style sheet of maven-surefire-report-plugin at surefire-reports/html/custom.css so that your reports include the link to the correct screenshot for each test?

    0 讨论(0)
  • 2020-11-21 08:32

    Selenese

    captureEntirePageScreenshot | /path/to/filename.png | background=#ccffdd
    
    0 讨论(0)
  • 2020-11-21 08:33

    Python

    You can capture the image from windows using the Python web driver. Use the code below which page need to capture the screenshot.

    driver.save_screenshot('c:\foldername\filename.extension(png, jpeg)')
    
    0 讨论(0)
  • 2020-11-21 08:35

    Ruby

    require 'rubygems'
    require 'selenium-webdriver'
    
    driver = Selenium::WebDriver.for :ie
    driver.get "https://www.google.com"
    driver.save_screenshot("./screen.png")
    

    More file types and options are available and you can see them in file takes_screenshot.rb.

    0 讨论(0)
  • 2020-11-21 08:36

    C#

    public static void TakeScreenshot(IWebDriver driver, String filename)
    {
        // Take a screenshot and save it to filename
        Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
        screenshot.SaveAsFile(filename, ImageFormat.Png);
    }
    
    0 讨论(0)
  • 2020-11-21 08:36

    Java

    A method to capture a screenshot for the failures in Selenium with TestName and Timestamp appended.

    public class Screenshot{
        final static String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
        public static String imgname = null;
    
        /*
         * Method to Capture Screenshot for the failures in Selenium with TestName and Timestamp appended.
         */
        public static void getSnapShot(WebDriver wb, String testcaseName) throws Exception {
          try {
          String imgpath = System.getProperty("user.dir").concat("\\Screenshot\\"+testcaseName);
          File f = new File(imgpath);
          if(!f.exists())   {
              f.mkdir();
            }
            Date d = new Date();
            SimpleDateFormat sd = new SimpleDateFormat("dd_MM_yy_HH_mm_ss_a");
            String timestamp = sd.format(d);
            imgname = imgpath + "\\" + timestamp + ".png";
    
            // Snapshot code
            TakesScreenshot snpobj = ((TakesScreenshot)wb);
            File srcfile = snpobj.getScreenshotAs(OutputType.FILE);
            File destFile = new File(imgname);
            FileUtils.copyFile(srcfile, destFile);
    
          }
          catch(Exception e) {
              e.getMessage();
          }
       }
    
    0 讨论(0)
提交回复
热议问题