首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >柏树是否支持多重检查条件?

柏树是否支持多重检查条件?
EN

Stack Overflow用户
提问于 2021-10-13 11:22:13
回答 3查看 678关注 0票数 1

我在柏树中有以下代码:

代码语言:javascript
运行
复制
    for(let i = 0; i < lengthOfRow; i++){
        if (
            Cypress.$(':nth-child(1) > :nth-child(6) > .index_textBlock_20DCh').eq(0).text().length == 0 &&
            Cypress.$(':nth-child(1) > :nth-child(7) > .index_textBlock_20DCh').eq(1).text().length == 0 &&
            Cypress.$(':nth-child(1) > :nth-child(8) > .index_textBlock_20DCh').eq(2).text().length == 0
          ) {
            //Do Something
            cy.get('tbody > :nth-child(1) > :nth-child(6)').click()
          } else if (
            Cypress.$(':nth-child(2) > :nth-child(6) > .index_textBlock_20DCh').eq(0).text().length == 0 &&
            Cypress.$(':nth-child(2) > :nth-child(7) > .index_textBlock_20DCh').eq(1).text().length == 0 &&
            Cypress.$(':nth-child(2) > :nth-child(8) > .index_textBlock_20DCh').eq(2).text().length == 0
          ){
            //Do Something
            cy.get('tbody > :nth-child(2) > :nth-child(6)').click() }
//another more conditions

在这里,我正在检查块为空(否则转到elseIf),但是它一开始已经失败了(错误:应该是空的,但是找到了一些东西)。那么,我的问题是,这个代码有什么问题,柏树是否支持使用&&?

HTML:

代码语言:javascript
运行
复制
<tr class = ="index_tr_2P">
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">AnyText</div></td>
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">SomeText</div></td>
<td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">MoreTextHere</div></td>
</tr>

<tr class = ="index_tr_2P">
    <td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">AnyText2</div></td>
    <td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">SomeText2</div></td>
    <td class="index___td_halign_left_woynx index___td_1fTgv index__with-details_1OZkV index_tdHovered_1ZOPR index__has-background_1ZgcC"><div class="index_textBlock_20DCh">MoreTextHere2</div></td>
    </tr>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-10-13 22:30:54

您有大量的nth:child()eq(),因此很难跟踪位置。使用<tr><td>选择器会更清楚一些。

我就是这样接近它的

代码语言:javascript
运行
复制
cy.get('tr').each(($tr, rowIndex) => {    // look at each row individually

  cy.wrap($tr)
    .find('td')                           // all cols inside this row
    .invoke('slice', 5, 8)                // filter to last 3 cols only
    .invoke('text')                       // get all the text in one lump
    .then(text => {
      if (text.trim() === '') {           // after trim(), is all text empty?
        console.log(`Row ${rowIndex+1} has three empty cols`) 
        cy.wrap($tr).find('td').eq(5).click()  // click the 6th col
      }
    })
})

假设

  • 您想要测试的三个cols在每一行
  • 单击第6个cols不移除任何行

如果click()对DOM进行更改,则不能使用cy.get('tr').each(...

票数 1
EN

Stack Overflow用户

发布于 2021-10-13 11:57:30

Cypress将每个“应该”作为断言,因此如果“如果”失败,您的测试就会失败。如果这是您想要的行为,那么您应该让每个行为分别如下:

代码语言:javascript
运行
复制
cy.get('body').then((body) => {
  cy.get(':nth-child(1) > :nth-child(6) > .index_textBlock_20DCh').should('be.empty')
cy.get(':nth-child(1) > :nth-child(7) > .index_textBlock_20DCh').should('be.empty')
cy.get(':nth-child(1) > :nth-child(8) > .index_textBlock_20DCh').should('be.empty')
cy.get('tbody > :nth-child(1) > :nth-child(6)').click()

//here continue with whatever was in your "else" without writing else

在前一个条件为真或失败后,Cypress将执行每个cy.get。

现在,要使"if“工作,您需要的是访问命令产生的结果。只有在那里你才能做些比较

所以如果你这么做

代码语言:javascript
运行
复制
cy.get(':nth-child(1) > :nth-child(6) > .index_textBlock_20DCh').then((actualValue) => {
if(actualValue === ""){
} else {
}
})

那就行了

这里是与变量相关的柏树文档。别名部分对于您的多个条件if语句也有帮助。

https://docs.cypress.io/guides/core-concepts/variables-and-aliases

票数 1
EN

Stack Overflow用户

发布于 2021-10-13 18:28:42

你可以做这样的事。

代码语言:javascript
运行
复制
if (
  Cypress.$('td div.index_textBlock_20DCh').eq(0).is(':empty') &&
  Cypress.$('td div.index_textBlock_20DCh').eq(1).is(':empty') &&
  Cypress.$('td div.index_textBlock_20DCh').eq(2).is(':empty')
) {
  //Do Something
} else {
  //Do Something
}

您还可以提取文本并检查文本长度是否为0。类似于:

代码语言:javascript
运行
复制
if (
  Cypress.$('td div.index_textBlock_20DCh').eq(0).text().length == 0 &&
  Cypress.$('td div.index_textBlock_20DCh').eq(1).text().length == 0 &&
  Cypress.$('td div.index_textBlock_20DCh').eq(2).text().length == 0
) {
  //Do Something
} else {
  //Do Something
}

使用each()

代码语言:javascript
运行
复制
var countWithoutText = 0
cy.get('td div.index_textBlock_20DCh')
  .each((ele, lis) => {
    if (ele.text().length == 0) {
      countWithoutText++
    }
  })
  .then((lis) => {
    if (lis == countWithoutText) {
      //Do something all elements are empty
    } else {
      //Do something when all elements are not empty
    }
  })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69554607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档