SyntaxError:意外令牌{-运行量角器黄瓜测试时出错

时间:2019-04-17 07:19:19

标签: angular typescript protractor cucumber

我只是尝试从Protractor- Cucumber测试设置开始,以下是我的基本设置,我在网上跟随一些教程就做了。感谢撒玛利亚人分享了这一点。

问题是当我尝试运行它时,出现错误-导入的意外令牌。我不知道为什么会这样。非常感谢您的帮助。

以下是我正在使用的node等版本-

C:\Users\A>npm --version
6.4.1

C:\Users\A>node --version
v10.15.1

C:\Users\A>protractor --version
Version 5.4.2

以下是我的功能文件-

Feature: Go to the home
  Display the title

  Scenario: Home Page
    Given I am on the home page
    When I do nothing
    Then I should see the title

以下是我的步骤定义文件-

import { AppPage } from '../pages/app.po';
import { Before, Given, When, Then } from 'cucumber';
import { expect } from 'chai';

let page: AppPage;

Before(() => {
  page = new AppPage();
});

Given(/^I am on the home page$/, async () => {
  await page.navigateTo();
});

When(/^I do nothing$/, () => {});

Then(/^I should see the title$/, async () => {
  expect(await page.getTitleText()).to.equal('Welcome to angular-cli-cucumber-demo!');
});

以下是我的页面对象文件-

import { browser, by, element } from 'protractor';

export class AppPage {
  navigateTo() {
    return browser.get('/');
  }

  getTitleText() {
    return element(by.css('app-root h1')).getText();
  }
}

当我尝试使用命令ng e2e运行测试时,我遇到了以下错误-

DevTools listening on ws://127.0.0.1:56273/devtools/browser/7653bd59-c490-4ac0-a00e-0d3132dc5b11
[0417/123615.432:ERROR:broker_win.cc(137)] Error sending sync broker message: Error (0x5) while retrieving error. (0xE8)
[0417/123615.433:ERROR:command_buffer_proxy_impl.cc(92)] ContextResult::kFatalFailure: AllocateAndMapSharedMemory failed
[12:36:17] E/launcher - Error: C:\Users\A527629\Documents\workspace-vs-code\poc-serenity\e2e\src\steps\app.steps.ts:1
(function (exports, require, module, __filename, __dirname) { import { AppPage } from '../pages/app.po';
                                                                     ^

SyntaxError: Unexpected token {
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at supportCodePaths.forEach.codePath (C:\Users\A527629\Documents\workspace-vs-code\poc-serenity\node_modules\cucumber\lib\cli\index.js:142:42)
    at Array.forEach (<anonymous>)
    at Cli.getSupportCodeLibrary (C:\Users\A527629\Documents\workspace-vs-code\poc-serenity\node_modules\cucumber\lib\cli\index.js:142:22)
    at C:\Users\A527629\Documents\workspace-vs-code\poc-serenity\node_modules\cucumber\lib\cli\index.js:169:41
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (C:\Users\A527629\Documents\workspace-vs-code\poc-serenity\node_modules\cucumber\lib\cli\index.js:44:103)
[12:36:17] E/launcher - Process exited with error code 100
An unexpected error occurred: undefined

2 个答案:

答案 0 :(得分:1)

将以下部分添加到您的protractor.conf.js

onPrepare() {
  require('ts-node').register({
    project: require('path').join(__dirname, './tsconfig.json')
  });
}

答案 1 :(得分:0)

尝试删除

步骤定义

import { Before, Given, When, Then } from 'cucumber';
import { expect } from 'chai';

页面对象

import { browser, by, element } from 'protractor';

这些导入应在量角器初始化时自动完成。