如何将页面中的字符串数组与cucumber / capybara中的表格文本进行匹配

时间:2017-05-10 10:28:05

标签: ruby rspec cucumber capybara

我用黄瓜写这个场景:

Then the following drawers should be present on the page
      |Stations  |
      |Categories|
      |Schedules |
      |My Radio  |
下面的

是我的步骤定义:

Then(/^the following drawers should be present on the page$/) do |table|

  value = Array.new
  value = "#{table}"
  stats = page.all(:css,'.radionav__panel-item').map(&:text)
 expect(value).to contain_exactly(stats)
end

当我打印stats时,我得到了这个结果:["Stations", "Categories", "Schedules", "My Radio"]

并且value得到了:

|Stations  |
|Categories|
|Schedules |
|My Radio  |

当我尝试匹配两者时,我得到错误说

    Then the following drawers should be present on the page # features/step_def
initions/radio_nav_steps.rb:14
      stats : ["Stations", "Categories", "Schedules", "My Radio"]
      | Stations   |
      | Categories |
      | Schedules  |
      | My Radio   |
      expected a collection that can be converted to an array with `#to_ary` or
`#to_a`, but got "\n  | \e[32m    Stations  \e[0m\e[0m |\e[0m\n  | \e[32m    Cat
egories\e[0m\e[0m |\e[0m\n  | \e[32m    Schedules \e[0m\e[0m |\e[0m\n  | \e[32m
   My Radio  \e[0m\e[0m |\e[0m\n" (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/radio_nav_steps.rb:22:in `/^the following draw
ers should be present on the page$/'
      features/radio_nav.feature:34:in `Then the following drawers should be pre
sent on the page'

Failing Scenarios:
cucumber features/radio_nav.feature:33 # Scenario: As a user I should see all th
e drawers

我只想将黄瓜特征文件的表格元素文本与我从页面获取的文本进行匹配。

3 个答案:

答案 0 :(得分:1)

将表格传递给黄瓜步骤时,表格作为黄瓜数据表传递。可以通过调用raw将其转换为数组。

Then(/^the following drawers should be present on the page$/) do |table|
  stats = page.all(:css,'.radionav__panel-item').map(&:text)
  expect(table.raw.flatten).to contain_exactly(stats)
end

答案 1 :(得分:0)

您需要将字符串转换为数组。

尝试这样的事情,虽然我不确定那些转义序列是什么:

value = table.lines.map{ |l| l.match(/\|(.*)\|/)[1].strip }

答案 2 :(得分:0)

此代码适用于我:

 Then(/^the following drawers should be present on the page$/) do |table|
  stats = page.all(:css,'.radionav__panel-item').map(&:text)
  data = table.hashes
  begin
    data.each do|row|
      row.each do |key, value|
        if stats.include? value
        else
          puts "element not found"
        end
      end
    end
  end
end