Cypress 获取table内容动态tr和td

2020-07-27 09:44:36 浏览数 (1)

前言

如何获取table内容动态tr和td的dom对象,今天给大家总结了方法。

获取table中DOM元素方法总结

代码语言:javascript复制
<table id="projectstatus" class="sortable pane bigtable stripped-odd"><tbody><tr class="header"><th tooltip="上次构建状态" title="上次构建状态"><a href="#" class="sortheader">&nbsp;&nbsp;S<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a></th><th tooltip="构建晴雨表" title="构建晴雨表"><a href="#" class="sortheader">&nbsp;&nbsp;W<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a></th><th initialsortdir="down"><a href="#" class="sortheader">名称<span class="sortarrow">&nbsp;&nbsp;↓</span></a></th><th><a href="#" class="sortheader">上次成功<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a></th><th><a href="#" class="sortheader">上次失败<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a></th><th><a href="#" class="sortheader">上次持续时间<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a></th><th width="1"><a href="#" class="sortheader">&nbsp;<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a></th><th><a href="#" class="sortheader">&nbsp;<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a></th></tr><tr id="job_FCA玛莎拉蒂自动化测试" class=" job-status-blue"><td data="4"><img src="/static/ecc2f937/images/24x24/blue.png" alt="成功" tooltip="成功" style="width: 24px; height: 24px; " class="icon-blue icon-md" title="成功"></td><td data="100" class="healthReport" onmouseover="this.className='healthReport hover';return true;
        " onmouseout="this.className='healthReport';return true;"><a href="job/FCA玛莎拉蒂自动化测试/lastBuild" class="build-health-link"><img src="/static/ecc2f937/images/24x24/health-80plus.png" alt="100%" style="width: 24px; height: 24px; " class="icon-health-80plus icon-md"></a><div class="healthReportDetails"><table border="0"><thead><tr><th align="left">W</th><th align="left">描述</th><th align="right">%</th></tr></thead><tbody><tr><td align="left"><img src="/static/ecc2f937/images/16x16/health-80plus.png" style="width: 16px; height: 16px; " class="icon-health-80plus icon-sm"></td><td>构建稳定性: 最近没有失败的构建。</td><td align="right">100</td></tr></tbody></table></div></td><td><a href="job/FCA玛莎拉蒂自动化测试/" class="model-link inside">1</a></td><td data="2020-07-17T02:00:14Z">
        2 小时 56 分
        - <a href="job/FCA玛莎拉蒂自动化测试/lastSuccessfulBuild/" class="model-link inside">#26</a></td><td data="2020-07-15T01:48:39Z">
        2 天 3 小时
        - <a href="job/FCA玛莎拉蒂自动化测试/lastFailedBuild/" class="model-link inside">#7</a></td><td data="1767243">29 分</td><td><a href="job/FCA玛莎拉蒂自动化测试/build?delay=0sec"><img src="/static/ecc2f937/images/24x24/clock.png" onclick="return build_id2549(this)" alt="Schedule a 构建 for1" style="width: 24px; height: 24px; " title="Schedule a 构建 for FCA玛莎拉蒂自动化测试" class="icon-clock icon-md"></a><script>function build_id2549(img) {
                  new Ajax.Request(img.parentNode.href);
                  hoverNotification('构建 scheduled', img, -100);
                  return false;
                }</script></td><td>&nbsp;</td></tr><tr id="job_2" class=" job-status-blue"><td data="4"><img src="/static/ecc2f937/images/24x24/blue.png" alt="成功" tooltip="成功" style="width: 24px; height: 24px; " class="icon-blue icon-md" title="成功"></td><td data="40" class="healthReport" onmouseover="this.className='healthReport hover';return true;
        " onmouseout="this.className='healthReport';return true;"><a href="job/一汽数据运营接口/lastBuild" class="build-health-link"><img src="/static/ecc2f937/images/24x24/health-20to39.png" alt="40%" style="width: 24px; height: 24px; " class="icon-health-20to39 icon-md"></a><div class="healthReportDetails"><table border="0"><thead><tr><th align="left">W</th><th align="left">描述</th><th align="right">%</th></tr></thead><tbody><tr><td align="left"><img src="/static/ecc2f937/images/16x16/health-20to39.png" style="width: 16px; height: 16px; " class="icon-health-20to39 icon-sm"></td><td>构建稳定性: 最近 5 次构建中有 3 次失败。</td><td align="right">40</td></tr></tbody></table></div></td><td><a href="job/一汽数据运营接口/" class="model-link inside">2</a></td><td data="2020-06-12T02:16:03Z">
        1 月 5 天
        - <a href="job/一汽数据运营接口/lastSuccessfulBuild/" class="model-link inside">#16</a></td><td data="2020-06-12T01:58:43Z">
        1 月 5 天
        - <a href="job/一汽数据运营接口/lastFailedBuild/" class="model-link inside">#14</a></td><td data="18773">18 秒</td><td><a href="job/一汽数据运营接口/build?delay=0sec"><img src="/static/ecc2f937/images/24x24/clock.png" onclick="return build_id2550(this)" alt="Schedule a 构建 for 一汽数据运营接口" style="width: 24px; height: 24px; " title="Schedule a 构建 for 一2" class="icon-clock icon-md"></a><script>function build_id2550(img) {
                  new Ajax.Request(img.parentNode.href);
                  hoverNotification('构建 scheduled', img, -100);
                  return false;
                }</script></td><td>&nbsp;</td></tr><tr id="job_3" class=" job-status-blue"><td data="4"><img src="/static/ecc2f937/images/24x24/blue.png" alt="成功" tooltip="成功" style="width: 24px; height: 24px; " class="icon-blue icon-md" title="成功"></td><td data="100" class="healthReport" onmouseover="this.className='healthReport hover';return true;
        " onmouseout="this.className='healthReport';return true;"><a href="job/长城车主卡上报模块接口/lastBuild" class="build-health-link"><img src="/static/ecc2f937/images/24x24/health-80plus.png" alt="100%" style="width: 24px; height: 24px; " class="icon-health-80plus icon-md"></a><div class="healthReportDetails"><table border="0"><thead><tr><th align="left">W</th><th align="left">描述</th><th align="right">%</th></tr></thead><tbody><tr><td align="left"><img src="/static/ecc2f937/images/16x16/health-80plus.png" style="width: 16px; height: 16px; " class="icon-health-80plus icon-sm"></td><td>构建稳定性: 最近没有失败的构建。</td><td align="right">100</td></tr></tbody></table></div></td><td><a href="job/长城车主卡上报模块接口/" class="model-link inside">3</a></td><td data="2020-07-17T02:30:49Z">
        2 小时 25 分
        - <a href="job/长城车主卡上报模块接口/lastSuccessfulBuild/" class="model-link inside">#55</a></td><td data="2020-07-14T08:29:55Z">
        2 天 20 小时
        - <a href="job/长城车主卡上报模块接口/lastFailedBuild/" class="model-link inside">#50</a></td><td data="26883">26 秒</td><td><a href="job/长城车主卡上报模块接口/build?delay=0sec"><img src="/static/ecc2f937/images/24x24/clock.png" onclick="return build_id2551(this)" alt="Schedule a 构建 for 3" style="width: 24px; height: 24px; " title="Schedule a 构建 for3" class="icon-clock icon-md"></a><script>function build_id2551(img) {
                  new Ajax.Request(img.parentNode.href);
                  hoverNotification('构建 scheduled', img, -100);
                  return false;
                }</script></td><td>&nbsp;</td></tr></tbody></table>

1.获取动态tr个数

代码语言:javascript复制
describe('处理table方法总结', function () {
    beforeEach(function () {
        var username = "admin"
        var password = ""
        //登陆地址
        cy.visit('http://xx.xx.xxx.xx:xxxx/login?from=/')
        cy.get('#j_username').type(username)
        cy.get('body > div > div > form > div:nth-child(2) > input').type(password)
        cy.get('body > div > div > form > div.submit.formRow > input').click()

    })
    it('动态获取tr', function () {
        cy.get('#projectstatus > tbody >tr').then(($el) => {
            const tr_total = $el.length
            cy.log(tr_total)
        })
    });})

2.获取动态tr去断言td内容

代码语言:javascript复制
describe('处理table方法总结', function () {
    beforeEach(function () {
        var username = "admin"
        var password = ""
        //登陆地址
        cy.visit('http://xx.xx.xxx.xx:xxxx/login?from=/')
        cy.get('#j_username').type(username)
        cy.get('body > div > div > form > div:nth-child(2) > input').type(password)
        cy.get('body > div > div > form > div.submit.formRow > input').click()

    })
    it('动态获取tr', function () {
        cy.get('#projectstatus > tbody >tr').then(($el) => {
            const tr_total = $el.length
            cy.log(tr_total)
            var list =['29 分','18 秒',"26 秒"]
             for (var i = 1; i < tr_total; i  ) {
                cy.get('#projectstatus > tbody >tr').eq(i).find('td:nth-child(6)')
                    .should('contain', list[i-1])
            }
        })
    });})

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家的支持。

更多文章关注小编公众号:自动化测试 To share

0 人点赞