我在沒有找到好的解決方案的情況下遇到了這個問題。我嘗試了在 Google 中找到的每篇文章,但都沒有成功。我正在隔離問題,以便我可以與您分享確切的觀點。我正在“mercadolibre.com”中搜索特定產品,我想將串列從較低的價格排序到最高的價格。為此,您可以直接
手動執行此操作很好,但是使用 Cypress 我無法做到這一點。這個腳本運行良好,但最后一步似乎沒有效果。
// Activate intelligence
/// <reference types="Cypress" />
describe("Main test suite for price control", () => {
it("search scanners and order them by ascending price", () => {
// Web access
cy.visitAndWait("https://listado.mercadolibre.com.ar/scanner-blueetooth#D[A:scanner blueetooth");
// Order by ascending price
cy.get(':nth-child(2) > .andes-list__item-first-column > .andes-list__item-text > .andes-list__item-primary').click({force: true})
});
});;
也許我使用了不好的方法來參考物件?
此致!
uj5u.com熱心網友回復:
您可能已經注意到有關關閉彈出視窗的禮貌討論,是否有必要。
我相信不會,并且依靠關閉彈出視窗將是一個不穩定的測驗。
我也認為問題正如@DizzyAl 所說,下拉按鈕上的事件處理程式直到頁面加載后期才附加。
這是我嘗試過的,使用重試為頁面加載提供更多時間。
Cypress.on('uncaught:exception', () => false)
before(() => {
cy.visit('http://mercadolibre.com.ar')
cy.get(".nav-search-input").type("scanner bluetooth para auto{enter}");
cy.get('.ui-search-result__wrapper') // instead of cy.wait(3000), wait for the list
})
it('gets the dropdown menu on 1st retry', {retries: 2}, () => {
// Don't dismiss the popups
cy.get('button.andes-dropdown__trigger').click()
cy.contains('a.andes-list__item', 'Menor precio').click()
// page reload happens
cy.contains('.ui-search-result__wrapper:nth-child(1)', '$490', {timeout:20000})
});
uj5u.com熱心網友回復:
我會重試選單,直到選項變得可見
Cypress.on('uncaught:exception', () => false)
before(() => {
cy.visit('https://listado.mercadolibre.com.ar/scanner-blueetooth#D[A:scanner blueetooth]')
})
it('retries the menu open command', () => {
function openMenu(attempts = 0) {
if (attempts > 6) throw 'Failed open menu'
return cy.get('button.andes-dropdown__trigger').click()
.then(() => {
const option = Cypress.$('.andes-list:visible') // is list visible
if (!option.length) {
openMenu( attempts) // try again, up to 6 attempts
}
})
}
openMenu().then(() => {
cy.get('a.andes-list__item:contains(Menor precio)').click()
})
// Verification
cy.contains('.ui-search-result__wrapper:nth-child(1)', '$490', {timeout:20000})
})
uj5u.com熱心網友回復:
看起來點擊事件被添加到下拉按鈕中,點擊失敗。
查看什么時候可以開始測驗?進行討論。
我嘗試修改文章中的代碼,但無法使其正常作業。
但是,為最后一個網路呼叫添加 acy.wait()或 acy.intercept()是可行的。
cy.intercept('POST', 'https://bam-cell.nr-data.net/events/**').as('events')
cy.visit('https://listado.mercadolibre.com.ar/scanner-blueetooth#D[A:scanner blueetooth]')
cy.wait('@events', {timeout: 20000})
// Page has loaded, verify the top item price
cy.contains('.ui-search-result__wrapper:nth-child(1)', '$1.385')
// Open the sort-by dropdown
cy.get('button.andes-dropdown__trigger').click()
// Choose sort-by lowest price
cy.contains('a.andes-list__item', 'Menor precio').click()
// Verify first item has different price, long timeout to wait for page re-load
cy.contains('.ui-search-result__wrapper:nth-child(1)', '$490', {timeout:20000})
您可以通過選擇不同的網路呼叫等待來縮短頁面加載等待時間。
uj5u.com熱心網友回復:
我在網頁上注意到兩件事:
- 顯示了兩個彈出視窗。單擊它們后,一切都按預期進行。
- 網頁會隨機拋出例外,因此您必須告訴您的 cypress 腳本忽略這些例外,以便進行穩定的測驗。話雖如此,這不是一個好的做法,因為您希望您的測驗捕獲這樣的例外。此外,請您的開發人員調查并解決導致拋出例外的問題。
下面的腳本對我有用,沒有任何額外的超時。
cy.visit(
'https://listado.mercadolibre.com.ar/scanner-blueetooth#D[A:scanner blueetooth]'
)
Cypress.on('uncaught:exception', (err, runnable) => {
return false
}) //For ignoring exceptions
cy.get('#newCookieDisclaimerButton').click() //Click Accept cookies button
cy.get('.andes-button--filled > .andes-button__content').click() //Click the pop up button
cy.get('.andes-dropdown__trigger').click() //Clicks the dropdown menu button
cy.get('.andes-list > :nth-child(2)').click() //Clicks the first option from the dropdown
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/337905.html
標籤:javascript 谷歌浏览器 柏
