簡體   English   中英

如何檢查元素是否在頁面上可用,如果沒有,go 返回並單擊下一個元素,如果存在預期元素,則單擊該元素

[英]How to check if element is available on a page, if not, go back and click on next element, if expected element is there then click on that element

我的任務是主頁包含二十個廣告標題(元素)。 我想點擊打開正在運行的詳細信息頁面的廣告圖塊。 在詳細信息頁面上,我有一個用於聊天按鈕的元素,但在每個詳細信息頁面上都不可見。 (原因:有些用戶沒有在他們的廣告上啟用聊天選項)這就是我想要實現的,如果找到聊天元素,則單擊聊天元素並退出循環如果找不到聊天元素,則返回 go 並單擊下一個廣告圖塊並重復,直到打開帶有聊天的詳細信息頁面這是我嘗試過但不起作用的方法:


it('should send chats', () => {
      const adTiles = 20;
      cy.loginWithApi() // custom command that logs in user and land hompage
      for (let i = 0; i < adTiles; i++) { //loop to iterate between ad tiles
        cy.get('._459428ad').eq(i).click(); // click on ad tile on homepage
        cy.wait(5000) //custom wait to load detail page properly
        if (cy.get('._5fd7b300.f3d05709').should('exist')){ 
          //if chat btn is these then click on chat
          cy.get('._5fd7b300.f3d05709').click()
        }
        else{
          cy.go('back') // else go back to home page if chat not enabled
        }
        }
      });

如果在 IF 條件下找不到該元素,則它不會返回 go,否則返回主頁上的 go

我想:

如果“IF”部分失敗,即,如果未找到聊天元素,那么它應該 go 返回並單擊主頁上的下一個廣告圖塊。

你可以嘗試這樣的事情:

it('should send chats', () => {
  const adTiles = 20;
  cy.loginWithApi() // custom command that logs in user and land hompage
  for (let i = 0; i < adTiles; i++) { //loop to iterate between ad tiles
    cy.get('._459428ad').eq(i).click(); // click on ad tile on homepage
    cy.wait(5000) //custom wait to load detail page properly
    if (cy.get('._5fd7b300.f3d05709').should('exist')){ 
      //if chat btn is there then click on chat
      cy.get('._5fd7b300.f3d05709').click()
     **// add a go back command here if chat is opened, to return to ad detail page**
    }
    **// go back as a general command without else command**
      cy.go('back') // else go back to home page if chat not enabled
    }
  });

像這樣考慮您的情況:

如果有聊天按鈕,則應在聊天上單擊並執行所需的操作,然后返回到詳細信息頁面

然后返回主頁應該是一個通用步驟

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM