Selenium 作为 Windows 服务运行以截取错误信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1260106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:58:03  来源:igfitidea点击:

Selenium run as a Windows service to take screenshots on errors

javawindowscontinuous-integrationselenium

提问by Grzegorz Oledzki

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.

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

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

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

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

问题是屏幕截图只是空白文件- PNG 文件完全是黑色的。尺寸为 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.

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

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

当 Selenium 测试中发生异常时,如何获取页面截图?

The software used:

使用的软件:

  • Windows 2003 Server
  • Selenium 1.0.1
  • 视窗 2003 服务器
  • 硒 1.0.1

采纳答案by Grzegorz Oledzki

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

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

Consult the wrapper.ntservice.interactivefor 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.

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

回答by Dave Hunt

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.

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

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

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

回答by searlea

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 accessto the application desktop (required for screenshots) and I'd guess their security restrictions continue to this day.

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

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

这个关于java.net 论坛上的服务的线程可能会提供让它工作所需的线索。

回答by DownChapel

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

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

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

使用 Dave Hunt 推荐的 CaptureEntirePageScreenshot 对我来说一直有效。

回答by Steven

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

下面是一些使用 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);
}

It is taken from this site

这是从这个网站上截取的

回答by Stroboskop

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 windowor some other root element. "unrooted" elements will just not paint properly and result in black images most of the time.

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

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.

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

回答by andy droid

An alternative solution to your RunListener approach:

您的 RunListener 方法的替代解决方案:

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

将您的测试包含在 try-catch 块中并在 catch 块中执行屏幕截图,这就是我经常做的。

Example java code:

示例Java代码:

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.

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