I've searched around quite a bit on this problem, but can't find a solution.
在这个问题上我已经研究了很多,但是还没有找到解决的办法。
I'm trying to mock my backend, which is well tested so I can completely isolate my frontend. I've tried using protractor-http-mock and also various efforts with angular-mocks.
我试着模仿我的后端,它经过了很好的测试,所以我可以完全隔离我的前端。我尝试过使用protracer -http-mock,也尝试过使用angular-mocks。
Having settled on the angular-mocks method with HttpBackend, I'm getting this error when firing up my protractor tests:
在使用httpback解决了angular-mocks方法之后,在启动量角器测试时,我将得到这个错误:
MBP:test-site admin$ protractor protractor.conf.js
Using ChromeDriver directly...
[launcher] Running 1 instances of WebDriver
[launcher] Error: ReferenceError: window is not defined
at Object.<anonymous> (/Users/Ed/Sites/F4F/web/node_modules/angular/angular.js:30426:4)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/Users/Ed/Sites/F4F/web/node_modules/angular/index.js:1:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
[launcher] Process exited with error code 100
This is my protractor.conf.js
这是我protractor.conf.js
exports.config = {
directConnect: true,
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},
chromeOnly: true,
chromeDriver: './node_modules/protractor/selenium/chromedriver',
// Framework to use. Jasmine 2 is recommended.
framework: 'jasmine2',
baseUrl: 'http://localhost:5000/',
seleniumAddress: 'http://127.0.0.1:4444/wd/hub',
// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['./tests/**/*.js'],
// Options to be passed to Jasmine.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
}
};
And this is my test:
这是我的测试:
'use strict'
var angular = require('angular');
var angular_mock = require('angular-mocks');
var HttpBackend = require('httpbackend');
var backend = null;
describe("Footer", function () {
beforeEach(function() {
backend = new HttpBackend(browser);
});
afterEach(function() {
backend.clear();
});
describe("Display Values", function () {
it("should show correct contact details", function () {
backend.whenGET(/app/).respond({
"name": "ExampleApp",
"company": {
"code": "EXA",
"name": "ExampleApp",
"brand_name": "ExampleApp",
"head_office_id": 3,
"display_email": "sales@example.com",
"display_tel": "+44 (0) 1234 56789"
}
});
browser.get('/');
var tel_li = $('#footer .top li:first-child');
var email_li = $('#footer .top li:last-child');
expect(tel_li.getText()).toEqual('+44 (0) 1234 56789');
expect(email_li.getText()).toEqual('sales@example.com');
});
});
});
Can anybody help please?
有人能帮助好吗?
--- Responding to @alecxe's comment
---回复@alecxe的评论
Revising the test to look like this:
修改测试如下:
'use strict'
describe("Footer", function () {
describe("Display Values", function () {
it("should show correct contact details", function () {
browser.get('/');
});
});
});
Gives the following result:
给出了以下结果:
MBP:test-site admin$ protractor protractor.conf.js
Using ChromeDriver directly...
[launcher] Running 1 instances of WebDriver
Started
.
1 spec, 0 failures
Finished in 0.749 seconds
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 passed
2 个解决方案
#1
1
Don't import angular-mocks
and angular
at all, httpbackend
don't need them to be imported:
不要导入角度-模拟和角度,http后端不需要导入:
'use strict'
var HttpBackend = require('httpbackend');
var backend = null;
describe("Footer", function () {
beforeEach(function() {
backend = new HttpBackend(browser);
});
afterEach(function() {
backend.clear();
});
describe("Display Values", function () {
it("should show correct contact details", function () {
backend.whenGET(/app/).respond({
"name": "ExampleApp",
"company": {
"code": "EXA",
"name": "ExampleApp",
"brand_name": "ExampleApp",
"head_office_id": 3,
"display_email": "sales@example.com",
"display_tel": "+44 (0) 1234 56789"
}
});
browser.get('/');
var tel_li = $('#footer .top li:first-child');
var email_li = $('#footer .top li:last-child');
expect(tel_li.getText()).toEqual('+44 (0) 1234 56789');
expect(email_li.getText()).toEqual('sales@example.com');
});
});
});
#2
1
I had your exact issue, and ended up solving it by importing angular-mocks
as a raw string and injecting it via browser.addMockModule
:
我有你确切的问题,最后我把angular-mocks作为原始字符串导入并通过browser.addMockModule注入。
const fs = require('fs');
const ngMock = fs.readFileSync(__dirname + '/../../node_modules/angular-mocks/angular-mocks.js', 'utf-8');
browser.addMockModule('ngMockE2E', ngMock);
browser.addMockModule('e2e', function e2e() {
angular.module('e2e', ['ngMockE2E'])
.run(['$httpBackend', function($httpBackend) {
// Your mocked calls here
}])
});
This will force the driver to compile the script during runtime, where window
is available. However, this breaks if you have browser.ignoreSynchronization = true;
however.
这将迫使驱动程序在运行时(窗口可用)编译脚本。但是,如果你有浏览器,这就会中断。ignoreSynchronization = true;然而。
#1
1
Don't import angular-mocks
and angular
at all, httpbackend
don't need them to be imported:
不要导入角度-模拟和角度,http后端不需要导入:
'use strict'
var HttpBackend = require('httpbackend');
var backend = null;
describe("Footer", function () {
beforeEach(function() {
backend = new HttpBackend(browser);
});
afterEach(function() {
backend.clear();
});
describe("Display Values", function () {
it("should show correct contact details", function () {
backend.whenGET(/app/).respond({
"name": "ExampleApp",
"company": {
"code": "EXA",
"name": "ExampleApp",
"brand_name": "ExampleApp",
"head_office_id": 3,
"display_email": "sales@example.com",
"display_tel": "+44 (0) 1234 56789"
}
});
browser.get('/');
var tel_li = $('#footer .top li:first-child');
var email_li = $('#footer .top li:last-child');
expect(tel_li.getText()).toEqual('+44 (0) 1234 56789');
expect(email_li.getText()).toEqual('sales@example.com');
});
});
});
#2
1
I had your exact issue, and ended up solving it by importing angular-mocks
as a raw string and injecting it via browser.addMockModule
:
我有你确切的问题,最后我把angular-mocks作为原始字符串导入并通过browser.addMockModule注入。
const fs = require('fs');
const ngMock = fs.readFileSync(__dirname + '/../../node_modules/angular-mocks/angular-mocks.js', 'utf-8');
browser.addMockModule('ngMockE2E', ngMock);
browser.addMockModule('e2e', function e2e() {
angular.module('e2e', ['ngMockE2E'])
.run(['$httpBackend', function($httpBackend) {
// Your mocked calls here
}])
});
This will force the driver to compile the script during runtime, where window
is available. However, this breaks if you have browser.ignoreSynchronization = true;
however.
这将迫使驱动程序在运行时(窗口可用)编译脚本。但是,如果你有浏览器,这就会中断。ignoreSynchronization = true;然而。