Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i combine Selector with my Utility function?

While writing tests with TestCafe i'm creating utility functions, but there seems to be a problem when using the Selector('') method inside any function.

The Selector('') method works fine inside test files and also when importing from another file (utility_selectors.js). I think I need to include something inside the function, but I'm stuck and can't seem to find the solution.

My goal is to create a function to select mouse click coordinates.


Utility_selectors.js

import { Selector } from 'testcafe';

export const viewport = Selector('.viewport').find('canvas');

Utility_functions.js

import * as s from './selectors.js';

export const selectPoint = (x,y) => {
  return s.viewport + ",{ offsetX :" + x + ", offsetY :" + y + "}"
}

OR (both don't work)

export function selectPoint(x,y){
  return s.viewport + ",{ offsetX :" + x + ", offsetY :" + y + "}"
}

Testfile.js (utility function in action)

import { selectPoint } from '../utilities/functions.js';

test('example utility function', async (t) => {
  await t.click(selectPoint(100,200));
});

When executing, the following error occurs in cmd:

  SyntaxError: Failed to execute 'querySelectorAll' on 'Document': 'function
  __$$clientFunction$$() {
  const testRun = builder._getTestRun();
  const callsite = (0, _getCallsite.getCallsiteForMethod)(builder.callsiteNames.execution);
  const args = [];

  // OPTIMIZATION: don't leak `arguments` object.
  for (let i = 0; i < arguments.length; i++) args.push(arguments[i]);

  return builder._executeCommand(args, testRun, callsite);
  },{ offsetX :100, offsetY :200}' is not a valid selector.

So long story short, I want to include the TestCafe's Selector('') method inside a utility function.

Thanks in advance!

like image 307
D.Rooij Avatar asked Jan 09 '19 09:01

D.Rooij


1 Answers

Your provided code will not work since you are trying to concatenate a string to a generated function. This string should be an Object passed as the second argument to the click function.

If you always use the same selector, I can imagine you would want to create a utility like clickPoint(t, 100,200).

This can be achieved by the following utility function.

import * as s from './selectors.js';

export const clickPoint = (t, x, y) => {
  return t.click(s.viewport, { offsetX : x, offsetY: y });
};

Your testfile would look like this:

import { clickPoint } from '../utilities/functions.js';

test('example utility function', async (t) => {
  await clickPoint(t, 100, 200);
});
like image 138
Chris Avatar answered Oct 31 '22 11:10

Chris