如何让HtmlUnit的WebClient加速执行window.setTimeout触发的javascript?

发布于 2024-11-06 00:29:02 字数 714 浏览 0 评论 0原文

我正在使用 Java 库 HtmlUnit 为 Web 应用程序创建回归测试套件。

我有一个“onload”处理程序挂在应用程序页面的主体中,以在会话过期后重定向到超时页面。处理程序是一些以下形式的 JavaScript:

window.setTimeout( function() { window.location = 'timout.html'; }, 3600000);

我想测试重定向最终会是当时间到达时触发,但我能找到的最接近的方法是实际等待整个时间(在上面的示例中为一个小时),如下面的 Java 示例所示:

WebClient webClient = new WebClient( ); ... webClient.waitForBackgroundJavaScript( 3600000);

我想知道是否有可能欺骗脚本执行引擎,让它表现得好像已经过去了那么长时间,而实际上不必等待几分钟或几小时"实时" 用于运行测试套件。

理想情况下,可以告诉引擎/客户端/解释器已经过去了 X 毫秒(以模拟等待),或者可能设置某种“时间膨胀”因子并轮询页面以查看其更新情况。

I am using the Java library HtmlUnit to create a regression test suite for a web app.

I have an "onload" handler hooked in the body of pages of the app to redirect to a timeout page after the session would have expired. The handler is some JavaScript of the form:

window.setTimeout( function() { window.location = 'timout.html'; }, 3600000);

I would like to test that the redirect will eventually be fired when the time arrives, but the closest thing I can find is to actually wait the entire duration of the time (say an hour in the example above), as suggested by the Java sample below:

WebClient webClient = new WebClient();
...
webClient.waitForBackgroundJavaScript( 3600000);

I would like to know if it is possible to trick the script execution engine into behaving as if that much time has passed, without actually having to wait minutes or hours "real time" for a test suite to be run.

Ideally, one could tell the engine/client/interpreter that X milliseconds had passed (to emulate the wait), or perhaps to set some kind of "time dilation" factor and poll the page to see how it is being updated.

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

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

发布评论

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

评论(1

初懵 2024-11-13 00:29:02

我不认为你能轻易做到这一点。我能看到它的唯一方法是模拟在测试中处理 settimeout 的 Rhino 方法......
也许使用像 Mockito 这样的框架...但这不是您想要的,就像您想测试页面重定向的有效性一样,您测试重定向所需的时间

欢呼

groveek

I don't think you can do that easily. The only way I can see it possible would be by mocking the Rhino method that handles the settimeout in you tests...
Perhaps with a framework like Mockito... But that's not what you want as if you want to test the effectiveness of the redirection of the page, you test the amount of time it takes to be redirected

cheers

grooveek

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