【问题标题】:Cypress if compare object赛普拉斯如果比较对象
【发布时间】:2021-01-31 07:37:48
【问题描述】:

(.pd-incidents-table tbody) 有随机元素; 我需要检查 (.pd-incidents-table tbody) 的每个元素的每个细节单元格; 它工作正常,但如果断言匹配/失败,我需要做一些事情

例如: 如果 details-cell element[3] 与 "BOXER123" 匹配,需要点击一些东西 否则什么都不做

当前行为是:即使匹配或失败 cy.get(btn).click() 也不执行

   let found = false
   const timeout = 11 
   for(let i = 1; i<timeout && !found ;i++){
    cy.get(`.pd-incidents-table tbody >`).each((el, index) => {
      cy.get(`.pd-incidents-table > table > tbody > :nth-child(${index+1}) .details-cell`).invoke('text').then( text => {
         if(softExpect(text).to.contains('BOXER123')){
           cy.get(btn).click()
          }

【问题讨论】:

  • 你能在 if 语句中做一个cy.log() 来检查控件是否进入了 if 块吗?
  • 请说明btn是如何设置的,选择器是什么?

标签: javascript cypress


【解决方案1】:

即使匹配或失败,cy.get(btn).click() 也不会执行 - 这可能是 btn 的问题。

首先要做的就是简化并去掉所有的嵌套。

else do nothing - 表示您不需要if,只需选择匹配的元素并单击即可。

多个单元格有文字

// Look for cells containing "BOXER123"
const cellSelector = '.pd-incidents-table td.details-cell:contains("BOXER123")';

cy.get(cellSelector).each(cell => {
  cy.wrap(cell).find('button').click();   // wrap + find gets just the button in the cell
});

数据是随机的,所以如果您遇到的情况是 可能不是 带有文本的单元格并且您不想通过测试,那么 p>

// Look for cells containing "BOXER123"
const cellSelector = '.pd-incidents-table td.details-cell:contains("BOXER123")';

if (Cypress.$(cellSelector).length) {     // check that any cells exist

  cy.get(cellSelector).each(cell => {
    cy.wrap(cell).find('button').click(); // wrap + find gets just the button in the cell
  });

}

单个单元格有文字

我只想要第一个带有文本“BOXER123”的单元格,那么它可以更简单

contains()

.contains() 产生它找到的新 DOM 元素

// Look for cells containing "BOXER123"
const cellSelector = '.pd-incidents-table td.details-cell';

cy.contains(cellSelector, 'BOXER123').within(cell => {  // within the cell
  cy.get('button').click(); 
});

【讨论】:

    【解决方案2】:

    您可以通过两种方式做到这一点:

    1.使用cy.contains()。它获取包含文本的 DOM 元素。假设 BOXER123 是您表中的唯一文本。

    let found = false
    const timeout = 11
    for (let i = 1; i < timeout && !found; i++) {
      cy.get(`.pd-incidents-table tbody >`).each((el, index) => {
        cy.get(`.pd-incidents-table > table > tbody > :nth-child(${index+1}) .details-cell`).invoke('text').then(text => {
          if (softExpect(text).to.contains('BOXER123')) {
            cy.contains(text).click()
          }
        })
      })
    }
    

    2.使用cy.wrap()。它产生传递给.wrap() 的对象。如果对象是一个 Promise,则产生它的解析值。

    let found = false
    const timeout = 11
    for (let i = 1; i < timeout && !found; i++) {
      cy.get(`.pd-incidents-table tbody >`).each((el, index) => {
        cy.get(`.pd-incidents-table > table > tbody > :nth-child(${index+1}) .details-cell`).then($ele => {
          if ($ele.text().trim() == 'BOXER123') {
            cy.wrap($ele).click()
          }
        })
      })
    }
    

    【讨论】:

    • @eric99 是的,因为 OP 使用 if 条件检查文本是否存在,我假设文本在表中出现一次。无论如何,我已将此添加到答案中,即文本必须是唯一的。
    • 是的,很难说他的意图是什么——例如timeoutfound的目的是什么?我猜这些表明只需要 first 单元格(尽管他从未设置 found)。
    猜你喜欢
    • 1970-01-01
    • 2020-01-26
    • 2020-06-25
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    相关资源
    最近更新 更多