检查水豚中多个文本节点内容时的计时问题

发布于 2025-01-01 06:55:27 字数 773 浏览 1 评论 0原文

我们在页面上有一个快速搜索字段,它通过 AJAX 调用过滤先前搜索的结果列表。

我们尝试了多种方法来检查更改列表的内容。

一开始,有一个像这样的列表:

<div class="search-list">
  <div class="entry">
    <div class="job-title">Manager</div>
    ...
  </div>
  <div class="entry">
    <div class="job-title">Slave</div>
    ...
  </div>
</div>

快速搜索后,整个搜索 search-list 将被 AJAX 响应替换,第二个 entry div 为已删除。

我们尝试使用以下语句检查此条件:

page.find('.search-list .job-title').map(&:text) =~ ['Manager']

但它只能看到发出 AJAX 请求之前的状态。这可能是由于立即检查条件而没有等待 AJAX 请求完成。我们尝试了多种方法,例如设置 Selenium 驱动程序的 resynchronize 选项。

将 sleep 10 放在上面的行前面确实有效,但这是一个不干净的解决方案。目前我们不知道如何让它发挥作用,其他人知道吗?

We're having a quick search field on a page which filters the result list of a previous search via an AJAX call.

We have tried several methods of checking the content of the changed list.

At the beginning, there is a list like e.g. this:

<div class="search-list">
  <div class="entry">
    <div class="job-title">Manager</div>
    ...
  </div>
  <div class="entry">
    <div class="job-title">Slave</div>
    ...
  </div>
</div>

After the quick search, the whole search search-list will be replaced by an AJAX response with the second entry div being removed.

We're trying to check this condition with the following statement:

page.find('.search-list .job-title').map(&:text) =~ ['Manager']

but it only sees the state before the AJAX request has been made. This is probably due to the condition being checked immediately without waiting for the completion of the AJAX request. We have tried several methods, like settings the Selenium driver's resynchronize option.

Putting a sleep 10 in front of the above line does work, but is an unclean solution. Currently we have no idea how to get this working, does anyone else?

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

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

发布评论

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

评论(1

栀梦 2025-01-08 06:55:27

我使用 wait_until 的辅助方法来等待 ajax 完成,然后进行测试。

所以也许像这样:

page.wait_until { page.evaluate_script "jQuery.active == 0" }
page.find('.search-list .job-title').map(&:text) =~ ['Manager']

wait_until 等待块返回 true (尽管它会在一段时间后超时)。您还可以向其传递一个超时值 wait_until(5),以便在分配的时间后退出。

您还可以延长水豚的等待时间。

Capybara.default_wait_time = 10

Angular.js

angular.element.active == 0 的更新以同样的方式工作。

jQuery.active == 0 仍然可以在 Angular.js 中工作,但不确定这是否是因为我将 jQuery 作为我的 javascript 库的一部分。

I use a helper method using wait_until to wait for the ajax to finish, then do my test.

So perhaps something like:

page.wait_until { page.evaluate_script "jQuery.active == 0" }
page.find('.search-list .job-title').map(&:text) =~ ['Manager']

wait_until wait until true is returned by the block (though it will timeout after a little bit). You can also pass it a timeout value, wait_until(5), to exit after the time allotted.

You could also possibly up your Capybara wait time.

Capybara.default_wait_time = 10

Update for Angular.js

angular.element.active == 0 works in the same fashion.

jQuery.active == 0 stil works within Angular.js, but not sure if that is because I have jQuery as part of my javascript libraries.

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