0

I have a task where home page contains twenty ad titles (elements). I want to click on an ad tile that opens the detail page which is working. On detail page I have an element for CHAT button but that is not visible on every detail page. (reason: some user do not enable CHAT option on their ads) Here's what I want to achieve, If Chat element is found, then click on Chat element and come out of loop If chat element is not found, then go back and click on next ad tile and repeat until detail page with chat is opened Here's what I have tried but it is not working:


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 the element is not found in IF condition it does not go to else part to go back on home page

I want:

If the "IF" part is failing, i.e, if chat element is not found then it should go back and click on next ad tile on home page.

1 Answers1

1

You can try something like this:

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
    }
  });

consider your scenario like this:

if there is a chat button then a click should be performed on chat and do the required operation and then return back to detail page

and after that going back to main page should be a general step

Ali Zain
  • 11
  • 1