Selenium 作为 Windows 服务运行以获取错误屏幕截图

发布于 2024-08-02 00:07:32 字数 561 浏览 2 评论 0原文

我想要一个基于 Selenium RC 的自动化 Web 测试套件,以便在遇到某些错误时截取页面的屏幕截图。 该测试套件是持续集成过程的一部分,从技术上讲,由作为 Windows 服务运行的 CruiseControl 执行。

调用selenium.captureScreenshot()方法来触发实际的屏幕截图。 我已经注册了一个 JUnit RunListener 来在任何测试失败时调用它。

问题是屏幕截图只是空白文件 - 全黑的 PNG 文件。 尺寸为 1440x900。

当从 Eclipse 手动运行相同的测试时,屏幕截图是正常的(尽管它们是整个桌面的屏幕截图而不是浏览器窗口,但这是一个不同的故事和一个小问题)。 所以我猜问题的原因是 Selenium 作为 Windows 服务运行。

Selenium测试出现异常时如何截取页面截图?

使用的软件:

  • Windows 2003 Server
  • Selenium 1.0.1

I would like an automated web tests suite based on Selenium RC to take screenshots of pages when some errors are encountered. The test suite is a part of the continuous integration process and technically is executed by CruiseControl running as a Windows service.

The selenium.captureScreenshot() method is called to trigger the actual screenshot. I've registered a JUnit RunListener to call it on any test failure.

The problem is the screenshots are just blank files - PNG files totally black. The dimensions are 1440x900.

When manually running the same tests from Eclipse the screenshots are OK (althought they are screenshots of the whole desktop not the browser window, but that's a different story and a minor issue). So I guess the cause of the problem is the Selenium being run as a Windows service.

How can I have the screenshots of pages taken when exceptions in Selenium tests happen?

The software used:

  • Windows 2003 Server
  • Selenium 1.0.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

阳光下慵懒的猫 2024-08-09 00:07:32

似乎有一个完全不同的答案。 Windows 服务(由 TanukiSoftware Java Service Wrapper 管理)是通过 wrapper.ntservice.interactive 设置为 false。 将其设置为 true 会有所帮助。 最终屏幕截图包含真实的屏幕内容。

请参阅wrapper.ntservice.interactive 详细信息。 对包装器代码的分析导致 [SERVICE_INTERACTIVE_PROCESS](http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx) 标志在启动 Windows 服务时设置。

Looks like there's a completely different answer. The Windows service (managed by TanukiSoftware Java Service Wrapper) was started with wrapper.ntservice.interactive set to false. Setting it to true has helped. Eventually the screenshots contain real screen content.

Consult the wrapper.ntservice.interactive for details. The analysis of the wrapper code leads to [SERVICE_INTERACTIVE_PROCESS](http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx) flag being set when starting the Windows service.

樱花细雨 2024-08-09 00:07:32

您在什么浏览器中运行这些测试? 如果是 Firefox,我建议使用 captureEntirePageScreenshot 来捕获整个画布 - 不仅仅是视图中的内容(而不是屏幕),如果您正在远程工作并希望在本地保存文件,则建议使用 captureEntirePageScreenshotToString。

不确定它是否能帮助解决您的问题,但由于它使用 Firefox 的附加组件,因此可能值得一试。

What browser are you running these tests in? If it's Firefox I'd recommend the captureEntirePageScreenshot to capture the entire canvas - not just what's in view (rather than the screen) and captureEntirePageScreenshotToString if you're working remotely and want to save the files locally.

Not sure if it will help with your issue, but as it uses an add-on to Firefox it might be worth a try.

一个人的旅程 2024-08-09 00:07:32

阅读更多相关内容,这可能与您的 Windows 服务需要额外的安全权限有关。 回到 Windows NT 3.5/4 服务对应用程序桌面的访问受到限制(屏幕截图需要) )我猜他们的安全限制一直持续到今天。

此帖子有关java.net 论坛上的服务可能会提供您使其正常工作所需的线索。

Reading a little more on this, it could be something to do with your windows service needing extra security permissions. Going back to Windows NT 3.5/4 services had restricted access to the application desktop (required for screenshots) and I'd guess their security restrictions continue to this day.

This thread about services on the java.net forums may provide the clues you need to get it working.

伤痕我心 2024-08-09 00:07:32

我遇到了在 Windows 服务下运行时 CaptureScreenshot 生成黑色图像的问题。

按照 Dave Hunt 的建议使用 CaptureEntirePageScreenshot 对我来说一直有效。

I have come accross the issue with CaptureScreenshot generating black images when running under a windows service.

Using CaptureEntirePageScreenshot as recommended by Dave Hunt is working consistently for me.

掀纱窥君容 2024-08-09 00:07:32

我不知道巡航控制将如何执行 GUI 测试,但我最近自己实现了一个屏幕截图机制,我了解到的是,您正在绘制的组件必须位于窗口或其他根目录中元素。 “无根”元素将无法正确绘制并在大多数情况下导致黑色图像。

因此,如果巡航控制或您的单元测试使用一些魔法来无法正确显示 gui 元素,您可能需要在绘制之前将它们添加到虚拟 JFrame 中。

I don't know how cruise control will execute GUI tests, but i recently implemented a screenshot mechanism myself and what i learned was that the components you're drawing have to be in a window or some other root element. "unrooted" elements will just not paint properly and result in black images most of the time.

So if cruise control or your unit tests are using some magic to not properly display the gui elements, you might want to add them to a dummy JFrame before painting.

贪了杯 2024-08-09 00:07:32

下面是一些使用 Selenium 和 Java Robot 类拍照的代码:

public void takeAScreenShotOfTheApp() {
  Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
  Rectangle screenBounds = new Rectangle(0, 0, screenDim.width, screenDim.height);

  Robot robot = new Robot();
  BufferedImage image =  robot.createScreenCapture(screenBounds);

  File screenshotFile = new File("target" + File.separator
                               + "image" + System.currentTimeMillis() + ".png");
  ImageIO.write(image, "png", screenshotFile);
}

它取自 网站

Here is some code for taking pics using Selenium and the Java Robot class:

public void takeAScreenShotOfTheApp() {
  Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
  Rectangle screenBounds = new Rectangle(0, 0, screenDim.width, screenDim.height);

  Robot robot = new Robot();
  BufferedImage image =  robot.createScreenCapture(screenBounds);

  File screenshotFile = new File("target" + File.separator
                               + "image" + System.currentTimeMillis() + ".png");
  ImageIO.write(image, "png", screenshotFile);
}

It is taken from this site

情何以堪。 2024-08-09 00:07:32

RunListener 方法的替代解决方案:

将测试包含在 try-catch 块中,并在 catch 块中进行屏幕截图,这就是我总是做的事情。

java 代码示例:

public void testEnterFormFields()
  try {
    enterFormFields();
  } catch(SeleniumException e) {
    //do screenshot, logging, dumping, stacktracing or whatever here
  }
}

如果您的编码规则允许,您也可以捕获 Throwable 或 Exception,但逐一捕获特定的 Exception 是有意义的。 这种方法会在代码中产生更多混乱,但在故障分析时会对您有很大帮助。 您可以根据错误类型定制异常处理。 与 SeleniumException 相比,您可能希望在发生 AssertionError 时捕获其他信息。

An alternative solution to your RunListener approach:

Enclose your test in a try-catch block and do the screenshot in the catch block, that's what i always do.

Example java code:

public void testEnterFormFields()
  try {
    enterFormFields();
  } catch(SeleniumException e) {
    //do screenshot, logging, dumping, stacktracing or whatever here
  }
}

You could catch Throwable or Exception as well, if your coding rules allow it, but it makes sense to catch the specific Exception one by one. This approach produces a bit more clutter in your code but it will help you a lot in case of failure analysis. You can tailor your exception handling to the type of error. You maybe want to capture other informations in case of an AssertionError than in case of a SeleniumException.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文