如果想從頭學起Cypress,可以看下面的系列文章哦
https://www.cnblogs.com/poloyy/category/1768839.html
作用
獲取上一條命令結果的屬性值
語法格式
.its(propertyName)
.its(propertyName, options)
參數說明
propertyName:索引、屬性名、要獲取的嵌套屬性名稱
options:log、timeout
命令返回結果
屬性值
正確寫法
cy.wrap({ width: '50' }).its('width') // 獲取寬度屬性 cy.window().its('sessionStorage') // 獲取 sessionStorage 屬性
錯誤寫法
cy.its('window') // 不能鏈接在 cy 后面 cy.clearCookies().its('length') // clearCookies 並不返回對象
各種栗子
獲取字典對象的屬性值
cy.wrap({age: 52}).its('age').should('eq', 52) // true
數組對象,根據索引取值
cy.wrap(['polo', 'yy']).its(1).should('eq', 'yy')
獲取元素的屬性值
cy
.get('ul li') .its('length') .should('be.gt',4)
獲取字符串對象的屬性值
cy .url() .its('length') .should('be.gt', 20)
屬性值是函數
const fn = () => { return 42 } cy.wrap({getNum: fn}).its('getNum').should('be.a', 'function')
返回的是函數對象本身,而不是 return 的值
獲取嵌套屬性值
const user = { contacts: { work: { name: 'Kamil' } } } cy.wrap(user).its('contacts.work.name').should('eq', 'Kamil') // true