1- import assert from 'node:assert' ;
1+ import assert from 'node:assert/strict' ;
2+ import { setTimeout } from 'node:timers/promises' ;
23import { getGlobalVariable } from '../../utils/env' ;
34import { expectFileToMatch , writeFile , writeMultipleFiles } from '../../utils/fs' ;
45import { findFreePort } from '../../utils/network' ;
56import { execAndWaitForOutputToMatch , ng } from '../../utils/process' ;
67import { updateJsonFile } from '../../utils/project' ;
8+ import { executeBrowserTest } from '../../utils/puppeteer' ;
79
810const CSP_META_TAG = / < m e t a h t t p - e q u i v = " C o n t e n t - S e c u r i t y - P o l i c y " / ;
911
@@ -67,55 +69,6 @@ export default async function () {
6769 </body>
6870 </html>
6971 ` ,
70- 'e2e/src/app.e2e-spec.ts' : `
71- import { browser, by, element } from 'protractor';
72- import * as webdriver from 'selenium-webdriver';
73-
74- function allConsoleWarnMessagesAndErrors() {
75- return browser
76- .manage()
77- .logs()
78- .get('browser')
79- .then(function (browserLog: any[]) {
80- const warnMessages: any[] = [];
81- browserLog.filter((logEntry) => {
82- const msg = logEntry.message;
83- console.log('>> ' + msg);
84- if (logEntry.level.value >= webdriver.logging.Level.INFO.value) {
85- warnMessages.push(msg);
86- }
87- });
88- return warnMessages;
89- });
90- }
91-
92- describe('Hello world E2E Tests', () => {
93- beforeAll(async () => {
94- await browser.waitForAngularEnabled(true);
95- });
96-
97- it('should display: Welcome and run all scripts in order', async () => {
98- // Load the page without waiting for Angular since it is not bootstrapped automatically.
99- await browser.driver.get(browser.baseUrl);
100-
101- // Test the contents.
102- expect(await element(by.css('h1')).getText()).toMatch('Hello');
103-
104- // Make sure all scripts ran and there were no client side errors.
105- const consoleMessages = await allConsoleWarnMessagesAndErrors();
106- expect(consoleMessages.length).toEqual(4); // No additional errors
107- // Extract just the printed messages from the console data.
108- const printedMessages = consoleMessages.map(m => m.match(/"(.*?)"/)[1]);
109- expect(printedMessages).toEqual([
110- // All messages printed in order because execution order is preserved.
111- "Inline Script Head",
112- "Inline Script Body: 1339",
113- "First External Script: 1338",
114- "Second External Script: 1337",
115- ]);
116- });
117- });
118- ` ,
11972 } ) ;
12073
12174 async function spawnServer ( ) : Promise < number > {
@@ -137,7 +90,49 @@ export default async function () {
13790 // Make sure if contains the critical CSS inlining CSP code.
13891 await expectFileToMatch ( 'dist/test-project/browser/index.html' , 'ngCspMedia' ) ;
13992
140- // Make sure that our e2e protractor tests run to confirm that our angular project runs.
93+ // Make sure that our e2e tests run to confirm that our angular project runs.
14194 const port = await spawnServer ( ) ;
142- await ng ( 'e2e' , `--base-url=http://localhost:${ port } ` , '--dev-server-target=' ) ;
95+ await executeBrowserTest ( {
96+ baseUrl : `http://localhost:${ port } /` ,
97+ checkFn : async ( page ) => {
98+ const warnMessages : string [ ] = [ ] ;
99+ page . on ( 'console' , ( msg ) => {
100+ if ( msg . type ( ) === 'warning' ) {
101+ warnMessages . push ( msg . text ( ) ) ;
102+ }
103+ } ) ;
104+
105+ // Reload to ensure we capture messages from the start if needed,
106+ // although executeBrowserTest already navigated.
107+ await page . reload ( ) ;
108+
109+ // Wait for the expected number of warnings
110+ let retries = 50 ;
111+ while ( warnMessages . length < 4 && retries > 0 ) {
112+ await setTimeout ( 100 ) ;
113+ retries -- ;
114+ }
115+
116+ assert . strictEqual (
117+ warnMessages . length ,
118+ 4 ,
119+ `Expected 4 console warnings, but got ${ warnMessages . length } :\n${ warnMessages . join ( '\n' ) } ` ,
120+ ) ;
121+
122+ const expectedMessages = [
123+ 'Inline Script Head' ,
124+ 'Inline Script Body: 1339' ,
125+ 'First External Script: 1338' ,
126+ 'Second External Script: 1337' ,
127+ ] ;
128+
129+ for ( let i = 0 ; i < expectedMessages . length ; i ++ ) {
130+ if ( ! warnMessages [ i ] . includes ( expectedMessages [ i ] ) ) {
131+ assert . fail (
132+ `Expected warning ${ i } to include '${ expectedMessages [ i ] } ', but got '${ warnMessages [ i ] } '` ,
133+ ) ;
134+ }
135+ }
136+ } ,
137+ } ) ;
143138}
0 commit comments