How can I get string value of selector?

Viewed 370

I mean when we initialize Selector like this:

let stringLocator = 'some element locator'
selector = new Selector(stringLocator)

is that possible to get original string locator somehow like this:

selector.locator

p.s. This question is related to this one where I've found some hacky workarounds to get testcafe display my xpath locators in error.

2 Answers

Testcafe does not support this, but you can use the following approach

function createSelector (locator) {
  return Object.assign(Selector(locator), { locator });
}

const selector = createSelector('#button');

console.log(selector.locator)

I had the same issue yesterday. The apiFnChain contains the fnChain you used for the selector. It is behind a symbol. This is what helped me. Maybe this will help you too.

const selector = Selector('button.explore-button').withText('Explore the site')
const selectorFnChain = selector[Object.getOwnPropertySymbols(selector)[0]].options.apiFnChain
console.log(selectorFnChain)

This will give you this:

Selector('button.explore-button'),.withText('Explore the site')

You don't have to get the entire chain but this was the most helpful for me.

Related