【问题标题】:Verify text of a particular css value using capybara + selenium使用 capybara + selenium 验证特定 css 值的文本
【发布时间】:2019-05-23 11:00:48
【问题描述】:

我要验证特定 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 语句做到这一点?

【问题讨论】:

    标签: ruby selenium rspec capybara


    【解决方案1】:

    来自 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'
    )
    

    【讨论】:

      【解决方案2】:

      如果您尝试在视图规范中执行此操作,那么 aridlehoover 的答案将是正确的。但是,在功能/系统规范中(您通常将硒与水豚一起使用),那么它将是

      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')
      

      【讨论】:

        猜你喜欢
        • 2015-09-05
        • 1970-01-01
        • 2019-06-26
        • 2019-05-06
        • 2019-02-11
        • 2014-04-18
        • 1970-01-01
        • 1970-01-01
        • 2011-12-19
        相关资源
        最近更新 更多