黄瓜测试表中元素的顺序

发布于 2024-10-01 11:47:19 字数 1719 浏览 3 评论 0原文

我想运行一个测试来测试元素的顺序 我希望订单按日期升序。

这是我的黄瓜特征场景和最后一句话的步骤。

Scenario: Order of the events should be Ascending
    Given I am signed into an account called "Gorilla Tech" as a customer
    When I follow "Register"
    And I follow "Events"
    And I follow "Register new event"
    Then I should see the header "Use the form below to register a new event"
    And I fill in "Title" with "Apefest 2010"
    And I fill in "Event at" with "2010-01-07"
    And I fill in "Add a note" with "This was truly awesome"
    Then I press "Create"
    Then I follow "Register new event"
    And I fill in "Title" with "Monkeyfest 2010"
    And I fill in "Event at" with "2010-01-08"
    And I fill in "Add a note" with "Yeah"
    Then "Apefest 2010" should appear before "Monkeyfest 2010"



   Then /"(.*)" should appear before "(.*)"/ do |first_example, second_example|
     response.body.should =~ /#{first_example}.*#{second_example}/
   end

我这里确实有两个问题。第一个是我如何正确指定我的测试?我可以指定上面的测试不同且更正确吗?

我想要做的是在 2 个不同的日期下注册 2 个不同的事件。该事件稍后将作为列表显示在网页上。然后我想检查事件是否按日期排序。我希望最新日期的活动出现在顶部。

这是我想要测试的集合的代码。在某种程度上,我想检查下面 div 中的集合是否按日期升序排列。

<div id="feeds">
    <table>
      <caption><%= t('events.all') %></caption>
      <thead>
        <tr>
          <th><%= Event.t(:title) %></th>
          <th><%= Event.t(:event_at) %></th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <%= render :partial => 'event', :collection => @events %>
      </tbody>
    </table>
  </div>

  <%= will_paginate(@events) %>

I want to run a test that tests the order of the elements
I want the order to be ascending by date.

Here is my cucumber feature scenario and the step for the last sentence.

Scenario: Order of the events should be Ascending
    Given I am signed into an account called "Gorilla Tech" as a customer
    When I follow "Register"
    And I follow "Events"
    And I follow "Register new event"
    Then I should see the header "Use the form below to register a new event"
    And I fill in "Title" with "Apefest 2010"
    And I fill in "Event at" with "2010-01-07"
    And I fill in "Add a note" with "This was truly awesome"
    Then I press "Create"
    Then I follow "Register new event"
    And I fill in "Title" with "Monkeyfest 2010"
    And I fill in "Event at" with "2010-01-08"
    And I fill in "Add a note" with "Yeah"
    Then "Apefest 2010" should appear before "Monkeyfest 2010"



   Then /"(.*)" should appear before "(.*)"/ do |first_example, second_example|
     response.body.should =~ /#{first_example}.*#{second_example}/
   end

I really have two problems here. The first one is how do i specify my test correctly? Can i specify my test above different and more correct?

What i want to do is to register 2 different events under 2 different dates. The event will later appear as a list on the webpage. Then i want to check if the events are in order by date . I want the event with the newest date to appear on the top.

here is the code with the collection i want to test. In some way i want to check if the collection in the div below has the an ascending order by date.

<div id="feeds">
    <table>
      <caption><%= t('events.all') %></caption>
      <thead>
        <tr>
          <th><%= Event.t(:title) %></th>
          <th><%= Event.t(:event_at) %></th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <%= render :partial => 'event', :collection => @events %>
      </tbody>
    </table>
  </div>

  <%= will_paginate(@events) %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神也荒唐 2024-10-08 11:47:19

您编写步骤定义的方式很好,您只需将多行 (m) 开关添加到您的模式中即可。 :)

response.body.should =~ /#{first_item}.*#{second_item}/m

The way you wrote your step definition is fine you just need to add the multiline (m) switch to your pattern. :)

response.body.should =~ /#{first_item}.*#{second_item}/m
梦萦几度 2024-10-08 11:47:19

我也许会建议使用表格对您的步骤进行一些整合。

Scenario: Order of the events should be Ascending
...
And I fill in "Title" with "Apefest 2010"
And I fill in "Event at" with "2010-01-07"
And I fill in "Add a note" with "This was truly awesome"
Then I press create

我建议:

Scenario: Order of the events should be Ascending
...
When I register a new event:
| Title | Event at | Add a note |
| Apefest 2010 | 2010-01-07 | This was truly awesome |

您当前的解决方案的缺点是不太精确,在整个响应正文中您可能会在另一位置找到一个示例中的文本。我建议使用 XPATH 达到您的疼痛阈值,或者使用(我假设您使用 Watir)(或变体)类。

您可以通过迭代行(并指定包含事件名称的列;这里我假设第一个)来从表中收集所有事件名称...

event_names = $browser.div(:id,"feeds").tables.first.rows.collect {|row| row[0].text}

然后断言事件存在并且它们在数组。

event_names.index(first_example).should_not be_nil
event_names.index(second_example).should_not be_nil
event_names.index(first_example).should < event_names.index(second_example)

I would perhaps propose some consolidation to your steps by using a table.

Scenario: Order of the events should be Ascending
...
And I fill in "Title" with "Apefest 2010"
And I fill in "Event at" with "2010-01-07"
And I fill in "Add a note" with "This was truly awesome"
Then I press create

I would suggest:

Scenario: Order of the events should be Ascending
...
When I register a new event:
| Title | Event at | Add a note |
| Apefest 2010 | 2010-01-07 | This was truly awesome |

Your current solution has the trade off of not being very precise, looking in the entire response body you may find text from one example in another location. I would suggest the use of XPATH up to your pain threshold or utilizing, I am assuming your using Watir (or variant), the classes.

You could collect all the event names from the table by iterating over the rows (and specifying the column that contains the event name; here I assume the first one)...

event_names = $browser.div(:id,"feeds").tables.first.rows.collect {|row| row[0].text}

Then assert that the events exist and they are in the desired order within the array.

event_names.index(first_example).should_not be_nil
event_names.index(second_example).should_not be_nil
event_names.index(first_example).should < event_names.index(second_example)
心的憧憬 2024-10-08 11:47:19

为什么不直接使用 nth-child 选择器呢?

page.should have_selector('tbody tr:nth-child(1)', text: first_example)
page.should have_selector('tbody tr:nth-child(2)', text: second_example)

Why not just use the nth-child selector?

page.should have_selector('tbody tr:nth-child(1)', text: first_example)
page.should have_selector('tbody tr:nth-child(2)', text: second_example)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文