选择器应使用函数进行初始化

时间:2019-03-04 19:14:26

标签: testing ecmascript-6 automated-tests e2e-testing testcafe

我成功选择了一堆页面元素,现在我将所有这些元素传递给单独的页面模型和助手功能。

在后者上,我创建了一个辅助函数类,如下所示:

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;
    }
  }
}

然后在我的测试文件中,按如下方式调用该方法:

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'));
});

但出现以下错误:

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.

我对ES6还是很陌生,所以我认为这与我调用该方法的方式有关。

1 个答案:

答案 0 :(得分:4)

解决方案可能是:

export default class Helpers {
  selectPageElement (elementName) {
    return Selector('div > h2')
      .withText(elementName);
  }
}