1

I was trying to use Locator Builder (https://codecept.io/locators/#locator-builder) and noticed that when I use withAttr, it does a equal comparison

Ex: locate('a').withAttr({href: '/order/offer/'})

Translates to: .//a[@href = '/order/offer']

I was looking at option where withAttr translates to "contains" comparison.

Ex: .//a[contains(@href, '/order/offer')]

Since the href has a dynamic value at the end, I had to use "contains" in my xpath. Would like to know if there is a similar option with Location Builder

Note: I saw that withText does a contains comparison

Svirin
  • 564
  • 1
  • 7
  • 20

3 Answers3

1

you can use something like

locate('a[href^='/order/offer/']')

This will translate to .//a[starts-with(@href,'/order/offer')]

Prerit M
  • 11
  • 1
0

No, you can't do it with locator builder since only developers of this feature can define how it work. You can do a feature request, but from my experience this feature are not widely used so most probably they will not improve it or create a new command.

elind
  • 36
  • 3
0

You are probably looking for Custom Locators.

// inside a plugin or a bootstrap script:
codeceptjs.locator.addFilter((providedLocator, locatorObj) => {
    if (typeof providedLocator === 'string') {
      // this is a string
      if (providedLocator[0] === '=') {
        locatorObj.value = `.//*[text()="${providedLocator.substring(1)}"]`;
        locatorObj.type = 'xpath';
      }
    }
});

This translates to:

I.click('=Login');

You can be pretty creative with it.

Kevin
  • 561
  • 1
  • 7
  • 20