为什么我使用Protractor API函数时会收到错误?我已经在网上搜索了这个问题的解决方案,我找到了几个类似的答案,但是他们没有工作,所以我问社区。 p>
我正在尝试使用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 } }));
}
};
添加任何量角器功能并运行“测试”后,会出现以下错误:
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
有没有人知道造成这种情况的原因以及如何解决这个问题?
答案 0 :(得分:5)
请仔细检查您的日志
日志-
./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 \ 量角器 \ built'
解决方案-
确保在app.module.ts中已导入正确版本的事件发射器
从'@ angular / core'导入{EventEmitter};
不(通常是Visual Studio自动执行)
从“量角器”导入{EventEmitter};
答案 1 :(得分:1)
再安装两个包:<input type="text" class="input-text hasDatepicker" name="wc_order_field_2563" id="wc_order_field_2563" placeholder="Click here to choose your pickup date*: (Tuesday - Saturday)" value="">
和@types/node
答案 2 :(得分:1)
在我的情况下,偶然从保护器而不是jquery导入了$
答案 3 :(得分:1)
如果使用的是编辑器(例如VSCode),则自动导入将产生此问题。除了上面的答案,即表明了这一点:
import { EventEmitter } from 'protractor';
代替这个...
import { EventEmitter } from '@angular/core';
会给您所说的错误。但是,即使这样:
import { error } from 'protractor';
代替这个...
import { error} from '@angular/core';
也会给您相同的结果。就我而言,我不需要导入!我试图在catch语句中使用临时变量名称“错误”,并且在键入该名称时会自动从量角器创建导入。 (我现在决定使用更具体的名称,例如“ myfuncerr”)。因此,任何自动完成功能都可能会从错误的库中导入。
如果遇到此错误,首先要做的是对“量角器”一词进行全局查找。这是一个快速解决方案(它将为我节省至少几个小时)。