【问题标题】:capybara cannot find value on a page水豚在页面上找不到价值
【发布时间】:2017-03-18 22:00:37
【问题描述】:

我有一个网页游戏,它使用 javascript 和 jquery 在输入字段中显示一个值。我要测试的值在页面上可见。但是,如果我检查元素,则该值不会出现在 html 中。

这里是一些设置值的 JS 代码:

function updateTotal(){
        $('#edit-game-result').val(game.totalScore);
      }

这里是html输入框:

<input type="text" id="edit-game-result" size="10" maxlength="3" class="form-text" readonly/>

我尝试过 Capybara 测试,例如:

expect(find_field('edit-game-result').value).to eq 10

此测试找到该字段但给出输出: 预期:10 得到:无

我认为这是由于以下原因: Is the HTML is View Source different from the HTML in Inspect Element? 但我找不到让 Capybara 读取值的方法。

这也可能是相关的: http://ahmednadar.github.io/12/09/2013/overwrite-how-Capybara-ignores-hidden-elements/

我相信在运行测试时该字段的值为 10。但是我无法在 Capybara 测试中访问该值。

您能否帮我找到一个有效的 Capybara 测试或阐明为什么它不起作用?

【问题讨论】:

    标签: javascript capybara


    【解决方案1】:

    您遇到的问题是您正在查找元素,然后检查其上的值,该值不会等待值匹配。相反,您应该使用 Capybara 提供的匹配器

    expect(page).to have_field('edit-game-result', with: '10')
    

    它将使用 Capybaras 等待/重试行为来等待值更改。

    【讨论】:

    • 感谢您的回复。我试过了,但得到了:Failure/Error: expect(page).to have_field('edit-game-result', with: '10') expected to find field "edit-game-result" with value "10" but there were no matches. Also found "", which matched the selector but not all filters.
    • @jeebee 那么当你检查它或者它在页面上不可见时,该字段在 Capybara.default_max_wait_time 秒内没有值'10'。您可以通过将visible: false 选项传递给have_field 来检查不可见元素,但您真的不应该依赖于检查测试中的不可见元素。除此之外,显示你的测试的其余部分,以便我们可以看到你实际在做什么
    • 嗨,Thomas 我的代码在这里:github.com/glynester/bowling-challenge/blob/last_frame/spec/… 我尝试了很多组合,包括添加“可见:假”,但没有奏效。我什至在 spec_helper.rb 中为水豚添加了 10 秒的等待时间。感谢收看。
    • @jeebee 您正在测试 JS 行为,但使用不支持 JS 的默认 rack_test 驱动程序。见github.com/jnicklas/capybara#selecting-the-driver
    • 嗨,Thomas 谢谢你,当然你是对的。我现在一直在安装各种 gem 来实现这一点。最后一步(当然)是我需要将 geckodriver 添加到系统路径。我在命令行中尝试了这个:export PATH=$PATH:/home/glynester/Desktop/Projects/geckodriver(根据mzl.la/29MCc1e)但 rspec 仍然无法正常工作。我收到这条消息:Selenium::WebDriver::Error::WebDriverError: Unable to find Mozilla geckodriver.
    猜你喜欢
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-10
    相关资源
    最近更新 更多