【问题标题】:Protractor - SyntaxError: Unexpected identifier量角器 - SyntaxError:意外的标识符
【发布时间】:2019-02-03 16:39:31
【问题描述】:

我在 Protractor 中编写了一些测试,但遗憾的是我在导入/导出文件方面没有什么问题。

HomePage.js

export default class HomePage {
  constructor() {
    this.path = 'http://automationpractice.com/index.php';
    this.searchQuery = element(by.css('.search_query'));
    this.searchButton = element(by.name('submit_search'));
    this.signInButtonHeader = element(by.css('.header_user_info'));
    this.emailInput = element(by.id('email'));
    this.passwordInput = element(by.id('passwd'));
    this.logInButton = element(by.id('SubmitLogin'));
  }
}

homepage-spec.js

import HomePage from '../page_objects/HomePage';
browser.waitForAngularEnabled(false);
const homePage = new HomePage();
beforeEach(async () => {
  browser.get(homePage.path);
});
describe('Homepage', () => {
  it('should have a title', () => {
    expect(browser.getTitle()).toEqual('My Store');
  });
  it('Log into your account', () => {
    homePage.signInButtonHeader.click();
    homePage.emailInput.sendKeys('testprotractor@gmail.com');
    homePage.passwordInput.sendKeys('xxxx');
    homePage.logInButton.click();
  });
  it('Check categories', () => {
    element(by.linkText('Women')).click();
  });
});`

conf.js

const SpecReporter = require('jasmine-spec-reporter').SpecReporter;
require('@babel/register');

exports.config = {
  framework: 'jasmine',
 capabilities: {
    browserName: 'chrome',
 },
 suites: {
    homePage: 'specs/homepage-spec.js',
    searchResults: 'specs/search-results-spec.js',
  },
  onPrepare: () => {
    jasmine.getEnv().addReporter(new SpecReporter({
      spec: {
        displayStacktrace: true,
      },
    }));
  },
};

package.json

{
  "name": "protractorautomation",
  "version": "1.0.0",
  "description": "This is my test",
  "main": "conf.js",
  "dependencies": {
    "babel-eslint": "^10.0.1",
    "@babel/register": "^7.0.0",
    "eslint": "^5.3.0",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-plugin-import": "^2.14.0",
    "jasmine-spec-reporter": "^4.2.1"
  },
   "devDependencies": {
    "@babel/core": "^7.2.2",
    "babel-preset-env": "^1.7.0",
    "babel-register": "^6.26.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Protractor"
  ],
  "author": "Xxxx",
  "license": "ISC"
}

每当我运行测试时,都会收到此错误:

(function (exports, require, module, __filename, __dirname) { import
来自“../page_objects/HomePage”的主页; ^^^^^^^^

SyntaxError: Unexpected identifier
at new Script (vm.js:84:7)
at createScript (vm.js:264:10)
at Object.runInThisContext (vm.js:312:10)
at Module._compile (internal/modules/cjs/loader.js:694:28)
at Module._compile   (C:\Users\X\Documents\protractorAutomation\node_modules\pirates\lib\index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:745:10)
at Object.newLoader [as .js] (C:\Users\Mati\Documents\protractorAutomation\node_modules\pirates\lib\index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:626:32)
at tryModuleLoad (internal/modules/cjs/loader.js:566:12)
at Function.Module._load (internal/modules/cjs/loader.js:558:3) [16:53:29] E/launcher - Process exited with error code 100

谁能帮我解决这个问题?

【问题讨论】:

  • 您尝试从 homepage.js 本身导入主页 PO 是否正确?
  • @DublinDev 抱歉,我在第一篇文章中错误地为 HomePage.js、homepage-spec.js 粘贴了相同的代码。我刚刚更正了第一篇文章。

标签: javascript node.js protractor automated-tests


【解决方案1】:

您尝试使用的 es6 导入样式似乎在 NodeJS 中不受本机支持。

根据this answer,您有两个选择。我已经创建了第一个示例供您尝试。

module.exports = class HomePage {
  constructor() {
    this.path = 'http://automationpractice.com/index.php';
    this.searchQuery = element(by.css('.search_query'));
    this.searchButton = element(by.name('submit_search'));
    this.signInButtonHeader = element(by.css('.header_user_info'));
    this.emailInput = element(by.id('email'));
    this.passwordInput = element(by.id('passwd'));
    this.logInButton = element(by.id('SubmitLogin'));
  }
}

将导入更改为要求

//Change import statement to standard require
let HomePage = require('../page_objects/HomePage');
browser.waitForAngularEnabled(false);
const homePage = new HomePage();
beforeEach(async () => {
  browser.get(homePage.path);
});
describe('Homepage', () => {
  it('should have a title', () => {
    expect(browser.getTitle()).toEqual('My Store');
  });
  it('Log into your account', () => {
    homePage.signInButtonHeader.click();
    homePage.emailInput.sendKeys('testprotractor@gmail.com');
    homePage.passwordInput.sendKeys('xxxx');
    homePage.logInButton.click();
  });
  it('Check categories', () => {
    element(by.linkText('Women')).click();
  });
});

【讨论】:

    猜你喜欢
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2017-10-11
    • 1970-01-01
    • 2018-03-09
    • 2018-03-29
    • 2018-07-19
    相关资源
    最近更新 更多