selenium - 我如何调用 captureScreenshot 方法来报告错误?
问题描述
我的以下代码仅显示 5 个工具提示列表,实际上有 6 个工具提示。System.out.println(contactTooltipList.size()) 给出大小 6。
public void clickOnContacts()
{
log.info("Clicked on 'Contacts' tab option and Tooltips are: ");
System.out.println(contactTooltipList.size());
contactDropDown.click();
Iterator<WebElement> itr= contactTooltipList.iterator();
while(itr.hasNext()) {
String toolTips = itr.next().getText();
toolTips=toolTips.replaceAll("\n", " ");
System.out.println(toolTips);
}
System.out.println("\n");
}
缺少第一个工具提示(地址),上面的代码显示了从 2 到 5 的工具提示,如下所示:
Home: (111) 222-3333
Mobile: (500) 000-0000
Email: xxxxxxxxx...
Secondary Language -
Preferred Unknown
我想通过捕获屏幕截图来报告错误,因为显示的工具提示与大小不同。我如何调用 captureScreenshot 方法?或者我如何报告这是一个错误?
解决方案
在 Java 中,您可以这样截屏:
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
// add the above imports
File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
现在你已经screenshotFile
存储了,你可以用它做任何你需要的事情——比如把它保存到你的系统中:
FileUtils.copyFile(screenshotFile, new File("C:/Path/where/to/save/screenshot));