【问题标题】:Failed loading config.ts due to import protractor由于导入量角器而无法加载 config.ts
【发布时间】:2019-10-08 17:11:43
【问题描述】:

我正在尝试启动一个新的量角器项目来测试一个角度站点。我安装了 node.js、typescript、protractor global 和 jasmine。我转到项目文件夹并执行webdriver-manager update。然后我做webdriver-manager start。我还使用tsc config.ts 构建了config.ts。一切正常,直到我尝试protractor config.ts。在这里我将提供我的 config.ts 和我的 package.json。

{
"name": "protractortests",
"version": "1.0.0",
"description": "Automated tests for a game platform",
"main": "index.js",
"dependencies": {
  "@types/jasmine": "^3.3.12",
  "@types/node": "^12.0.2",
  "jasmine": "^3.4.0",
  "protractor": "^5.4.2"
},
"devDependencies": {},
"scripts": {
  "test": "protractor config.ts"
}

还有我的 config.ts:

import { ProtractorBrowser, Config } from "protractor";
    export let config: Config = {
      seleniumAddress: 'http://localhost:4444/wd/hub',
      capabilities: {
        'browserName': 'chrome'
        },

      framework: 'jasmine',
      specs: ['./FirstSpec.ts'],
      jasmineNodeOpts: {
        defaultTimeoutInterval: 90000
      },
      onPrepare: () => {
       let globals = require('protractor/built');
       let browser = globals.browser;
       browser.manage().window().maximize();
       browser.manage().timeouts().implicitlyWait(5000);
     }
    }
E/configParser - Error code: 105
[11:40:53] E/configParser - Error message: failed loading configuration file config.ts
[11:40:53] E/configParser - C:\Users\Victor\Documents\ProtractorTests\config.ts:1
(function (exports, require, module, __filename, __dirname) { import { ProtractorBrowser, Config } from "protractor";
                                                                     ^

SyntaxError: Unexpected token {
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._compile (internal/modules/cjs/loader.js:664:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
npm ERR! Test failed.  See above for more details.

【问题讨论】:

  • 尝试删除 import { ProtractorBrowser, Config } from "protractor"; 这些导入会在 Protractor 初始化时自动发生
  • 不能删除它,因为它后来在导出配置中使用:Config
  • 哦我的错,使用打字稿based on this example时可能需要导入。我会删除ProtractorBrowser,但似乎不需要这样做
  • 但还是同样的错误:(
  • 您是否在尝试执行之前使用npm run tsc 编译您的代码?如果不试一试

标签: node.js typescript selenium protractor automated-tests


【解决方案1】:

参考链接https://github.com/angular/protractor/tree/5.4.1/exampleTypescript上的示例

您不需要导入 ProtractorBrowser。您可以使用对象浏览器直接使用浏览器。

【讨论】:

    【解决方案2】:

    评论者指出,你不能在原生 Typescript 中给 Protractor 一个配置文件,需要将其编译为 config.js 然后传递。用 Typescript 编写文件真的没有意义,它只是增加了一个额外的步骤,对你没有任何价值。如果你想要编辑器自动完成,你可以用类型注释来装饰你的 JS:

    const { join } = require("path");
    const { register } = require("ts-node");
    
    const { SpecReporter, StacktraceOption } = require("jasmine-spec-reporter");
    
    
    /** @type {import("protractor").Config} */
    const config = {
        directConnect: true,
        baseUrl: "http://localhost:8080",
        framework: "jasmine",
        noGlobals: true,
        specs: [ "./src/**/*.e2e.ts" ],
        onPrepare() {
            register({
                project: join(__dirname, "./tsconfig.json")
            });
            jasmine.getEnv().addReporter(new SpecReporter({
                spec: {
                    displayStacktrace: StacktraceOption.PRETTY
                }
            }));
        }
    };
    
    module.exports = { config };
    

    我从 this excellent example 调整了我的配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      相关资源
      最近更新 更多