skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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])
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search