為了避免再次發瘋,有沒有可能得到htmlTemplate這個元素的值?
<rect x="303" y="28" height="53" width="10" htmlTemplate="foo: 115" ></rect>
我想得到那個數字foo,所以只有數字 115
uj5u.com熱心網友回復:
如果您只想斷言該值,foo: 115則可以執行此操作。
cy.get('rect').invoke('attr', 'htmlTemplate').should('equal', 'foo: 115')
現在,如果要提取數字部分,可以執行以下操作:
cy.get('rect')
.invoke('attr', 'htmlTemplate')
.then((val) => {
cy.log(val.split(' ')[1]) //prints 115
expect(val.split(' ')[1]).to.equal('115')
})
如果你有多個 rect 并且你想訪問第一個你可以使用 eq(0)
cy.get('rect').eq(0).invoke('attr', 'htmlTemplate').should('equal', 'foo: 115')
如果你想獲得所有矩形的值,你可以使用each(),比如:
cy.get('rect').each(($ele) => {
cy.wrap($ele)
.invoke('attr', 'htmlTemplate')
.then((val) => {
cy.log(val.split(' ')[1]) //prints value one by one
})
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/365014.html
