diff --git a/src/Playwright.TestingHarnessTest/tests/baseTest.ts b/src/Playwright.TestingHarnessTest/tests/baseTest.ts index 968eba1dc..14602c7c2 100644 --- a/src/Playwright.TestingHarnessTest/tests/baseTest.ts +++ b/src/Playwright.TestingHarnessTest/tests/baseTest.ts @@ -21,7 +21,8 @@ export const test = base.extend<{ proxyServer: ProxyServer; testMode: 'nunit' | 'mstest' | 'xunit'; runTest: (files: Record, command: string, env?: NodeJS.ProcessEnv) => Promise; - launchServer: ({ port: number }) => Promise; + launchServer: (options: { port: number }) => Promise; + server: SimpleServer; }>({ proxyServer: async ({}, use) => { const proxyServer = new ProxyServer(); @@ -29,6 +30,12 @@ export const test = base.extend<{ await use(proxyServer); await proxyServer.stop(); }, + server: async ({}, use) => { + const server = new SimpleServer(); + await server.listen(); + await use(server); + await server.stop(); + }, testMode: null, launchServer: async ({ playwright }, use) => { const servers: BrowserServer[] = []; @@ -183,4 +190,38 @@ class ProxyServer { } } +class SimpleServer { + private _server: http.Server; + + constructor() { + this._server = http.createServer(this.handler.bind(this)); + } + + handler(req: http.IncomingMessage, res: http.ServerResponse) { + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(` + + + Test Server + + +

Test Server

+

This is a simple test server for Playwright tests.

+ +`); + } + + get EMPTY_PAGE() { + return `http://127.0.0.1:${(this._server.address() as AddressInfo).port}/empty.html`; + } + + async listen() { + await new Promise(resolve => this._server.listen(0, resolve)); + } + + async stop() { + await new Promise(resolve => this._server.close(() => resolve())); + } +} + export { expect } from '@playwright/test'; diff --git a/src/Playwright.TestingHarnessTest/tests/mstest/basic.spec.ts b/src/Playwright.TestingHarnessTest/tests/mstest/basic.spec.ts index c1d8e1ae6..a192ff572 100644 --- a/src/Playwright.TestingHarnessTest/tests/mstest/basic.spec.ts +++ b/src/Playwright.TestingHarnessTest/tests/mstest/basic.spec.ts @@ -223,7 +223,7 @@ test('should be able to parse BrowserName and LaunchOptions.Headless from runset expect(result.stdout).not.toContain("Headless") }); -test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer }) => { +test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer, server }) => { const result = await runTest({ 'ExampleTests.cs': ` using System; @@ -240,7 +240,7 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru public async Task Test() { Console.WriteLine("User-Agent: " + await Page.EvaluateAsync("() => navigator.userAgent")); - await Page.GotoAsync("http://example.com"); + await Page.GotoAsync("${server.EMPTY_PAGE}"); } }`, '.runsettings': ` @@ -266,8 +266,8 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru expect(result.stdout).not.toContain("Headless"); - const { url, auth } = proxyServer.requests.find(r => r.url === 'http://example.com/')!;; - expect(url).toBe('http://example.com/'); + const { url, auth } = proxyServer.requests.find(r => r.url === server.EMPTY_PAGE)!; + expect(url).toBe(server.EMPTY_PAGE); expect(auth).toBe('user:pwd'); }); @@ -307,7 +307,7 @@ test('should be able to parse LaunchOptions.Args from runsettings', async ({ run expect(result.stdout).toContain("User-Agent: hello") }); -test('should be able to override context options', async ({ runTest }) => { +test('should be able to override context options', async ({ runTest, server }) => { const result = await runTest({ 'ExampleTests.cs': ` using System; @@ -335,7 +335,7 @@ test('should be able to override context options', async ({ runTest }) => { Assert.AreEqual("Foobar", await Page.EvaluateAsync("() => navigator.userAgent")); - var response = await Page.GotoAsync("https://example.com/"); + var response = await Page.GotoAsync("${server.EMPTY_PAGE}"); Assert.AreEqual(await response.Request.HeaderValueAsync("Kekstar"), "KekStarValue"); } diff --git a/src/Playwright.TestingHarnessTest/tests/nunit/basic.spec.ts b/src/Playwright.TestingHarnessTest/tests/nunit/basic.spec.ts index 995ce49b3..27b346295 100644 --- a/src/Playwright.TestingHarnessTest/tests/nunit/basic.spec.ts +++ b/src/Playwright.TestingHarnessTest/tests/nunit/basic.spec.ts @@ -221,7 +221,7 @@ test('should be able to parse BrowserName and LaunchOptions.Headless from runset expect(result.stdout).not.toContain("Headless") }); -test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer }) => { +test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer, server }) => { const result = await runTest({ 'ExampleTests.cs': ` using System; @@ -237,7 +237,7 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru public async Task Test() { Console.WriteLine("User-Agent: " + await Page.EvaluateAsync("() => navigator.userAgent")); - await Page.GotoAsync("http://example.com"); + await Page.GotoAsync("${server.EMPTY_PAGE}"); } }`, '.runsettings': ` @@ -263,8 +263,8 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru expect(result.stdout).not.toContain("Headless"); - const { url, auth } = proxyServer.requests.find(r => r.url === 'http://example.com/')!;; - expect(url).toBe('http://example.com/'); + const { url, auth } = proxyServer.requests.find(r => r.url === server.EMPTY_PAGE)!; + expect(url).toBe(server.EMPTY_PAGE); expect(auth).toBe('user:pwd'); }); @@ -303,7 +303,7 @@ test('should be able to parse LaunchOptions.Args from runsettings', async ({ run expect(result.stdout).toContain("User-Agent: hello") }); -test('should be able to override context options', async ({ runTest }) => { +test('should be able to override context options', async ({ runTest, server }) => { const result = await runTest({ 'ExampleTests.cs': ` using System; @@ -330,7 +330,7 @@ test('should be able to override context options', async ({ runTest }) => { Assert.AreEqual("Foobar", await Page.EvaluateAsync("() => navigator.userAgent")); - var response = await Page.GotoAsync("https://example.com/"); + var response = await Page.GotoAsync("${server.EMPTY_PAGE}"); Assert.AreEqual(await response.Request.HeaderValueAsync("Kekstar"), "KekStarValue"); } diff --git a/src/Playwright.TestingHarnessTest/tests/xunit/basic.spec.ts b/src/Playwright.TestingHarnessTest/tests/xunit/basic.spec.ts index e6c603c97..d3a15039e 100644 --- a/src/Playwright.TestingHarnessTest/tests/xunit/basic.spec.ts +++ b/src/Playwright.TestingHarnessTest/tests/xunit/basic.spec.ts @@ -251,7 +251,7 @@ test('should be able to parse BrowserName and LaunchOptions.Headless from runset expect(result.stdout).not.toContain("Headless") }); -test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer }) => { +test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer, server }) => { const result = await runTest({ 'ExampleTests.cs': ` using System; @@ -275,7 +275,7 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru public async Task Test() { output.WriteLine("User-Agent: " + await Page.EvaluateAsync("() => navigator.userAgent")); - await Page.GotoAsync("http://example.com"); + await Page.GotoAsync("${server.EMPTY_PAGE}"); } }`, '.runsettings': ` @@ -301,8 +301,8 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru expect(result.stdout).not.toContain("Headless"); - const { url, auth } = proxyServer.requests.find(r => r.url === 'http://example.com/')!;; - expect(url).toBe('http://example.com/'); + const { url, auth } = proxyServer.requests.find(r => r.url === server.EMPTY_PAGE)!; + expect(url).toBe(server.EMPTY_PAGE); expect(auth).toBe('user:pwd'); }); @@ -349,7 +349,7 @@ test('should be able to parse LaunchOptions.Args from runsettings', async ({ run expect(result.stdout).toContain("User-Agent: hello") }); -test('should be able to override context options', async ({ runTest }) => { +test('should be able to override context options', async ({ runTest, server }) => { const result = await runTest({ 'ExampleTests.cs': ` using System; @@ -376,7 +376,7 @@ test('should be able to override context options', async ({ runTest }) => { Assert.Equal("Foobar", await Page.EvaluateAsync("() => navigator.userAgent")); - var response = await Page.GotoAsync("https://example.com/"); + var response = await Page.GotoAsync("${server.EMPTY_PAGE}"); Assert.Equal("KekStarValue", await response.Request.HeaderValueAsync("Kekstar")); }