Daniel Eisenreich
Daniel Eisenreich

Reputation: 1523

Combine/Concat TestCafe Selector for reuse

I use the page object pattern and it's often the case, that I like to address an element inside another selector. So I would just like to reuse an already defined CSS-selector. My current workaround is just to define a plain string.

Is it possible to do something like:

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

It would be even enough to get the CSS-Selector text of the Selector like:

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

Or that the find-method supports a Selector:

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

Upvotes: 1

Views: 805

Answers (1)

Shurygin.Sergey
Shurygin.Sergey

Reputation: 431

Yes, you surely may combine selector strings, since it is a general css selector string.

Note: testscafe testing api actions such as Click (https://devexpress.github.io/testcafe/documentation/test-api/actions/click.html) accept strings as arguments. If you don't need to use the Selector class methods such as withText, nth etc., you can write a test as follows:

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

//somewhere in test:
  await t.click(listSelector('My List Name');

Upvotes: 1

Related Questions