某些量角器 API 函数会导致错误

Certain protractor API functions cause errors

为什么我在使用 Protractor API 函数时收到错误消息?我在互联网上搜索了这个问题的解决方案,我找到了几个类似的答案,但它们没有用,所以我向社区询问。

我正在尝试使用 Karma 和 Protractor 设置最简单的 e2e 测试。 像这样的空测试代码块不会产生错误:

describe('Mapping Assistant State', () => {
   beforeEach(() => {
   });

   it('should display a table',() => {
      console.log('test');
   });

   it('should click the create Paste button',() => {

    });
});

一旦我从量角器添加某些函数,如 expect() 或 browser(),就会出现问题:

it('should display a table',() => {
    browser.get('someUrl');
    expect(element(by.css('.map')).getText()).toContain('map');

});

以下是我的部分内容 package.json:

"protractor": "^5.3.0",
"karma": "^1.7.1",
"@angular/cli": "^1.6.6",
"typescript": "^2.5.2"
"@angular/core": "^4.3.6",
"rxjs": "^5.4.3",
"@types/selenium-webdriver": "^3.0.6",
"ts-node": "^4.0.1",
"@types/node": "^8.0.58",

还有:

import { browser, by, element } from 'protractor';
import {describe} from 'selenium-webdriver/testing';
import {beforeEach} from 'selenium-webdriver/testing';

这是我的 protractor.conf.js 文件:

const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
    allScriptsTimeout: 11000,
    specs: [
        './e2e/**/*.e2e-spec.ts'
    ],
    capabilities: {
        'browserName': 'chrome'
    },
    directConnect: true,
    baseUrl: 'http://localhost:4200/',
    framework: 'jasmine',
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 30000,
        print: function() {}
    },
    onPrepare() {
        require('ts-node').register({
            project: 'e2e/tsconfig.e2e.json'
        });
        jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
    }
};

添加任何 Protractor 函数和 运行 'ng test' 后,会出现这些错误:

    ERROR in ./node_modules/saucelabs/index.js
Module not found: Error: Can't resolve './lib-cov/SauceLabs' in '/Users/moop/Work/frontend/web/node_modules/saucelabs'
 @ ./node_modules/saucelabs/index.js 2:2-32
 @ ./node_modules/protractor/built/driverProviders/sauce.js
 @ ./node_modules/protractor/built/driverProviders/index.js
 @ ./node_modules/protractor/built/runner.js
 @ ./node_modules/protractor/built/index.js
 @ ./e2e/sample.e2e-spec.ts
 @ ./src/test.ts

ERROR in ./node_modules/protractor/built/runner.js
Module not found: Error: Can't resolve 'child_process' in '/Users/moop/Work/frontend/web/node_modules/protractor/built'
 @ ./node_modules/protractor/built/runner.js 54:32-56
 @ ./node_modules/protractor/built/index.js
 @ ./e2e/sample.e2e-spec.ts
 @ ./src/test.ts

ERROR in ./node_modules/protractor/built/bpRunner.js
Module not found: Error: Can't resolve 'child_process' in '/Users/moop/Work/frontend/web/node_modules/protractor/built'
 @ ./node_modules/protractor/built/bpRunner.js 3:24-48
 @ ./node_modules/protractor/built/driverProviders/driverProvider.js
 @ ./node_modules/protractor/built/driverProviders/index.js
 @ ./node_modules/protractor/built/runner.js
 @ ./node_modules/protractor/built/index.js
 @ ./e2e/sample.e2e-spec.ts
 @ ./src/test.ts

ERROR in ./node_modules/protractor/built/debugger.js
Module not found: Error: Can't resolve 'child_process' in '/Users/moop/Work/frontend/web/node_modules/protractor/built'
 @ ./node_modules/protractor/built/debugger.js 67:32-56
 @ ./node_modules/protractor/built/browser.js
 @ ./node_modules/protractor/built/index.js
 @ ./e2e/sample.e2e-spec.ts
 @ ./src/test.ts

ERROR in ./node_modules/selenium-webdriver/firefox/binary.js
Module not found: Error: Can't resolve 'child_process' in '/Users/moop/Work/frontend/web/node_modules/selenium-webdriver/firefox'
 @ ./node_modules/selenium-webdriver/firefox/binary.js 25:14-38
 @ ./node_modules/selenium-webdriver/firefox/index.js
 @ ./node_modules/protractor/built/index.js
 @ ./e2e/sample.e2e-spec.ts
 @ ./src/test.ts

ERROR in ./node_modules/selenium-webdriver/io/exec.js
Module not found: Error: Can't resolve 'child_process' in '/Users/moop/Work/frontend/web/node_modules/selenium-webdriver/io'
 @ ./node_modules/selenium-webdriver/io/exec.js 20:21-45
 @ ./node_modules/selenium-webdriver/remote/index.js
 @ ./node_modules/protractor/built/index.js
 @ ./e2e/sample.e2e-spec.ts
 @ ./src/test.t

有没有人知道是什么原因造成的,我该如何解决?

再安装两个包:@types/nodets-node

有时我们添加事件发射器然后visual studio自动添加事件发射器库。

喜欢:import { EventEmitter } from 'protractor';然后自动生成保护器错误。

解决方案:从自己的代码中删除自动导入并添加 import { EventEmitter } from '@angular/core';

所以错误是在我们的代码中为 EventEmitter 添加了错误的库。

在我的例子中,不小心从保护器中导入了 $ jquery

如果说

,请仔细检查您的日志

日志-
./node_modules/saucelabs/index.js 中的错误 找不到模块:错误:无法解析 'N:\Angular Codes\billmanagement\node_modules\ saucelabs' 中的“./lib-cov/SauceLabs”
./node_modules/protractor/built/runner.js 中的错误 找不到模块:错误:无法解析 'N:\Angular Codes\billmanagement\node_modules\ protractor\built'

中的 'child_process'


解决方案-
确保在 app.module.ts 中导入了正确版本的 Event Emitter
从“@angular/core”导入{EventEmitter};

不是这个-(通常 Visual Studio 会自动执行) 从 'protractor' 导入 {EventEmitter};

如果您使用的是编辑器(例如 VSCode),多次自动导入会产生此问题。除了上述答案外,即表明:

import { EventEmitter } from 'protractor';

而不是这个...

import { EventEmitter } from '@angular/core';

会给你说的错误。但即便如此:

import { error } from 'protractor';

而不是这个...

import { error} from '@angular/core';

也会给你同样的结果。就我而言,我不需要导入!我试图在 catch 语句中使用临时变量名称“error”,在键入该名称时,它会自动创建一个从量角器导入。 (我现在决定使用更具体的名称,例如“myfuncerr”)。因此,任何自动完成的内容都可能从错误的库中导入。

如果您遇到此错误,首先要做的是对“量角器”一词进行全局查找。这是一个快速修复(至少可以为我节省几个小时)。