执行测试时出错:ERROR Cannot prepare tests due to an error

Error while executing test: ERROR Cannot prepare tests due to an error

使用 CLI 在 TestCafe 上执行测试时,出现以下错误: ERROR Cannot prepare tests due to an error.

 at Object.<anonymous> (C:\Users\xxxx\Documents\TestCafe_Framework\tests\Test.js:1:1)
    at Function._execAsModule (C:\Users\xxxxx\Documents\TestCafe_Framework\node_modules\testcafe\src\compiler\test-file\api-based.js:50:13)
    at ESNextTestFileCompiler._runCompiledCode (C:\Users\xxxxx\Documents\TestCafe_Framework\node_modules\testcafe\src\compiler\test-file\api-based.js:150:42)
    at ESNextTestFileCompiler.execute (C:\Users\xxxxxx\Documents\TestCafe_Framework\node_modules\testcafe\src\compiler\test-file\api-based.js:174:21)
    at ESNextTestFileCompiler.compile (C:\Users\xxxxx\Documents\TestCafe_Framework\node_modules\testcafe\src\compiler\test-file\api-based.js:180:21)
    at Compiler._getTests (C:\Users\xxxxxx\Documents\TestCafe_Framework\node_modules\testcafe\src\compiler\index.js:87:31)
    at Compiler._compileTestFiles (C:\Users\xxxxx\Documents\TestCafe_Framework\node_modules\testcafe\src\compiler\index.js:99:35)

最初我记录了 TestCafe GUI 的步骤并执行了相同的步骤。但是当我将我的代码转换为 POM 样式时,它抛出了上面的错误

测试文件:这是将从 CLI 执行的实际测试

import LoginPage from `C:\Users\xxxxxx\Documents\TestCafe_Framework\page_object;`

fixture(`Testing`)
    .page(`https://xxxx/login.php`);

test('PVT ', async (t) => {
    //var email = Math.floor(Math.random() * 555);
    //var random_ascii = Math.floor((Math.random() * 25) + 97);
    //var name = String.fromCharCode(random_ascii);
    await t.LoginPage.login('hp', 'St');

});

页面文件:该文件包含用于执行登录操作的选择器和操作函数。

import { Selector, t } from 'testcafe';

   class LoginPage {

       get emailTextBox() { return Selector('#EmailAddr'); }
       get passwordTextBox() { return Selector('#Password'); }
       get loginButton() { return Selector('#SignIn'); }

       async login(username, password) {
           await t
               .click(this.emailTextBox)
               .typeText(this.emailTextBox, username)
               .click(this.passwordTextBox)
               .typeText(this.passwordTextBox, password)
               .click(this.loginButton)
       }
   }
   export default new LoginPage();

我可以在您的测试代码中看到两个错误:

  1. import语句中的斜杠没有转义。这是您遇到错误的原因。
  2. 第二期在这一行:await t.LoginPage.login('hp', 'St');。 LoginPage 是从文件导入的,因此无法从 testController 获得。

如果按以下方式进行更改,测试应该可以正常工作:

import LoginPage from 'C:\Users\xxxxxx\Documents\TestCafe_Framework\page_object';

fixture(`Testing`)
    .page(`https://xxxx/login.php`);

test('PVT ', async (t) => {
    //var email = Math.floor(Math.random() * 555);
    //var random_ascii = Math.floor((Math.random() * 25) + 97);
    //var name = String.fromCharCode(random_ascii);
    await LoginPage.login('hp', 'St');

});