Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/Playwright.TestingHarnessTest/tests/baseTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ export const test = base.extend<{
proxyServer: ProxyServer;
testMode: 'nunit' | 'mstest' | 'xunit';
runTest: (files: Record<string, string>, command: string, env?: NodeJS.ProcessEnv) => Promise<RunResult>;
launchServer: ({ port: number }) => Promise<void>;
launchServer: (options: { port: number }) => Promise<void>;
server: SimpleServer;
}>({
proxyServer: async ({}, use) => {
const proxyServer = new ProxyServer();
await proxyServer.listen();
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[] = [];
Expand Down Expand Up @@ -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(`<!DOCTYPE html>
<html>
<head>
<title>Test Server</title>
</head>
<body>
<h1>Test Server</h1>
<p>This is a simple test server for Playwright tests.</p>
</body>
</html>`);
}

get EMPTY_PAGE() {
return `http://127.0.0.1:${(this._server.address() as AddressInfo).port}/empty.html`;
}

async listen() {
await new Promise<void>(resolve => this._server.listen(0, resolve));
}

async stop() {
await new Promise<void>(resolve => this._server.close(() => resolve()));
}
}

export { expect } from '@playwright/test';
12 changes: 6 additions & 6 deletions src/Playwright.TestingHarnessTest/tests/mstest/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string>("() => navigator.userAgent"));
await Page.GotoAsync("http://example.com");
await Page.GotoAsync("${server.EMPTY_PAGE}");
}
}`,
'.runsettings': `
Expand All @@ -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');
});

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -335,7 +335,7 @@ test('should be able to override context options', async ({ runTest }) => {

Assert.AreEqual("Foobar", await Page.EvaluateAsync<string>("() => 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");
}

Expand Down
12 changes: 6 additions & 6 deletions src/Playwright.TestingHarnessTest/tests/nunit/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string>("() => navigator.userAgent"));
await Page.GotoAsync("http://example.com");
await Page.GotoAsync("${server.EMPTY_PAGE}");
}
}`,
'.runsettings': `
Expand All @@ -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');
});

Expand Down Expand Up @@ -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;
Expand All @@ -330,7 +330,7 @@ test('should be able to override context options', async ({ runTest }) => {

Assert.AreEqual("Foobar", await Page.EvaluateAsync<string>("() => 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");
}

Expand Down
12 changes: 6 additions & 6 deletions src/Playwright.TestingHarnessTest/tests/xunit/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string>("() => navigator.userAgent"));
await Page.GotoAsync("http://example.com");
await Page.GotoAsync("${server.EMPTY_PAGE}");
}
}`,
'.runsettings': `
Expand All @@ -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');
});

Expand Down Expand Up @@ -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;
Expand All @@ -376,7 +376,7 @@ test('should be able to override context options', async ({ runTest }) => {

Assert.Equal("Foobar", await Page.EvaluateAsync<string>("() => 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"));
}

Expand Down
Loading