我试图通过表格上的链接列表,并确保下一页有正确的URL,但遇到了问题.一个问题是没有好的类名可用,所以我一直使用cy.xPath.

//Loop through each element (This is a dynamic amount of elements)
cy.xpath('//span[text()="Id"]//following::a[contains(@href,"maps")]'.each($el) => { 
  cy.get($el).then(($btn) => {
    let id_text = $btn.text()
    
    //Check that the element is visible and click on it
    cy.get($el)
      .should('be.visible')
      .click()

    //Check that the url contains the text value of the element that was clicked on
    cy.url()
      .should('contain', id_text)
    
    })
  })

它只工作了一次,然后就出错了,说DOM元素分离了

推荐答案

找到了一个解决方案,它可能(可能不是)最好的方法,但嘿,它奏效了!这将遍历我需要的列中的所有数字,进入下一页,判断URL是否包含它所点击的数字,然后返回.

      //Get length of table
      cy.get('.MuiTableBody-root')
        .find('tr')
        .then((row) => {
          let num_rows = row.length
          for(let i=1; i < num_rows; i++){
            cy.get(`:nth-child(${i}) > .column-id > a`).then(($btn) => {
              let id_txt = $btn.text()
          
              //Click on ID Number
              cy.get(`:nth-child(${i}) > .column-id > a`)
                  .click()

              //Check that the url has the id_txt
              cy.url()
                  .and('contain',id_txt)

              cy.go(-1) //Go back 
            }) //Ends the Numbers of IDs loop
          } //Ends for loop
        }) //Ends the then((row)) loop

Javascript相关问答推荐

jest使用useLocate测试自定义挂钩

复合密钥查找在mongoDB中未按预期工作

在JavaScript中打开和关闭弹出窗口

Mongodb拥有5亿个文档,我想根据JavaScript驱动程序中的两个字段使用regEx进行搜索,而不是模式

Angular:ng-contract未显示

具有相同参数的JS类

将字符串UTC Date转换为ngx—counting leftTime配置值的数字

分层树视图

如何在模块层面提供服务?

setcallback是什么时候放到macrotask队列上的?

并不是所有的iframe都被更换

提交链接到AJAX数据结果的表单

当使用';字母而不是与';var#39;一起使用时,访问窗口为什么返回未定义的?

try 使用javascript隐藏下拉 Select

如何使用基于promise (非事件emits 器)的方法来传输数据?

当代码另有说明时,随机放置的圆圈有时会从画布上消失

删除元素属性或样式属性的首选方法

无法设置RazorPay订阅API项目价格

Node.js错误: node 不可单击或不是元素

重新呈现-react -筛选数据过多