我要验证特定css值的文本。
HTML是 -
<table class="resp_card_table company-dashboard">
<thead>...</thead>
<tbody>
<tr ng-repeat="company in company | filter:searchString" class="ng-scope">
<td data-label="Company Name" class="ng-binding">ABC Constructions</td>
</tr>
</tbody>
</table>
我要验证“公司名称”是否有文字“ABC Constructions”
如何使用expect语句执行此操作?
从Capybara回购:https://github.com/teamcapybara/capybara#using-capybara-with-rspec
...视图规范也支持Capybara匹配器:
RSpec.describe "todos/show.html.erb", type: :view do it "displays the todo title" do assign :todo, Todo.new(title: "Buy milk") render expect(rendered).to have_css("header h1", text: "Buy milk") end end
鉴于您的HTML,请尝试以下方法:
expect(rendered).to have_css(
'td[data-role="Company Name"]', text: 'ABC Constructions'
)
如果您尝试在视图规范中执行此操作,则aridlehoover的答案将是正确的。不过,在一个功能/系统规范中(你通常会在其中使用含有水豚的硒)那么它就是
expect(page).to have_css('td[data-role="Company Name"]', text: 'ABC Constructions')
请注意,text选项将(默认情况下)匹配子字符串(包含而不是equals),因此它也会匹配具有“ABC Constructions Company”文本内容的元素。如果你想完全匹配'ABC Constructions'那么你就可以做到
expect(page).to have_css('td[data-role="Company Name"]', text: 'ABC Constructions', exact_text: true)
或者更简洁
expect(page).to have_css('td[data-role="Company Name"]', exact_text: 'ABC Constructions')