gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:32:45 24 4
gpt4 key购买 nike

我试图为每次失败事件捕获屏幕截图并编写以下代码,但这不起作用。

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)

进口 list :

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;

最佳答案

你分享的stacktrace不是stacktrace,但我认为是testng日志。
您提供的示例确实有效。我只是让测试失败了,因为在 @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();
}

}
}
}

关于java - 无法在 Selenium 中截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13719689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com