I would like to have a flexible assertion which checks if a string is one of several values.
I have just started running my WebdriverIO + Mocha tests on BrowserStack, but they have US locale machines which render dates differently, so my tests are failing. There doesn’t seem to be a locale setting in BrowserStack config.
e.g.
expectDOB_AU = '29/02/2024'
expectDOB_US = '2/29/2024'
await expect(await MyPage.inputDOB.getValue()).toBe([expectDOB_AU, expectDOB_US])
I can see that hasText
can take an array, but this doesn’t work for a string fetched from an input value.
I have looked at this related question Multiple values in a single assertion in webdriverio
I have implemented this as a custom matcher – but still would like to know if it’s possible in standard webdriverio.
test/cusom/customMatcher.js
expect.extend({
toBeOneOf(actual, expecteds) {
return {pass: expecteds.includes(actual), message: () => `${actual} should be one of ${expecteds}`}
}
})
optional: test/custom/CustomMatchers.d.ts
// keep the IDE or Typescript happier
declare namespace ExpectWebdriverIO {
interface Matchers<R, T> {
toBeOneOf(actual:string, expecteds: string[]): R
}
}
wdio.conf.js
before: function (capabilities, specs) {
require('./test/custom/cutomMatchers')
},
in the test spec
await expect(await myPage.inputDOB.getValue()).toBeOneOf([sDOB_AU,sDOB_US])
I realised I can use “native” Jest expects in webdriverio, and by inverting the assertion I can use a standard array matcher without need for custom matcher.
await expect([sDOB, sDOB_US]).toContain(await myPage.inputDOB.getValue())
It feels unusual to me to have the expect value on the other side, but it works.