我正在嘗試使用 Cypress 查找元素文本。$ All 與 cy 物件配合良好,但 Cypress.$ 產生未定義
//這行得通
cy.get('abc').eq(0).find('xyz').then(($elm)=>{
cy.log("$elm=" $elm.text())
})
//這會產生“未定義”
var elmText = Cypress.$('abc').eq(0).find('xyz').text()
cy.log(elmText)
有人可以幫助為什么 Cypress.$ 在這里不起作用嗎?
uj5u.com熱心網友回復:
我只能猜測您有一些 cypress 命令必須在查詢選擇器之前完成。由于 cypress 命令是異步的,因此執行 jquery 時您的 UI 狀態尚未準備好。因此,您可以使其與 cypress 命令同步執行,如下所示:
cy.then(() => {
var elmText = Cypress.$('abc').eq(0).find('xyz').text()
cy.log(elmText)
})
uj5u.com熱心網友回復:
@MikhailBolotov 是正確的,Cypress.$與命令相同的塊中的同步代碼首先運行。
試試這個簡單的訪問和航向檢查。
it('asynchronous commands', () => {
cy.visit('http://example.com')
.then(() => console.log('After visit'))
var elmText = Cypress.$('h1').text()
cy.log(elmText)
console.log('At end of test')
})
console.logs 的順序是
- 在測驗結束時
- 參觀后
除了@MikhailBolotov'a 的建議外,您還可以使用鉤子beforeEach來分解塊并實作您想要的。
beforeEach(() => {
cy.visit('http://example.com')
.then(() => console.log('After visit'))
})
it('asynchronous commands', () => {
var elmText = Cypress.$('h1').text()
cy.log(elmText)
console.log('At end of test')
})
console.logs 的順序現在是正確的
- 參觀后
- 在測驗結束時
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439718.html
標籤:javascript jQuery 柏
