将当前 URL 写入 TestCafe 中的控制台

Write current URL to console in TestCafe

我有一个变量 "currentPage",我想将其设置为 运行 页面上的当前 URL。 但是要看到 URL 是正确的,我想将它打印到控制台。无论我尝试什么,我都会得到 "not defined"、"object"、... 另一方面,如果我使用 "await t.expect(...)" 方法并使其失败,那么我会看到想要的 URL.

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

我可以将它写入控制台输出吗? 如果是这样,那么我想也应该可以做类似的事情 "currentPage = getURL()" 但我得到:

current page function __$$clientFunction$$() {

您在调用 ClientFunction 之前错过了 await 关键字。请参考http://devexpress.github.io/testcafe/documentation/test-api/obtaining-data-from-the-client.html#executing-client-functions。 我建议您按以下方式编写:

const url = await getURL();
console.log(url);

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

只需使getURL() 自调用函数即可。恕我直言