如何获取选择器的字符串值?

How can I get string value of selector?

我的意思是当我们像这样初始化选择器时:

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

是否有可能像这样以某种方式获得原始字符串定位器:

selector.locator

p.s。这个问题与这个 one 有关,我在其中找到了一些 hacky 解决方法来让 testcafe 错误地显示我的 xpath 定位器。

Testcafe不支持这个,但是可以使用下面的方法

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

const selector = createSelector('#button');

console.log(selector.locator)

我昨天遇到了同样的问题。 apiFnChain 包含您用于选择器的 fnChain。它在一个符号后面。这就是帮助我的。也许这对你也有帮助。

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

这会给你这个:

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

您不必获得整个链条,但这对我来说是最有帮助的。