skip to Main Content

I have one issue with cypress test.
I have such test

const deeplinkUrl = 'https://deeplink.page.link/2WmRA4zbfUcQz5AY9';

describe('upload scan view', () => {
  beforeEach(() => {
    cy.visit('/deplinkPage');
    cy.injectAxe();
  });

  it('should redirect user to deep link', () => {
    cy.waitForLoaderRemoval();
    cy.getEnabledButton('Install for Free');
    cy.get('body').compareSnapshot('upload-scan-view');

    cy.get('button').contains('Install for Free').trigger('click');
    cy.url().should('be.equals', deeplinkUrl);
  });
});

I have issue every time I am clicking on button with deeplink. My test fails because cypress waits for page to be loaded and every time it checks url there is different url because deepllink have redirection to app store or it will redirect you to app with totally different url. And because of that after page load event there is different link
I need to check that after clicking button users url is my deeplink which is there for some short time after clicking

2

Answers


  1. You can introduce a custom command that waits for the URL to match your deeplink. Here’s an example of how you can modify your code:

    const deeplinkUrl = 'https://deeplink.page.link/2WmRA4zbfUcQz5AY9';
    
    describe('upload scan view', () => {
      beforeEach(() => {
        cy.visit('/deplinkPage');
        cy.injectAxe();
      });
    
      // Custom command to wait for the URL to match the expected deeplink
      Cypress.Commands.add('waitForDeeplink', (expectedUrl) => {
        cy.url().should('include', expectedUrl);
      });
    
      it('should redirect user to deep link', () => {
        cy.waitForLoaderRemoval();
        cy.getEnabledButton('Install for Free');
        cy.get('body').compareSnapshot('upload-scan-view');
    
        cy.get('button').contains('Install for Free').click();
    
        // Wait for the deeplink URL
        cy.waitForDeeplink(deeplinkUrl);
      });
    });
    
    Login or Signup to reply.
  2. Can you catch the link with an intercept?

    This should work as long as the app you are testing fires off the request to deeplinkUrl.

    let deepLinkWasCaught = false
    
    cy.intercept(deeplinkUrl).as('deepLink')
    
    cy.get('button').contains('Install for Free').click()  
    
    cy.wait('@deeplink')
    

    Adding req.destroy() will stop the redirect happening if that is getting in the way of your test.

    Or if you just wish to delay the redirect, add a setTimeout().

    let deepLinkWasCaught = false
    
    cy.intercept(deeplinkUrl, (req) => {
      setTimeout(() => {
        req.continue()
      }, 2000)
    }).as('deepLink')
    
    cy.get('button').contains('Install for Free').click()  
    
    cy.wait('@deeplink')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search