我在柏树中有以下代码:
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:
<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>发布于 2021-10-13 22:30:54
您有大量的nth:child()和eq(),因此很难跟踪位置。使用<tr>和<td>选择器会更清楚一些。
我就是这样接近它的
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
}
})
})假设
如果click()对DOM进行更改,则不能使用cy.get('tr').each(...
发布于 2021-10-13 11:57:30
Cypress将每个“应该”作为断言,因此如果“如果”失败,您的测试就会失败。如果这是您想要的行为,那么您应该让每个行为分别如下:
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“工作,您需要的是访问命令产生的结果。只有在那里你才能做些比较
所以如果你这么做
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
发布于 2021-10-13 18:28:42
你可以做这样的事。
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。类似于:
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()
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
}
})https://stackoverflow.com/questions/69554607
复制相似问题