运行量角器时无法在模块外部使用import语句

时间:2020-06-03 18:27:38

标签: javascript selenium ionic-framework import protractor

我有两个文件,用于运行量角器。当我使用命令“ protractor protractor.config.js”时,出现此错误:

D:\work\staru-app>protractor protractor.config.js
[16:57:17] I/launcher - Running 1 instances of WebDriver
[16:57:17] I/local - Starting selenium standalone server...
[16:57:18] I/local - Selenium standalone server started at http://192.168.1.8:51672/wd/hub
[16:57:23] E/launcher - Error: D:\work\staru-app\e2e\login.spec.js:1
import { AppPage } from './app.po';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1072:16)
    at Module._compile (internal/modules/cjs/loader.js:1122:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at C:\Users\fabio\AppData\Roaming\npm\node_modules\protractor\node_modules\jasmine\lib\jasmine.js:93:5
    at Array.forEach (<anonymous>)
    at Jasmine.loadSpecs (C:\Users\fabio\AppData\Roaming\npm\node_modules\protractor\node_modules\jasmine\lib\jasmine.js:92:18)
[16:57:23] E/launcher - Process exited with error code 100

我拥有的文件:

import { AppPage } from './app.po';


describe('Signing in', function(){
 let page: AppPage;

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

 it('Testing the usage of app.po',function () {
   //page.navigateTo();
   //expect(page.getToolbarTittle()).toEqual('LOGIN');
   browser.sleep(1000)
   expect(element(by.css('.toolbar-title')).getText()).toEqual('LOGIN');
 })


  it('should start on sign-in view', function(){
    browser.sleep(1000)
    expect(element(by.css('.toolbar-title')).getText()).toEqual('LOGIN');
  });
});

另一个:

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

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

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

两个文件都是js。

该项目在Ionic中构建。

我该怎么做才能解决此错误?

1 个答案:

答案 0 :(得分:2)

您不能在js文件中使用esnext import语句。

您可以使用2种方法解决该问题:

  1. 将js文件的扩展名更改为.ts
  2. 或使用require js语法将您的依赖项加载到.js文件中,如下所示:

    const appPage = require('./ app.po');