Rails 5.1与Capybara一起使用assert_select查找字符串

时间:2018-07-09 12:07:28

标签: capybara ruby-on-rails-5.1

我发现很难在Rails 5.1的敏捷Web开发中的练习的系统测试中确定assert_select的语法。无论我怎样尝试,都无法找到包含要搜索的字符串的网页元素。

检查器中显示的HTML代码如下:

<html>
  <head>
    <title>Pragprog Books Online Store</title>
    <meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token"         content="3FpWTUujgCw2iIsY9AjcvLuEd+SPVFT/zmlXfJKqETL8RJymxmnu1Moaqsz7hfhsnSToYDKBhzqk7ohzFzY6kg==" />

  </head>

  <body>
    <header class="main">
      <img alt="The Pragmatic Bookshelf" src="/assets/logo-a    8906fb6c933f00883d2ad0385ae52201c0e1d1f9235c37f4a5762f02ad80936.svg" />
      <h1></h1>
    </header>
    <section class="content">
      <nav class="side_nav">

        <div id="cart" class="carts">


        </div>

        <ul>
          <li><b href="/">Today is Mon 09 Jul 2018</b>
          <c href="/">12:31 pm</c></li>
          <li><a href="/">Home</a></li>
          <li><a href="/questions">Questions</a></li>
          <li><a href="/news">News</a></li>
          <li><a href="/contact">Contact</a></li>
        </ul>
      </nav>
      <main class='products'>
        <p id="notice">Product was successfully updated.</p>

我要的行是此代码的最后一行。

这是我到目前为止尝试过的:

assert_select 'body', text: "Product was successfully updated."

assert_select 'nav.main.products.notice', text: "Product was successfully updated."

assert_select "#notice"

assert_select "#notice", "Product was successfully updated."

page.find_field('notice' text: "Product was successfully updated")

assert_select "notice"

assert_select ‘id.notice’

assert_select "body", {:text=>"Product was successfully updated."}, "Nope!"

assert_select "body", "Product was successfully updated."

assert_select "body", {:text=>"Product was successfully updated."}

expect(page).to have_css("#notice", text: "Product was successfully updated.")

assert_text "Product was successfully created."

assert page.has_selector?("div#main", text: 'Product was successfully created')

assert_select '#notice'

assert_text "successfully"

assert select "div#notice", "Product was successfully created."

assert page.has_selector?(id="notice")

我得到的错误总是相同的:

Failure:
ProductsTest#test_creating_a_Product [/Users/Srjones/.rvm/gems/ruby-2.4.1/gems/capybara-3.2.1/lib/capybara/minitest.rb:105]:
expected to find visible select box "body" with text "Product was successfully updated." that is not disabled but there were no matches

有任何线索吗?

/ s

1 个答案:

答案 0 :(得分:0)

从链接到assert_select的文档中-“检查页面或当前节点是否具有选择字段...”-这告诉您它可以与您没有的<select>元素一起使用在此HTML中,因此它将永远无法工作。

如果您要尝试验证文本是否包含在ID为“ notice”的元素中,那么应该为您工作的是

assert_css('#notice', text: 'Product was successfully updated.')

简写为

assert_selector(:css, '#notice', text: 'Product was successfully updated.'  # Note selector - not select