Closed hyrongo closed 6 years ago
Hi,
This requires some investigation. I’ll look into it hopefully till the end of the week and let you the result.
This requires some investigation. I’ll look into it hopefully till the end of the week and let you the result.
Thank you very much!
Hi @iport2018,
I’ve created a demo for you in the example project: https://github.com/yevhenpavliuk/ng-mock-e2e-example/commit/8ecead84af4802e6e54d2a780a912a52bac8aaec
If you’re unfamiliar with the project, check it out first — it’s a tiny demo for ngMockE2E
: https://github.com/yevhenpavliuk/ng-mock-e2e-example
I added:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ngMockE2E Example</title>
</head>
<body ng-app="example">
<h1>{{::heading}}</h1>
<script src="bower_components/angular/angular.js"></script>
<script src="app.js"></script>
</body>
</html>
index.html
— that opens the popup with window name popup
:
<button type="button" id="open-popup-button" onclick="window.open('popup.html', 'popup', 'width=640,height=480')">Open Popup</button>
Modified one of the tests to show how to switch to the popup, make assertions there, and switch back to the main window:
fit('should have heading "It works!" if the server responds "It works!"', async () => {
$httpBackend.when('GET', 'heading').respond('It works!');
browser.get('/');
expect(browser.getCurrentUrl()).toBe('http://localhost:8000/');
expect($('h1').getText()).toEqual('It works!');
$('#open-popup-button').click();
await browser.switchTo().window('popup');
ngMockE2E.addMockModule();
ngMockE2E.addAsDependencyForModule('example');
ngMockE2E.embedScript('bower_components/angular-mocks/angular-mocks.js');
$httpBackend.when('GET', 'heading').respond('It works in a popup!');
browser.refresh();
expect($('h1').getText()).toEqual('It works in a popup!');
expect(browser.getCurrentUrl()).toBe('http://localhost:8000/popup.html');
await browser.switchTo().window('')
expect(browser.getCurrentUrl()).toBe('http://localhost:8000/');
expect($('h1').getText()).toEqual('It works!');
});
Please notice the following:
await
ing on switching between windows (f.e. await browser.switchTo().window('popup');
) is absolutely required.ngMockE2E
is set up for the popup page:
ngMockE2E.addMockModule();
ngMockE2E.addAsDependencyForModule('example');
ngMockE2E.embedScript('bower_components/angular-mocks/angular-mocks.js');
$httpBackend.when('GET', 'heading').respond('It works in a popup!');
is applied to the popup page.browser.refresh();
is done because ngMockE2E
setup happens too late. I think there’s a way to overcome this limitation.await browser.switchTo().window('')
switches back to the main window.I hope this will help you.
Great answer, great detail!I've tried your solution and it does work.Thanks! @yevhenpavliuk
You’re welcome!
I am using Protractor and ng-mock-e2e to test my AngularJS application. When I test page(B) which is opened from page(A) by window.open, I hope the $http in page(B) will be faked by $httpBackend but it seems not available.
My Code is like
*(1) This will open pageB in a new window (by window.open())and pageB will access "http://.../heading" in it‘s init.
How can I fake http in a new window that is opened by application.