【问题标题】:Is it possible to access af function defined on the website with Testcafé?是否可以使用 Testcafé 访问网站上定义的 af 功能?
【发布时间】:2019-12-05 15:55:19
【问题描述】:

假设“mywebsite.com”具有全局功能:

function sayHello(message){
console.log(message)
}

是否可以在 nodejs 中使用 testcafé 运行该功能的测试?

到目前为止我有这个

import { Selector } from 'testcafe';

fixture `mywebsite test`
    .page `http://mywebsite.com`;

test('sayHelloFuncTest', async t => {

    sayHello('HELLO!')

});

这会给我一个错误提示:ReferenceError: sayHello is not defined

那么有没有办法访问 sayHello() 函数?

【问题讨论】:

  • testcafe 用于模拟网页上的用户交互。如果您想测试特定的功能,您可能需要导出您想要测试的功能,并将它们导入您的测试脚本
  • 我试过了,但遇到了很多错误,例如“未定义提取”。所以它不会像我出于某种原因在浏览器中运行它一样运行。是否有更合乎逻辑的方式在浏览器中打开网站,运行全局定义的函数并返回错误,使用 nodeJs

标签: javascript node.js testing automated-tests testcafe


【解决方案1】:

您可以使用ClientFunction 在浏览器中执行一些脚本。

例如:

import { Selector, ClientFunction } from 'testcafe';

fixture `Fixture`
    .page `example.com`;

test('Test', async t => {
    const callSayHello = ClientFunction(() => { sayHello('HELLO!'); });

    await callSayHello();
});

await t.eval(() => { sayHello('HELLO!'); });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 2017-07-30
    • 2015-09-28
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    相关资源
    最近更新 更多