首页 > 解决方案 > 表元素断言

问题描述

我试图断言 11223 是否显示在桌子上。

元素如下:

<div class="page-wrapper table-responsive" id="shipping-option" xpath="1">
<table class="table table-bordered">
   <tbody>
      <tr>
         <th></th>
         <th class="checkUncheck">
            <span title="" data-toggle="tooltip" data-placement="top" data-original-title="Check / Uncheck All">
            <input type="checkbox" id="checkAll" class="icheckbox_square-green form-control">
            </span>
         </th>
         <th>
            Date/ID
         </th>
         <th>
            Name(pet name)
         </th>
         <th>
            Country/State
         </th>
         <th>
            Designation
         </th>
      </tr>
      <tr class="rowclick" style="cursor:pointer">
         <td>
            1
         </td>
         <td class="shipmentCheckbox">
            <input class="GroupChb" id="61577" name="item.isSelected" type="checkbox" value="true"><input name="item.isSelected" type="hidden" value="false">
         </td>
         <td>
            06-01-2021
            <br>
            <a href="http://www.google.dk" target="popup" onclick="window.open('http://www.google.dk'),'popup','width=600,height =600'); return false;">
            11223
            </a>
         </td>
         <td>
            Jonny
            <br>
            <span class="sub">
            Joe
            </span>
         </td>
         <td>
            US <br>
            <span class="sub">California</span>
         </td>
         <td>
            Software Quality Analyst
            <br>
            <span class="sub">
            QA
            </span>
         </td>
      </tr>
      <tr>
         <td colspan="10"></td>
      </tr>
   </tbody>
</table>
<div>

这是行不通的。我收到以下错误:

-包含 tr.rowclick,11223

重试超时:预计在元素中找到内容:“11223”:<table.table.table-bordered> 并使用选择器:“tr.rowclick”,但从未找到。赛普拉斯/集成/History.js:55:65 53 |

    cy.get('#SearchString').type('11223')
  54 |                 cy.get('#filter').click()
> 55 |                 cy.get('.page-wrapper').find('.table-bordered').contains('tr.rowclick','11223')
     |                                                                 ^
  56 |                
  57 |             })
  58 |         })

标签: javascriptcypressassert

解决方案


<td>没有 rowclick 类,有<tr>它。

这是我检查“11223”是否在单元格中的首选命令:

cy.contains('tr.rowclick td', '11223')

请注意,将搜索所有<td>子元素(如<a>)。

这是我喜欢的一种极简主义方法,因为对页面结构的更改可能会破坏测试(个人偏好)。


推荐阅读