【问题标题】:Error while executing test: ERROR Cannot prepare tests due to an error执行测试时出错:ERROR 由于出现错误无法准备测试
【发布时间】:2019-08-12 16:00:12
【问题描述】:

使用 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();

【问题讨论】:

    标签: testing automated-tests e2e-testing testcafe web-testing


    【解决方案1】:

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

    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');
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多