Home > Blockchain >  How to do an action if the URL contains - TestCafe
How to do an action if the URL contains - TestCafe

Time:08-05

I'm looking to add some actions, only if the page URL is the given one. I'm using TestCafe Studio. So I have added a 'Custom Scripts' block with the following. But this is erroring out that contains is not a function.

const getURL = ClientFunction(() => window.location.href.toString());
const myurl = await getURL;
if(myurl.contains('/update')){
await t.click('#updateInfoSubmit');
}

CodePudding user response:

First things first you need ClientFunction module to use ClientFunction method. Then your test should looks more or less like this \/. Also remember to use await when getting the url

import { Selector, t, ClientFunction } from 'testcafe';

 fixture `RandomPage`
.page("https://stackoverflow.com/questions/73225773/how-to-do-an-action-if-the-url-contains-testcafe")

// Function that Returns the URL of the current web page
const getPageUrl = ClientFunction(() => window.location.href);

test('Test if url has parmeter', async t => {
    await t 
        .click(Selector("a").withExactText("automated-tests"))
     
    // Use await to get page url
    var pageUrl = await getPageUrl();
    // Check if url has string
    if(pageUrl.includes('tagged')){
        console.log("url contains string - tagged")
        // await t.click(Selector("a"))
    }
});
  • Related