Search code examples
javascriptregexwebdriver-io

How to pass hex chars regex in string assertion


I am using javascript with webDriverIO for automated test and in my assertion, one of the substrings is a dynamic value which keeps changing to hex chars every time I run my test therefore want to cover this value with a regex pattern

For an example:

expect(await browser.testFunction()).to.match(/conda[\\/]envs[\\/]f0b3e3538b395cf003cf5128ce26[\\/]bin[\\/]python3/)

Where f0b3e3538b395cf003cf5128ce26 is project id which keep changing in a given pattern.

In above i want to cover f0b3e3538b395cf003cf5128ce26 with regex pattern [0-9a-fA-F]+

I am trying this like:

expect(await browser.testFunction()).to.match(/conda[\\/]envs[\\/][0-9a-fA-F]+[\\/]bin[\\/]python3/)

This throw error "re.exec is not a function"

Can someone help with how to apply regex pattern correctly ?


Solution

  • I thinks that the issue you're experiencing might be related to using a regex literal inside the .match() function, and not the regex pattern itself. To fix this issue, I think you should use the RegExp constructor to create a regex object with your desired pattern. Here is an example I have made to show you how you could do this:

    const regexPattern = new RegExp('conda[\\\\/]envs[\\\\/][0-9a-fA-F]+[\\\\/]bin[\\\\/]python3');
    
    
    expect(await browser.testFunction()).to.match(regexPattern);