代码之家  ›  专栏  ›  技术社区  ›  smcdrc

如何用Cucumber测试JQuery UI可排序

  •  12
  • smcdrc  · 技术社区  · 14 年前

    4 回复  |  直到 14 年前
        1
  •  1
  •   paozac    14 年前

    我正在使用这样的网络步骤,它运行良好:

    When /^I drag "([^"]*)" on top$/ do |name|
      item = Item.find_by_name(name)
      sleep 0.2
      src  = find("#item_id_#{item.id}")
      dest = find("div.title")
      src.drag_to(dest)
    end
    
        2
  •  12
  •   Matthew O'Riordan    13 年前

    我开发了一个JQuery插件来解决这个问题 jquery.simulate.drag-sortable.js 其中包括一个插件以及一套测试和示例。

    马特

        3
  •  2
  •   Francois    14 年前

    拖拽法对我不起作用。但是,通过在使用jquery.simulate.js的capybara selenium测试中包含以下内容,我能够将列表中的第一个元素拖到最后一个位置:

    page.execute_script %Q{
      $.getScript("/javascripts/jquery.simulate.js", function(){ 
        distance_between_elements = $('.task:nth-child(2)').offset().top - $('.task:nth-child(1)').offset().top;
        height_of_elements = $('.task:nth-child(1)').height();
        dy = (distance_between_elements * ( $('.task').size() - 1 )) + height_of_elements/2;
    
        first = $('.task:first');
        first.simulate('drag', {dx:0, dy:dy});
      });
    }                 
    
        4
  •  0
  •   codener    9 年前

    #drag_to 但它的力量似乎是有限的。

    为了向下移动一个UI可排序表行,我必须创建一个包含三行的表,然后运行这个cumber步骤:

    # Super-primitive step
    When /^I drag the first table row down$/ do
      element = find('tbody tr:nth-child(1)')
      # drag_to needs to drag the element beyond the actual target to really perform
      # the reordering
      target = find('tbody tr:nth-child(3)')
    
      element.drag_to target
    end
    

    这会把第一排换成第二排。我的解释是水豚拖得不够远,所以我给了它一个超出我实际目标的目标。

    tolerance: 'pointer '.

        5
  •  0
  •   henry    6 年前

    我遵循了@codener的解决方案,它成功了!我在代码中唯一更改的是用 tolerance: 'pointer'

    @codener的答案中描述的限制也没有消失。(我使用的是capybara 2.18.0)不需要第三行就可以将第一行和第二行交换。

    When /^I drag the first table row down$/ do
      element = find('tbody tr:nth-child(1)')
      target = find('tbody tr:nth-child(2)')
      element.drag_to target
    end