Search code examples
testingautomated-testse2e-testingtestcafebrowser-automation

Assert a URL redirection when second URL can not be resolved in TestCafe


I want to assert that the next URL gets resolved. It's a URL generated by SendGrid and changes one you search it in the browser. The problem is that to access the second you need access to our VPN. For security reasons I don't want to run the test behind our VPN but I would like to assert if the redirection from SendGrid is successful (I know exactly what the resolved URL going to look like).

http://link.novemberfirst.com/ls/click?upn=xv2uBCiA-2BCPo8h2-2F2dXgSrKknBTgupgeogOdcsRpYXuG4cjYbJP1rkCCJ1zjXTj38aokCt-2BgUcrLMVeDJ0JrFznoH-2B4JC2bxKIUkkwWGvQPWZcW89yPA45cRI4YcN4kxIWOMamgXF7h0x-2BEumL4yC4cDPD6ib2Q-2FupSBTSJJvOohq9d3QApg3ktPsAr3s71dx8_f_jH2AkQSF6IE8lczSdagr9wUDFh2rSjc-2FDARefysokTdqaEnXNbSWSyYzYz4pIdy3KyzKI2WI84SAhUWA6Ysrwbu-2FEyv-2FLVj28UDJ96u9m0SfvfLMgVTTOypSpnfzJhz8HBp-2BErWzxw-2BHQBTWKJQCCsahFPt7FAp2yflzLocMroRH-2Blyqk-2Be61Q84U19yBAQ11beRliiLdNHnzJwd88-2FGbgPTIQoQi0xlVBQxVt08NyA-3D

The problems comes when using ClientFunction(() => document.location.href) you will get only a URL that have been resolved successfully, and since I am not connected to the VPN the last URL won't be displayed and cant be asserted.

Is there a way to create an assert after the redirection is done even if I can't resolve the second URL?

await t.navigateTo(URL);

const getLocation = ClientFunction(() => document.location.href);

await t.expect(getLocation()).contains('expected-url');

In this case the expected-url would be: https://app-uat.novemberfirst.cloud.ec/#/public/reset-password/208100002/[email protected]/F1PMDcC05gYhlqh82UDnl183/dan


Solution

  • Found the solution to this.

    Basically when making a http request you have to disable any redirection to get the url.

    private async resolveRedirectLink(url) {
        let redirectedURL;
        try{
            let response = await fetch(url, {redirect: 'manual'});
            let link = await response.text();
            let matches = link.match(/"(.*?)"/);
            redirectedURL = matches[1];
        }
        catch(error){
            redirectedURL = url;
            console.warn('Error redirecting URL: ' + error.message );
        }
        return redirectedURL;
    }
    

    This is the snipped used in my code.