Skip to content

Commit 0ef084e

Browse files
authored
test: do not depend on example.com in integration tests (#3214)
1 parent 888c053 commit 0ef084e

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

src/Playwright.TestingHarnessTest/tests/baseTest.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@ export const test = base.extend<{
2121
proxyServer: ProxyServer;
2222
testMode: 'nunit' | 'mstest' | 'xunit';
2323
runTest: (files: Record<string, string>, command: string, env?: NodeJS.ProcessEnv) => Promise<RunResult>;
24-
launchServer: ({ port: number }) => Promise<void>;
24+
launchServer: (options: { port: number }) => Promise<void>;
25+
server: SimpleServer;
2526
}>({
2627
proxyServer: async ({}, use) => {
2728
const proxyServer = new ProxyServer();
2829
await proxyServer.listen();
2930
await use(proxyServer);
3031
await proxyServer.stop();
3132
},
33+
server: async ({}, use) => {
34+
const server = new SimpleServer();
35+
await server.listen();
36+
await use(server);
37+
await server.stop();
38+
},
3239
testMode: null,
3340
launchServer: async ({ playwright }, use) => {
3441
const servers: BrowserServer[] = [];
@@ -183,4 +190,38 @@ class ProxyServer {
183190
}
184191
}
185192

193+
class SimpleServer {
194+
private _server: http.Server;
195+
196+
constructor() {
197+
this._server = http.createServer(this.handler.bind(this));
198+
}
199+
200+
handler(req: http.IncomingMessage, res: http.ServerResponse) {
201+
res.writeHead(200, { 'Content-Type': 'text/html' });
202+
res.end(`<!DOCTYPE html>
203+
<html>
204+
<head>
205+
<title>Test Server</title>
206+
</head>
207+
<body>
208+
<h1>Test Server</h1>
209+
<p>This is a simple test server for Playwright tests.</p>
210+
</body>
211+
</html>`);
212+
}
213+
214+
get EMPTY_PAGE() {
215+
return `http://127.0.0.1:${(this._server.address() as AddressInfo).port}/empty.html`;
216+
}
217+
218+
async listen() {
219+
await new Promise<void>(resolve => this._server.listen(0, resolve));
220+
}
221+
222+
async stop() {
223+
await new Promise<void>(resolve => this._server.close(() => resolve()));
224+
}
225+
}
226+
186227
export { expect } from '@playwright/test';

src/Playwright.TestingHarnessTest/tests/mstest/basic.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ test('should be able to parse BrowserName and LaunchOptions.Headless from runset
223223
expect(result.stdout).not.toContain("Headless")
224224
});
225225

226-
test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer }) => {
226+
test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer, server }) => {
227227
const result = await runTest({
228228
'ExampleTests.cs': `
229229
using System;
@@ -240,7 +240,7 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru
240240
public async Task Test()
241241
{
242242
Console.WriteLine("User-Agent: " + await Page.EvaluateAsync<string>("() => navigator.userAgent"));
243-
await Page.GotoAsync("http://example.com");
243+
await Page.GotoAsync("${server.EMPTY_PAGE}");
244244
}
245245
}`,
246246
'.runsettings': `
@@ -266,8 +266,8 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru
266266

267267
expect(result.stdout).not.toContain("Headless");
268268

269-
const { url, auth } = proxyServer.requests.find(r => r.url === 'http://example.com/')!;;
270-
expect(url).toBe('http://example.com/');
269+
const { url, auth } = proxyServer.requests.find(r => r.url === server.EMPTY_PAGE)!;
270+
expect(url).toBe(server.EMPTY_PAGE);
271271
expect(auth).toBe('user:pwd');
272272
});
273273

@@ -307,7 +307,7 @@ test('should be able to parse LaunchOptions.Args from runsettings', async ({ run
307307
expect(result.stdout).toContain("User-Agent: hello")
308308
});
309309

310-
test('should be able to override context options', async ({ runTest }) => {
310+
test('should be able to override context options', async ({ runTest, server }) => {
311311
const result = await runTest({
312312
'ExampleTests.cs': `
313313
using System;
@@ -335,7 +335,7 @@ test('should be able to override context options', async ({ runTest }) => {
335335
336336
Assert.AreEqual("Foobar", await Page.EvaluateAsync<string>("() => navigator.userAgent"));
337337
338-
var response = await Page.GotoAsync("https://example.com/");
338+
var response = await Page.GotoAsync("${server.EMPTY_PAGE}");
339339
Assert.AreEqual(await response.Request.HeaderValueAsync("Kekstar"), "KekStarValue");
340340
}
341341

src/Playwright.TestingHarnessTest/tests/nunit/basic.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ test('should be able to parse BrowserName and LaunchOptions.Headless from runset
221221
expect(result.stdout).not.toContain("Headless")
222222
});
223223

224-
test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer }) => {
224+
test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer, server }) => {
225225
const result = await runTest({
226226
'ExampleTests.cs': `
227227
using System;
@@ -237,7 +237,7 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru
237237
public async Task Test()
238238
{
239239
Console.WriteLine("User-Agent: " + await Page.EvaluateAsync<string>("() => navigator.userAgent"));
240-
await Page.GotoAsync("http://example.com");
240+
await Page.GotoAsync("${server.EMPTY_PAGE}");
241241
}
242242
}`,
243243
'.runsettings': `
@@ -263,8 +263,8 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru
263263

264264
expect(result.stdout).not.toContain("Headless");
265265

266-
const { url, auth } = proxyServer.requests.find(r => r.url === 'http://example.com/')!;;
267-
expect(url).toBe('http://example.com/');
266+
const { url, auth } = proxyServer.requests.find(r => r.url === server.EMPTY_PAGE)!;
267+
expect(url).toBe(server.EMPTY_PAGE);
268268
expect(auth).toBe('user:pwd');
269269
});
270270

@@ -303,7 +303,7 @@ test('should be able to parse LaunchOptions.Args from runsettings', async ({ run
303303
expect(result.stdout).toContain("User-Agent: hello")
304304
});
305305

306-
test('should be able to override context options', async ({ runTest }) => {
306+
test('should be able to override context options', async ({ runTest, server }) => {
307307
const result = await runTest({
308308
'ExampleTests.cs': `
309309
using System;
@@ -330,7 +330,7 @@ test('should be able to override context options', async ({ runTest }) => {
330330
331331
Assert.AreEqual("Foobar", await Page.EvaluateAsync<string>("() => navigator.userAgent"));
332332
333-
var response = await Page.GotoAsync("https://example.com/");
333+
var response = await Page.GotoAsync("${server.EMPTY_PAGE}");
334334
Assert.AreEqual(await response.Request.HeaderValueAsync("Kekstar"), "KekStarValue");
335335
}
336336

src/Playwright.TestingHarnessTest/tests/xunit/basic.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ test('should be able to parse BrowserName and LaunchOptions.Headless from runset
251251
expect(result.stdout).not.toContain("Headless")
252252
});
253253

254-
test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer }) => {
254+
test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer, server }) => {
255255
const result = await runTest({
256256
'ExampleTests.cs': `
257257
using System;
@@ -275,7 +275,7 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru
275275
public async Task Test()
276276
{
277277
output.WriteLine("User-Agent: " + await Page.EvaluateAsync<string>("() => navigator.userAgent"));
278-
await Page.GotoAsync("http://example.com");
278+
await Page.GotoAsync("${server.EMPTY_PAGE}");
279279
}
280280
}`,
281281
'.runsettings': `
@@ -301,8 +301,8 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru
301301

302302
expect(result.stdout).not.toContain("Headless");
303303

304-
const { url, auth } = proxyServer.requests.find(r => r.url === 'http://example.com/')!;;
305-
expect(url).toBe('http://example.com/');
304+
const { url, auth } = proxyServer.requests.find(r => r.url === server.EMPTY_PAGE)!;
305+
expect(url).toBe(server.EMPTY_PAGE);
306306
expect(auth).toBe('user:pwd');
307307
});
308308

@@ -349,7 +349,7 @@ test('should be able to parse LaunchOptions.Args from runsettings', async ({ run
349349
expect(result.stdout).toContain("User-Agent: hello")
350350
});
351351

352-
test('should be able to override context options', async ({ runTest }) => {
352+
test('should be able to override context options', async ({ runTest, server }) => {
353353
const result = await runTest({
354354
'ExampleTests.cs': `
355355
using System;
@@ -376,7 +376,7 @@ test('should be able to override context options', async ({ runTest }) => {
376376
377377
Assert.Equal("Foobar", await Page.EvaluateAsync<string>("() => navigator.userAgent"));
378378
379-
var response = await Page.GotoAsync("https://example.com/");
379+
var response = await Page.GotoAsync("${server.EMPTY_PAGE}");
380380
Assert.Equal("KekStarValue", await response.Request.HeaderValueAsync("Kekstar"));
381381
}
382382

0 commit comments

Comments
 (0)