13

I have a variable "currentPage" which I want to set to the current URL on the running page. But to see that the URL is correct I want to print it to the console. What ever I try I keep getting "not defined", "object", ... If I on the other hand use the "await t.expect(...)" method and make it fail then I see the wanted URL.

const getURL = ClientFunction(() => window.location.href);
console.log(getURL) //does not work
console.log(getURL()) //does not work

Can I write it to console output? If so then I guess it should also be possible to do something like "currentPage = getURL()" but I get:

current page function __$$clientFunction$$() {
Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
ASE
  • 355
  • 2
  • 12

2 Answers2

11

You've missed the await keyword before calling ClientFunction. Please refer to http://devexpress.github.io/testcafe/documentation/test-api/obtaining-data-from-the-client.html#executing-client-functions.   I suggest you write it in the following manner:

const url = await getURL();
console.log(url);
Alex Kamaev
  • 6,198
  • 1
  • 14
  • 29
  • if you ever make an error in test cafe, double check you're awaiting things. As someone who's been using it a lot over the last few months. That's my most common error – Sagick Dec 30 '19 at 16:54
2

const getURL = await ClientFunction(() => window.location.href)();
console.log(getURL) //will work

Just make getURL() self invocation function. IMHO