使用 capybara + selenium 验证特定 css 值的文本

Verify text of a particular css value using capybara + selenium

我要验证特定 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>

我要验证 "Company Name" 是否有文本 "ABC Constructions"

如何使用 expect 语句执行此操作?

来自 Capybara 存储库:https://github.com/teamcapybara/capybara#using-capybara-with-rspec

...Capybara matchers are also supported in view specs:

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 的答案将是正确的。但是,在 feature/system 规范中(您通常将 selenium 与水豚一起使用)那么它将是

expect(page).to have_css('td[data-role="Company Name"]', text: 'ABC Constructions')

请注意,文本选项将(默认情况下)匹配子字符串(包含而不是等于),因此它也会匹配文本内容为 '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')