如何将页面中的字符串数组与 cucumber/capybara 中的 table 文本匹配
How can I match the array of strings from a page to the table text in 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
我只想将 cucumber 特征文件的 table 元素文本与我从页面获得的文本相匹配。
您需要将字符串转换为数组。
尝试这样的事情,虽然我不确定那些转义序列是什么:
value = table.lines.map{ |l| l.match(/\|(.*)\|/)[1].strip }
将 tables 传递给黄瓜步骤时,table 作为黄瓜数据传递table。可以通过调用 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
这段代码对我有用:
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
我用黄瓜写了这个场景:
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
我只想将 cucumber 特征文件的 table 元素文本与我从页面获得的文本相匹配。
您需要将字符串转换为数组。
尝试这样的事情,虽然我不确定那些转义序列是什么:
value = table.lines.map{ |l| l.match(/\|(.*)\|/)[1].strip }
将 tables 传递给黄瓜步骤时,table 作为黄瓜数据传递table。可以通过调用 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
这段代码对我有用:
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