gpt4 book ai didi

cucumber - 列出 Capybara/Poltergeist 元素的子元素

转载 作者:行者123 更新时间:2023-12-04 15:34:53 26 4
gpt4 key购买 nike

我四处搜索,但找不到办法做到这一点。

我们正在 EmberJS/Rails 应用程序上的 cucumber 功能中使用 Poltergeist 驱动程序运行 Capybara 测试。我无法使用 page.driver.debug 因为我在一个 headless 流浪者实例中运行,所以我无法进行故障排除,但屏幕截图有效,并且有问题的页面上的 Chrome 开发工具检查显示了正确的元素。

我有一个失败的 cucumber 场景,这是因为发现不起作用。我的测试是这样的:

When(/^I delete description "(.*?)"$/) do |description|
within :css, '#myform' do
first('label', :text => /#{description}/).find(:xpath, '..').find(:css, 'button.delete')
end
end

它找不到相关元素。这是 DOM 的相关部分:
<form id="myform">
<div class="row-fluid question">
<div class="span12">
<div class="padding">
<div id="ember576" class="ember-view is-viewing-edit-controls">
<button class="delete" data-ember-action="31"></button>
<button class="edit" data-ember-action="32"></button>
<button class="insert_above" data-ember-action="33"></button>
<button class="insert_below" data-ember-action="34"></button>
<button class="move_up" data-ember-action="35"></button>
<button class="move_down" data-ember-action="36"></button>
<label class="question" data-ember-action="37">Enter your first name</label>
<input id="ember577" class="ember-view ember-text-field">
<span class="error"></span>
</div>
</div>
</div>
</div>
</form>

如果我添加一个binding.pry,这个:
first('label', :text => /#{description}/).find(:xpath, '..')


给我这个回应:
=> #<Capybara::Element tag="div">

和这个:
first('label', :text => /#{description}/).find(:xpath, '..')


给我这个回应:
=> "Enter your first name"

但完整的发现:
first('label', :text => /#{description}/).find(:xpath, '..').find(:css, 'button.delete')

给我这个回应:
Capybara::ElementNotFound: Unable to find css "button.delete"

我有一种感觉,这是 parent / sibling 的问题,但我无法对其进行故障排除。所以我想我有几个问题:
  • 对于调试,是否有一个 poltergeist 驱动程序来列出所有子元素?
  • 我的 xpath 选择器有问题吗?事实..正在返回 Capybara::Element tag="div"让我觉得我有正确的父元素(就像我说的,它适用于其他类似测试中的其他页面),但我无法验证它是哪个元素。我找不到如何获取 xpath 或任何其他帮助我识别元素的东西。
  • 最佳答案

    我想通了。测试工作正常,但我在我的场景中缺少一个操作步骤。

    但是,我认为你们可能会发现了解我最终如何获取内容很有用:在页面上执行 jQuery。我在测试中添加了一个 binding.pry,然后在 binding.pry 控制台中记录了一个 jQuery 结果,例如:

    # this gave me the expected contents and verified that button.delete was a child
    page.execute_script("console.log($('#myform label.question').parent().html())");

    # and this gave me the classes to demonstrate that it was the correct div
    page.execute_script("console.log($('#myform label.question').parent().attr('class'))");

    我选择了选择器的快捷方式,因为我知道我正在寻找的标签是第一个,但是选择器并不重要,它是 binding.pry 中的 console.log()-ing jQuery 的想法很有用。

    关于cucumber - 列出 Capybara/Poltergeist 元素的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18621793/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com