Java 无法在Selenium中拍摄屏幕截图
我试图捕捉每个故障发生的屏幕截图,并编写以下代码,但这不起作用Java 无法在Selenium中拍摄屏幕截图,java,selenium,webdriver,testng,Java,Selenium,Webdriver,Testng,我试图捕捉每个故障发生的屏幕截图,并编写以下代码,但这不起作用 public class TestFile { WebDriver driver = new FirefoxDriver(); @Test public void Testone(){ driver.get("http://www.google.com/"); } @AfterMethod(alwaysRun=true) public
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();
}
}
}
}
这是失败的 文件scrFile=((TakesScreenshot)driver.getScreenshotAs(OutputType.File)
堆栈跟踪: 进口清单: 试试这个:
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);
代替
File scrFile=((TakesScreenshot)驱动程序).getScreenshotAs(OutputType.File)代码>您共享的stacktrace不是stacktrace,但我认为testng日志
您提供的示例实际上有效。我刚刚让测试失败了,因为在@AfterMethod中,只有当测试失败时才会截图:if(!result.issucess())
然后,当我再次运行该示例时,我得到:
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();
}
}
}
}
你好,西尼萨229米哈伊洛夫斯基
您的脚本工作正常。但是你的剧本有一点变化。如果我不注释“assert false”一行,它将给出错误。显示以下错误消息---配置失败:@AfterMethod catchExceptions([TestResult name=Testone status=FAILURE method=TestFile.Testone()[pri:0,instance:com.example.tests]。TestFile@1b273cc]输出={null}])net.sf.cglib.core.CodeGenerationException:java.lang.IllegalAccessException-->Class org.openqa.selenium.remote.Augmenter$CompoundHandler无法访问类org.openqa.selenium.firefox.FirefoxDriver的成员,修饰符为“protected”您使用的是driver.quit()还是driver.close()在执行屏幕截图代码之前?我建议将您的if条件更改为:if(driver!=null&&driver.getSessionId()!=null&&result.isSuccess())Class org.openqa.selenium.remote.Augmenter$CompoundHandler无法访问带有“protected”修饰符的类org.openqa.selenium.firefox.FirefoxDriver的成员…此消息显示用于解决该特定异常的问题和修复,请参阅您可以共享您的导入吗?@niharika_neo:导入在问题中更新我在代码之前的文本中提到了“断言更改”。如果删除断言,则不会出现错误,但测试将通过,并且不会截图。如果我对断言错误行进行注释,则它在没有截图的情况下正确运行。当我删除评论时,它正在拍摄屏幕截图。谢谢
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);
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();
}
}
}
}