我是JMeter + WebDriver + Java的新手。JavaScript代码正在像预期的那样用于屏幕截图。由于最近对Java的升级,我们正在从JavaScript转向Java。
下面是密码。你能帮忙吗?
import org.openqa.selenium.By;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.ui;
import java.io.File;
import org.apache.commons.io.FileUtils;
import Java.awt.Robot;
import java.awt.image.BufferedImage;
WDS.sampleResult.sampleStart();
WDS.browser.get("https://www.google.com/");
Thread.sleep(10);
TakesScreenshot ts = (TakesScreenshot)WDS.browser;
File srcFile = ts.getScreenshotAs(OutputType.FILE);
File destFile = new File("/Users/****/desktop/screenshot.png");
try{
FileUtils.copyFile(srcFile, destFile);
} catch (IOException e) {
e.printStackTrace();
}
WDS.sampleResult.sampleEnd();另外,我尝试了下面的代码
File mFile = ((TakesScreenshot)WDS.browser).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(mFile, new File("output/" + WDS.name + "-" + WDS.parameters + ".png"));错误消息:
2022-05-03 11:28:51,641 ERROR c.g.j.p.w.s.WebDriverSampler: Sourced file: inline evaluation of: ``import org.openqa.selenium.By; import org.openqa.selenium.support.events.EventFi . . . '' : Typed variable declaration : Class: TakesScreenshot not found in namespace : at Line: 14 : in file: inline evaluation of: ``import org.openqa.selenium.By;
import org.openqa.selenium.support.events.EventFi . . . '' : TakesScreenshot in inline evaluation of: ``import org.openqa.selenium.By;
import org.openqa.selenium.support.events.EventFi . . . '' at line number 14
发布于 2022-05-03 15:58:11
此错误是由于缺少导入 of TakesScreenshot类造成的。
您需要添加下一行:
import org.openqa.selenium.TakesScreenshot你剧本的开头。
另外,关于“从js迁移到Java",您要移动的地方不是"Java",而是贝贝解释器,它与Java不完全兼容。而且,使用Beanshell是某种形式的性能反模式.,所以也许“我们”应该考虑改用Groovy。
https://sqa.stackexchange.com/questions/50059
复制相似问题