加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 如何编写黄瓜步骤来测试元素列表是否已排序

发布时间:2020-12-17 02:52:18 所属栏目:百科 来源:网络整理
导读:我需要编写一个黄瓜场景来测试项目列表是否按顺序排序(按名称).我有类似的东西: Scenario: Sort projects by name Given there is a project called "Project B" And there is a project called "Project A" And there is a project called "Project C" Giv
我需要编写一个黄瓜场景来测试项目列表是否按顺序排序(按名称).我有类似的东西:

Scenario: Sort projects by name
   Given there is a project called "Project B"
   And there is a project called "Project A"
   And there is a project called "Project C"
   Given I am on the projects page
   When I follow "Sort by name"
   Then I should see in this order ["Project A","Project B","Project C"]

我添加了一个步骤,看起来像:

Given /^I should see in this order ([.*])$/ do |array|

end

测试页面上列出的项目是否显示正确顺序的最佳方法是什么?我试图通过jQuery获取所有项目名称:

$(function() {
    var arrjs = new Array();
    $("div.project-main-info").find("a:first").each(function(){
        arrjs.push($(this).text());
    })
  });

并将它们放在一个数组中,与作为参数传递到此步骤的数组进行比较,但我不知道如何在此步骤中集成该jQuery代码!

谢谢!

编辑

正如McStretch所建议的那样,我试图通过以下方式使用XPath来获取锚点:

all('a').each do |a|
    if(//projects/d*/).match("#{a[:href]}")
        arr_page << "...." # Need to retrieve the value out of <a href="..">VALUE</a> but don't know how..any idea?
    end
  end

这是正确的方法吗?我只是测试了,不幸的是arr_page没有充满任何东西(我用[:href]替换了“…”部分只是为了测试)!实际上我试图检查[:href]的值(通过提高它),它是空白的!我怎样才能更好地检查我的锚点(鉴于href与上面提到的正则表达式匹配)?

解决方法

首先,最好将最后一步写成:

Then I should see the projects in this order:
  | Project A |
  | Project B |
  | Project C |

现在,您可以轻松地将列表作为数组访问,例如

expected_order = table.raw

然后,您需要将页面中的项目收集到一个数组中,正如@McStretch建议的那样:

actual_order = page.all('a.project').collect(&:text)

(这假设您的每个项目链接都有一个“项目”CSS类,以使测试更容易).

然后,您可以使用RSpec来比较两个阵列.

expected_order.should == actual_order

如果订单不正确,这将显示失败.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读