java 无法在 Selenium 中截取屏幕截图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13719689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:45:54  来源:igfitidea点击:

Unable to take screenshot in Selenium

javaseleniumwebdrivertestng

提问by Pal

I am trying to capture a screenshot for each failure occurrence and written following code, but this is not working.

我正在尝试为每个故障发生捕获屏幕截图并编写以下代码,但这不起作用。

public class TestFile {

    WebDriver driver = new FirefoxDriver();

    @Test   
    public void Testone(){
        driver.get("http://www.google.com/");           
    }

    @AfterMethod(alwaysRun=true)
    public void catchExceptions(ITestResult result){
        System.out.println("result"+result);
        String methodName = result.getName();
        System.out.println(methodName);

        if(!result.isSuccess()){         
            try { 
                File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(scrFile,new File("C:\screenshot2.png" ));
            } catch (IOException e1) {
                e1.printStackTrace();
            }       
        }
    }
}


This is failing at

这是失败的

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

文件 scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);



stack trace:

堆栈跟踪:

[TestNG] Running:
C:\Documents and Settings7310\Local Settings\Temp\testng-eclipse-1576306112\testng-customsuite.xml

result[TestResult name=Testone status=FAILURE method=TestFile.Testone()[pri:0, instance:com.example.tests.TestFile@1b34126] output={null}]
FAILED CONFIGURATION: @AfterMethod catchExceptions([TestResult name=Testone status=FAILURE method=TestFile.Testone()[pri:0, instance:com.example.tests.TestFile@1b34126] output={null}])
net.sf.cglib.core.CodeGenerationException: java.lang.IllegalAccessException-->Class org.openqa.selenium.remote.Augmenter$CompoundHandler can not access a member of class org.openqa.selenium.firefox.FirefoxDriver with modifiers "protected"
at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:235)

list of imports:

进口清单:

package com.example.tests;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;  

采纳答案by Sinisha Mihajlovski

The stacktrace you shared is not the stacktrace, but I think the testng log.
The example you provided actually works. I just made the test fail, because in the @AfterMethod a screenshot is taken only if the test fails: if(!result.isSuccess())
Then when I ran the example again, I got:
java.io.FileNotFoundException: C:\screenshot2.png (Access is denied)
Then I changed the location of the picture to be on D: where the permissions are correct, and it worked end to end, I can see the screenshot.

您共享的堆栈跟踪不是堆栈跟踪,但我认为是 testng 日志。
您提供的示例确实有效。我只是让测试失败,因为在 @AfterMethod 中,只有在测试失败时才会截取屏幕截图: if(!result.isSuccess())
然后当我再次运行示例时,我得到:
java.io.FileNotFoundException: C: \screenshot2.png(访问被拒绝)
然后我将图片的位置更改为D:权限正确的位置,并且它端到端地工作,我可以看到屏幕截图。

Cheers

干杯

import java.io.File;
import java.io.IOException;

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.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

public class TestFile {

    WebDriver driver = new FirefoxDriver();

    @Test
    public void Testone() {

        driver.get("http://www.google.com/");
        assert false;

    }

    @AfterMethod(alwaysRun = true)
    public void catchExceptions(ITestResult result) {
        System.out.println("result" + result);
        String methodName = result.getName();
        System.out.println(methodName);

        if (!result.isSuccess()) {

            try {

                File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(scrFile, new File("C:\screenshot2.png"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
    }
}

回答by Umamaheshwar Thota

Hi sinisa229 mihajlovski,

嗨 sinisa229 米哈伊洛夫斯基,

Your script is working properly. but there is a slight change in your script. If i won't comment the line "assert false", it is giving error.

您的脚本运行正常。但是您的脚本略有变化。如果我不评论“assert false”这一行,就会出错。

回答by Abhishek_Mishra

Try This :

试试这个 :

 WebDriver augmentedDriver = new Augmenter().augment(driver);
    File screenshot = ((TakesScreenshot)augmentedDriver).
                        getScreenshotAs(OutputType.FILE);

In place of

代替

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);