【问题标题】:capybara waiting for ajax without using sleep水豚等待ajax而不使用睡眠
【发布时间】:2014-04-08 05:34:56
【问题描述】:

我正在使用 Capybara 2.x 对大型 Rails/AngularJS 应用程序进行一些集成测试,我遇到了一个测试,我需要在其中休眠以使其正常工作。

我的测试:

describe "#delete", js: true do
  it "deletes a costing" do
    costing = Costing.make!

    visit "/api#/costings"
    page.should have_content("General")

    click_link "Delete"     # Automatically skips the confirm box when in capybara

    sleep 0.4
    page.should_not have_content("General")
  end
end

它测试的代码使用 ng-table,它需要一秒钟的时间来更新,如果没有睡眠,它将失败。 Capybara 曾经为此有一个 wait_until 方法,但它已被取出。我找到了这个网站:http://www.elabs.se/blog/53-why-wait_until-was-removed-from-capybara,但找不到任何推荐的替代方案来解决这个问题。

这是我正在测试的代码。

  # --------------------------------------------------------------------------------
  # Delete
  # --------------------------------------------------------------------------------
  $scope.destroy = (id) ->
    Costing.delete (id: id), (response) -> # Success
      $scope.tableParams.reload()
      flash("notice", "Costing deleted", 2000)

这会更新 ng-table(tableParams 变量),也就是这段代码

  $scope.tableParams = new ngTableParams({
    page: 1,
    count: 10,
    sorting: {name: 'asc'}
  },{
    total: 0,

    getData: ($defer, params) ->
      Costing.query {}, (data) ->
        # Once successfully returned from the server with my data process it.
        params.total(data.length)

        # Filter
        filteredData = (if params.filter then $filter('filter')(data, params.filter()) else data)

        # Sort
        orderedData = (if params.sorting then $filter('orderBy')(filteredData, params.orderBy()) else data)

        # Paginate
        $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()))
  })

【问题讨论】:

    标签: ruby-on-rails angularjs rspec capybara ngtable


    【解决方案1】:

    尝试将Capybara.default_wait_time 加到 3 秒或 4 秒。

    如果失败,请尝试更改规范以查找 Flash 通知消息,然后再检查该项目是否已从页面中删除。 (假设 Flash 消息在 HTML 正文中呈现)

    describe "#delete", js: true do
      it "deletes a costing" do
        costing = Costing.make!
    
        visit "/api#/costings"
        page.should have_content("General")
    
        click_link "Delete"
    
        page.should have_content("Costing deleted")
        page.should_not have_content("General")
      end
    end
    

    编辑 - 删除解释,因为它不正确。

    【讨论】:

    • 它仍然在 should_not 测试中中断,它通过了 flash 消息测试。我还尝试将默认等待时间提高到 5 秒,但没有帮助。我还是得好好睡一觉。
    • 值得注意的是have_capybara方法一般会触发等待行为,基于默认等待时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 2012-10-29
    • 2011-08-03
    • 2017-07-02
    相关资源
    最近更新 更多