如何计算活动的 JavaScript 超时?

发布于 2024-11-30 05:13:19 字数 366 浏览 0 评论 0原文

我正在使用 Selenium 来测试一个使用 Dojo 的 Web 应用程序,它使用 java 脚本来显示某些元素。我想等到所有元素都显示后再尝试操作页面,但是我遇到了麻烦。

我首先等待 dojo inFlight 变量为 0,这表示所有 ajax 都已完成。这并不总是有效,因为它似乎会在之后超时时执行一些操作。

我也尝试过反复查找该元素,但这不太好,因为稍后可能有一些 javascript 会以某种方式使用该字段。

所以基本上我想要一个方法(至少在firefox中)来查询等待在setTimeout(或setInterval)上运行的javascript,我什至可以处理通过我自己的函数包装内置调用的方法,只是为了跟踪这。

任何想法或建议表示赞赏!

I am using Selenium to test a web app that uses Dojo, and it uses java script to display certain elements. I want to wait until all of the elements are desplayed before I try to manipulate the page, however I am having trouble.

I have started by waiting for the dojo inFlight variable to be 0, this says that all ajax has finished. This doesn't always work because it seems to do some things on a timeout afterwards.

I have also tried repeatedly looking for the element, but this isn't too nice, as perhaps there is some javascript later which will use this field in some way.

So basically I want a method (in firefox at least) to query the javascript waiting to run on a setTimeout (or setInterval) I could even cope with a way of wrapping the built in call through a function of my own just to keep track of this.

Any thoughts or suggestions appreciated!

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

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

发布评论

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

评论(3

蒲公英的约定 2024-12-07 05:13:19

JavaScript 中的每个函数都可以被替换。考虑这样的事情:

window.originalSetTimeout = window.setTimeout;

window.setTimeout = function(func, delay, params) {
    window.timeoutCounter++;
    window.originalSetTimeout(window.timeoutCallback, delay, [func, params]);
}

window.timeoutCallback = function(funcAndParams) {
    window.timeoutCounter--;
    func = funcAndParams[0];
    params = funcAndParams[1];
    func(params);
}

那么:

selenium.waitForCondition("window.timeoutCounter == 0");

Every function in JavaScript can be replaced. Consider something like this:

window.originalSetTimeout = window.setTimeout;

window.setTimeout = function(func, delay, params) {
    window.timeoutCounter++;
    window.originalSetTimeout(window.timeoutCallback, delay, [func, params]);
}

window.timeoutCallback = function(funcAndParams) {
    window.timeoutCounter--;
    func = funcAndParams[0];
    params = funcAndParams[1];
    func(params);
}

Then:

selenium.waitForCondition("window.timeoutCounter == 0");
柒夜笙歌凉 2024-12-07 05:13:19

每当您调用 setIntervalsetTimeout 时,都会返回一个计时器 ID。

  1. 将该计时器 ID 保存在数组中
  2. 在超时时调用的函数内,将该计时器 ID 从数组中弹出。因为我们必须在计时器结束后立即从数组中删除该 id。
  3. 任何时候你想检查号码。活动计时器,只需查询该数组的长度即可。

Whenever you call setTimeout of setInterval -- a timer id is returned.

  1. Save that timer id in an array
  2. Inside the function that you're calling on the timeout, pop that timer id off the array. Because we have to remove that id from the array as soon as the timer ends.
  3. Any time you want to check the no. of active timers, just query the length of that array.
囚你心 2024-12-07 05:13:19

另一种方法可能是这样的

const timeoutIndexThen = setTimeout(() => {});

// Some code that might call timeouts...

const timeoutIndexNow = setTimeout(() => {});

const countOfTimeoutsFiredSinceThen = timeoutIndexNow - timeoutIndexThen - 1;

它基于这样的事实:每次超时都会在每次调用时返回一个大于 1 的数字。

因此,我们创建一些虚拟超时只是为了在某个时刻获取这个数字。

然后,过了一会儿我们再次拨打它,我们得到了一个新号码。这些数字之间的差异是中间调用了多少次间隔。

这样做的缺点是您必须实际创建超时。好处是您不必修改原始功能。

Another approach could be like this

const timeoutIndexThen = setTimeout(() => {});

// Some code that might call timeouts...

const timeoutIndexNow = setTimeout(() => {});

const countOfTimeoutsFiredSinceThen = timeoutIndexNow - timeoutIndexThen - 1;

It is based on the fact that each timeout will return a number that is greater by 1 on each call.

So we create some dummy timeout just to get this number at some point.

Then, after a while we call it again and we get a new number. Difference between those numbers is how many times interval was called in between.

Downside of this is that you have to actually create the timeout. Upside is that you don't have to modify original function.

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