我需要檢查一組標簽,這些標簽具有與之關聯的特定類并具有值
<div class="ratinglabel col-6">
<label>Area 1: Professional Engagement
<span test-data="area_1_Scor">
(4/16)
</span>
</label>
</div>
我想找到 (4/16*5) 并將它們保存在 var 中以將它們與另一個值進行比較
我這樣做了:
cy.get('[test-data="area_1_Scor"]', { timeout: 2000 })
.then(function($ele) {
var countOfElements = $ele*5;
cy.log(countOfElements);
});
})
和
cy.get('[test-data="area_1_Scor"]').each(($li, index, $lis) => {
var sum = 0;
Cypress.$('[test-data="area_1_Scor"]').each(function() {
sum = Cypress.$(this).text()*5||0;
});
cy.log("Total sum of all span elements:" sum);
})
但是日志在第一種情況下顯示為 NaN,在第二種情況下顯示為 0 那么我該怎么做呢?
uj5u.com熱心網友回復:
你快到了(第一個塊),但你需要 1)從元素中提取文本和 2)將文本決議為數字
cy.get('[test-data="area_1_Scor"]', { timeout: 2000 })
.invoke('text')
.then(text => {
const numerator = text.split('/')[0].replace('(', '')
const denominator = text.split('/')[1].replace(')', '')
const countOfElements = numerator * 5 / denominator;
cy.log(countOfElements);
});
})
您的第二個塊表示有多個元素,如果是這樣.each()使用
let sum = 0;
cy.get('[test-data="area_1_Scor"]', {timeout: 2000})
.each($el => {
cy.wrap($el).invoke('text')
.then(text => {
const numerator = text.split('/')[0].replace('(', '')
const denominator = text.split('/')[1].replace(')', '')
const countOfElements = numerator * 5 / denominator;
sum = countOfElements
})
})
})
.then(() => cy.log(sum))
測驗數學
const times5 = (text) => {
const numerator = text.split('/')[0].replace('(', '')
const denominator = text.split('/')[1].replace(')', '')
return Math.round(numerator * 5 / denominator)
}
let sum = 0;
cy.get('[test-data="area_1_Scor"]', {timeout: 2000})
.each($el => {
cy.wrap($el).invoke('text')
.then(times5) // text is passed in automatically
.then(result => sum = result)
})
})
.then(() => cy.log(sum))
所以你可以檢查功能
cy.log(times5("(4/16)"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465851.html
上一篇:無法訪問地圖
