Combine / Concat TestCafe选择器以供重用

问题描述 投票:0回答:1

我使用页面对象模式,通常情况是,我喜欢在另一个选择器中寻址一个元素。所以我只想重用已经定义的CSS选择器。我当前的解决方法是定义一个纯字符串。

是否有可能做类似的事情:

const list = (listName) => Selector('#list' + listName)
const item = (itemName) => Selector(list, '#item-' + itemName)

获得选择器的CSS选择器文本就足够了:

const list = (listName) => Selector('#list' + listName)
const item = (itemName) => Selector(`${list.selectorText} #item-${itemName}`)

或者该查找方法支持选择器:

const list = (listName) => Selector('#list' + listName)
const item = (itemName) => list.find(Selector('#item-' + itemName))
testing automation automated-tests e2e-testing testcafe
1个回答
0
投票

是的,您肯定可以组合选择器字符串,因为它是常规的CSS选择器字符串。

注意:testscafe测试api操作(例如Click(https://devexpress.github.io/testcafe/documentation/test-api/actions/click.html))接受字符串作为参数。如果不需要使用Selector类方法(例如withText,nth等),则可以编写如下测试:

const listSelector  = (listName) => `#list${listName}`;

//somewhere in test:
  await t.click(listSelector('My List Name');
© www.soinside.com 2019 - 2024. All rights reserved.