【問題1】不支持多瀏覽器測試
【問題2】當元素不存在或者沒有找到時,測試會失敗
這個坑貌似聽起來很正確,但我們想一下這個場景:如果我們希望當某個元素不存在時,需要執行某個操作。但是因為以上默認的實現,沒有找到元素,所以會直接報錯。
或者某個元素剛開始沒有出現,必須將頁面滾動到底部,直到全部數據加載完后才出現,也會遇到問題。
有沒有方法解決?有 有 有!
利用jquery 查找元素的length是否大於0,然后利用if或while循環進行判斷。
if (Cypress.$('.show-more-button').length > 0) {
cy.get('.show-more-button a').click();
}
else{
do something
}/
有沒有方法解決?有 有 有!
使用cypress-promise這個庫 如上述代碼在返回最外層使用 promisify()方法,在使用ES7 promise語法 async await 就可以轉換成為異步操作。
return await promisify(Cypress.$(fxConfig[selector]).map(function () {
return Cypress.$(this).text()
}).get()))
}
【問題3】除了cy對象外的所有操作都是同步的
這就意味着類似以下代碼你必須用promise封裝,否則將會出現錯誤永遠拿不到正確值,因為Cypress.$
其實使用的是jquery對象,方法返回永遠都是同步。
getElementsText(selector) {
return Cypress.$(fxConfig[selector]).map(function () {
return Cypress.$(this).text()
})get()))
}
【問題4】CypressError: Timed out retrying: cy.type() failed because this element is readonly:
<input type="text" id="fstRegYm" name="fstRegYm" value="" readonly="readonly" class="in-pre" placeholder="請填寫注冊登記日期">
Fix this problem, or use {force: true} to disable error checking.
https://on.cypress.io/element-cannot-be-interacted-with
解決辦法:操作中加上參數{force: true}
【問題1】Uncaught TypeError: Cannot read property 'style' of undefined
This error originated from your application code, not from Cypress.
When Cypress detects uncaught errors originating from your application it will automatically fail the current test.
This behavior is configurable, and you can choose to turn this off by listening to the 'uncaught:exception' event.
https://on.cypress.io/uncaught-exception-from-application
解決辦法:忽略頁面本身存在的未處理異常,代碼如下
Cypress.Commands.add('exceptionIgnore', () => { cy.on('uncaught:exception', (err, runnable) => { expect(err.message).to.include(`Cannot read property 'style' of undefined`) // using mocha's async done callback to finish // this test so we prove that an uncaught exception // was thrown done() // return false to prevent the error from // failing this test return false }) }) --------- describe('Echannel moblie Test--shortTermTravelInsurance', () => { before(() => { cy.exceptionIgnore(); })