【问题标题】:Capture screenshot of alert捕获警报的屏幕截图
【发布时间】:2014-03-07 09:07:29
【问题描述】:

我使用下面的代码在弹出窗口出现后截取应用程序的屏幕截图。

Alert alert = driver.switchTo().alert();
File scrFile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("G:\\Screens\\sc1.jpg"));
String alertMsg = alert.getText();
System.out.println(alertMsg);
alert.accept();

但是它抛出了这个异常

线程“主”org.openqa.selenium.UnhandledAlertException:存在模式对话框:评估名称已存在。

但如果我删除屏幕截图程序,代码可以正常工作。

【问题讨论】:

  • 嘿尼克,您提供的重复链接也与我的问题相同。只有一个问题存在
  • 我是个白痴。谢谢!
  • 谢谢@AndersLindén

标签: testing selenium automation selenium-webdriver


【解决方案1】:

您必须在截屏之前处理警报...您可以在此处了解更多信息 https://code.google.com/p/selenium/issues/detail?id=4412

【讨论】:

  • 因此无法截取警报的屏幕截图
【解决方案2】:

您始终可以使用机器人获取整个页面的屏幕截图。我刚刚试了一下,这段代码可以正常工作:

WebDriver driver;
@Before
public void init() throws Exception {
    driver = new FirefoxDriver();
    driver.get("http://www.tizag.com/javascriptT/javascriptalert.php");
}

@Test
public void bla() throws AWTException, IOException {
    WebElement element = driver.findElement(By.xpath("//input[@type=\"button\"]"));
    // Trigger the alert
    element.click();
    BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    ImageIO.write(image, "png", new File("c:\\localdev\\bla.png"));
    driver.switchTo().alert().accept();
}

【讨论】:

  • 但是这种方法的问题是,如果我们在后台执行测试,那么实际活动的屏幕将被捕获。
  • 我在带有虚拟显示的 Linux 上运行 selenium,它对我有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 2012-01-20
  • 2017-12-03
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多