无法在Selenium中进行屏幕截图

7
我正在尝试为每个失败的情况捕获屏幕截图,并编写了以下代码,但这并没有起作用。
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();
            }       
        }
    }
}

这段代码出现错误:

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


错误堆栈信息:

[TestNG] Running:
C:\Documents and Settings\537310\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)

导入列表:

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;  

你能分享一下你的导入吗? - niharika_neo
@niharika_neo:问题中的导入已更新。 - Pal
3个回答

2
您分享的堆栈跟踪不是堆栈跟踪,而是测试日志。您提供的示例实际上是有效的。我只是让测试失败了,因为在@AfterMethod中仅在测试失败时才会拍摄屏幕截图: if(!result.isSuccess())。然后当我再次运行示例时,我得到了如下结果: java.io.FileNotFoundException: C:\screenshot2.png (访问被拒绝) 然后我将图片位置更改为D盘,在那里权限是正确的,它可以正常工作,我可以看到屏幕截图。 祝好!
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();
            }

        }
    }
}

1

你好,sinisa229 mihajlovski,

你的脚本运行正常。但是你的脚本需要稍作修改。如果我不注释掉“assert false”这一行,它会报错。


我在代码前面提到了我所做的“assert change”。如果你移除这个assert,就不会有错误,但测试也不会拍摄截图。 - Sinisha Mihajlovski
你说得对,如果我注释掉assert false那一行,它就可以正常运行而不会截屏。当我取消注释时,它就会截屏。谢谢。 - Umamaheshwar Thota

0

试一试这个:

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

替换为

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


1
显示以下错误消息---配置失败:@AfterMethod catchExceptions([TestResult name=Testone status=FAILURE method=TestFile.Testone()[pri:0, instance:com.example.tests.TestFile@1b273cc] output={null}]) net.sf.cglib.core.CodeGenerationException:java.lang.IllegalAccessException - Class org.openqa.selenium.remote.Augmenter $ CompoundHandler无法访问带有修饰符“protected”的类org.openqa.selenium.firefox.FirefoxDriver的成员 - Pal
你在执行屏幕截图代码之前使用了driver.quit()或driver.close()吗?我建议将你的if条件更改为:if (driver != null && driver.getSessionId()!= null && !result.isSuccess()) - Abhishek_Mishra
1
类 org.openqa.selenium.remote.Augmenter$CompoundHandler 无法访问带有修饰符“protected”的类 org.openqa.selenium.firefox.FirefoxDriver 的成员。此消息正在显示。 - Pal
1
有关该特定异常的问题和解决方法,请参见https://code.google.com/p/selenium/issues/detail?id=5087。 - David

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接