Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selector is expected to be initialized with a function

I successfully selected a bunch of page elements and now I'm passing all these elements to separate page models and helper functions.

On the latter, I created a helper function class as follows:

import { Selector } from 'testcafe';

export default class Helpers {
  selectPageElement (elementName) {
    const elementTitle = Selector ('div').child('h2');

    const numberOfElements = elementTitle.count;

    if (numberOfElements) {
      for (let i = 0; i < numberOfElements; i++) {
        if (elementTitle.nth(i) === 'aname') {
          return elementTitle.nth(i);
        }
      }
    } else {
      return false;
    }
  }
}

Then on my test file I'm calling that method as follows:

import { ClientFunction, Selector } from 'testcafe';
import Helpers from './helpers';

...
const helpers = new Helpers();

test('Test name', async t => {
  ...
  await t
    .click(helpers.selectPageElement('Element Name'));
});

but I'm getting the following error:

Action "selector" argument error:

Selector is expected to be initialized with a function, CSS selector string, 
another Selector, node snapshot or a Promise returned by a Selector,
but undefined was passed.

I'm fairly new to ES6, so I believe it's something with the way I'm calling the method.


1 Answers

A solution could be:

export default class Helpers {
  selectPageElement (elementName) {
    return Selector('div > h2')
      .withText(elementName);
  }
}
like image 63
hdorgeval Avatar answered Oct 22 '25 05:10

hdorgeval



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!