简体   繁体   English

Watir-webdriver无法单击出现在弹出窗口中的可见链接

[英]Watir-webdriver can't click on visible link which appears in popup

I am using watir-webdriver + ruby + rspec + gem parallel_tests. 我正在使用watir-webdriver + ruby​​ + rspec + gem parallel_tests。

And my test case has to be able to cancel and then delete item from items list. 我的测试用例必须能够取消然后从项目列表中删除项目。 "Cancel" and "Delete" links appears from pop-up menu. 弹出菜单中显示“取消”和“删除”链接。 Following method verifies cancel link gear_dropdown_menu.cancel_job = browser.link(:text, 'Cancel') is visible. 以下方法验证取消链接gear_dropdown_menu.cancel_job = browser.link(:text, 'Cancel')是可见的。

  Timer.repeat_until_true(30, 1) do
    sleep 1
    gear_dropdown_menu.cancel_job.visible?
  end

When link appeared and I can see it, next I try to click it using such code: 当链接出现并且可以看到它时,接下来我尝试使用以下代码单击它:

  browser.execute_script("
    id = $('div.job-actions:visible').data('id');
    $('a[href*=\"jobs/'+ id +'/cancel\"]').show().click()
  ")

div.job-actions:visible - actions popup, where links are placed

I use data-id attribute to specify direct link href. 我使用data-id属性指定直接链接href。 But it looks like watir can't see it. 但是看来watir看不到。

The problem is when I execute tests in parallel, 2 from 3 tries it fails. 问题是当我并行执行测试时,三分之二的尝试会失败。 When I execute test non-parallel, looks overall good. 当我非并行执行测试时,看起来总体不错。

What may be the cause of the problem? 问题可能是什么原因?

Updated: add code of popup. 更新:添加弹出代码。 It's not a separate window. 这不是一个单独的窗口。 Just hidden div element which appears when I click a button. 单击按钮时出现的只是隐藏的div元素。

 <div class="job-actions" data-id="8769" style="top: 329px; right: 0px; display: block;">
  <section>
    <header>Actions</header>
    <ul>
            <li class="pause">
              <a href="http://host.com/jobs/8769/pause">Pause Job</a>
            </li>
          <li class="cancel">
            <a href="http:/host.com/jobs/8769/cancel" class="cancel">Cancel Job</a>
          </li>
          </ul>
  </section>
</div>

(text: 'Cancel') is going to match the first cancel button on your list. (文本:“取消”)将与列表上的第一个取消按钮匹配。 If you are running tests in parallel you are likely getting race conditions to cancel/delete the same items from the list. 如果您并行运行测试,则可能会遇到竞争条件,以从列表中取消/删除相同的项目。

A couple other tips: You don't have to use Timer. 其他一些技巧:您不必使用Timer。 Your code is functionally equivalent to the built in method: 您的代码在功能上等效于内置方法:

gear_dropdown_menu.cancel_job.wait_until_present

Also, is your popup a separate window? 另外,您的弹出窗口是一个单独的窗口吗? If so, you don't need to use execute_script (which should be avoided when possible). 如果是这样,则无需使用execute_script(在可能的情况下应避免使用)。 you can use: 您可以使用:

browser.window(title: 'Your Popup Title').use { browser.link(href: /cancel/).click } (or whatever selector makes sense) browser.window(title: 'Your Popup Title').use { browser.link(href: /cancel/).click } (或任何有意义的选择器)

If your problem only occurs when running in parallel then it sounds like you may have either dependencies or interferences between tests, which end up creating a race condition. 如果您的问题仅在并行运行时发生,则听起来您在测试之间可能存在依赖关系或干扰,最终会导致产生竞争条件。

If you are doing Create, Update, Delete type operations, then each test needs to work with unique objects, created with unique identifiers (names? id's? ) if not then either an object might not exist when you need it (not created yet, deleted, etc) or you could run into conflicts caused because the object names/identifiers are not unique (so a test could re-create a new copy of an object another test just deleted, causing the second test to think the delete failed, etc) 如果您要执行创建,更新,删除类型操作,那么每个测试都需要使用唯一的对象,并使用唯一的标识符(名称或ID)来创建(如果没有),那么在您需要时某个对象可能不存在(尚未创建,删除等),或者由于对象名称/标识符不唯一而导致冲突(因此测试可能会重新创建对象的新副本,而另一个测试刚刚删除,导致第二个测试认为删除失败,等等) )

Without seeing a LOT more of your code it would be difficult to give you more precise advice, but in my experience when something works when done in series, but not if the order is altered, or the test run solo, or tests run in parallel, then you likely have dependencies between tests, or a situation like one test deleting something before the other test can try to cancel or update the same something. 如果看不到更多代码,很难为您提供更精确的建议,但是以我的经验,当串行完成某些工作时,而不是顺序更改,单独运行测试或并行运行测试时,这是不可行的,那么您可能在测试之间存在依赖关系,或者像一个测试在另一个测试可以尝试取消或更新相同内容之前删除某些内容的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM