Skip to content

test: use http endpoint instead of local file for codegen tests #36498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
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
50 changes: 24 additions & 26 deletions tests/library/inspector/cli-codegen-csharp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
* limitations under the License.
*/

import path from 'path';
import fs from 'fs';
import { test, expect } from './inspectorTest';

const emptyHTML = new URL('file://' + path.join(__dirname, '..', '..', 'assets', 'empty.html')).toString();
const launchOptions = (channel: string) => {
return channel ? `Channel = "${channel}",\n Headless = false,` : `Headless = false,`;
};
Expand All @@ -27,8 +25,8 @@ function capitalize(browserName: string): string {
return browserName[0].toUpperCase() + browserName.slice(1);
}

test('should print the correct imports and context options', async ({ browserName, channel, runCLI }) => {
const cli = runCLI(['--target=csharp', emptyHTML]);
test('should print the correct imports and context options', async ({ browserName, channel, runCLI, server }) => {
const cli = runCLI(['--target=csharp', server.EMPTY_PAGE]);
const expectedResult = `using Microsoft.Playwright;
using System;
using System.Threading.Tasks;
Expand All @@ -42,7 +40,7 @@ var context = await browser.NewContextAsync();`;
await cli.waitFor(expectedResult);
});

test('should print the correct context options for custom settings', async ({ browserName, channel, runCLI }) => {
test('should print the correct context options for custom settings', async ({ browserName, channel, runCLI, server }) => {
const cli = runCLI([
'--color-scheme=dark',
'--geolocation=37.819722,-122.478611',
Expand All @@ -52,7 +50,7 @@ test('should print the correct context options for custom settings', async ({ br
'--user-agent=hardkodemium',
'--viewport-size=1280,720',
'--target=csharp',
emptyHTML]);
server.EMPTY_PAGE]);
const expectedResult = `
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
Expand Down Expand Up @@ -84,10 +82,10 @@ var context = await browser.NewContextAsync(new BrowserNewContextOptions
await cli.waitFor(expectedResult);
});

test('should print the correct context options when using a device', async ({ browserName, channel, runCLI }) => {
test('should print the correct context options when using a device', async ({ browserName, channel, runCLI, server }) => {
test.skip(browserName !== 'chromium');

const cli = runCLI(['--device=Pixel 2', '--target=csharp', emptyHTML]);
const cli = runCLI(['--device=Pixel 2', '--target=csharp', server.EMPTY_PAGE]);
const expectedResult = `
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
Expand All @@ -98,7 +96,7 @@ var context = await browser.NewContextAsync(playwright.Devices["Pixel 2"]);`;
await cli.waitFor(expectedResult);
});

test('should print the correct context options when using a device and additional options', async ({ browserName, channel, runCLI }) => {
test('should print the correct context options when using a device and additional options', async ({ browserName, channel, runCLI, server }) => {
test.skip(browserName !== 'webkit');

const cli = runCLI([
Expand All @@ -111,7 +109,7 @@ test('should print the correct context options when using a device and additiona
'--user-agent=hardkodemium',
'--viewport-size=1280,720',
'--target=csharp',
emptyHTML]);
server.EMPTY_PAGE]);
const expectedResult = `
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
Expand Down Expand Up @@ -143,11 +141,11 @@ var context = await browser.NewContextAsync(new BrowserNewContextOptions(playwri
await cli.waitFor(expectedResult);
});

test('should print load/save storageState', async ({ browserName, channel, runCLI }, testInfo) => {
test('should print load/save storageState', async ({ browserName, channel, runCLI, server }, testInfo) => {
const loadFileName = testInfo.outputPath('load.json');
const saveFileName = testInfo.outputPath('save.json');
await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8');
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=csharp', emptyHTML]);
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=csharp', server.EMPTY_PAGE]);
const expectedResult1 = `
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
Expand Down Expand Up @@ -194,15 +192,15 @@ test('should work with --save-har and --save-har-glob', async ({ runCLI }, testI
});

for (const testFramework of ['nunit', 'mstest'] as const) {
test(`should not print context options method override in ${testFramework} if no options were passed`, async ({ runCLI }) => {
const cli = runCLI([`--target=csharp-${testFramework}`, emptyHTML]);
await cli.waitFor(`Page.GotoAsync("${emptyHTML}")`);
test(`should not print context options method override in ${testFramework} if no options were passed`, async ({ runCLI, server }) => {
const cli = runCLI([`--target=csharp-${testFramework}`, server.EMPTY_PAGE]);
await cli.waitFor(`Page.GotoAsync("${server.EMPTY_PAGE}")`);
expect(cli.text()).not.toContain('public override BrowserNewContextOptions ContextOptions()');
});

test(`should print context options method override in ${testFramework} if options were passed`, async ({ runCLI }) => {
const cli = runCLI([`--target=csharp-${testFramework}`, '--color-scheme=dark', emptyHTML]);
await cli.waitFor(`Page.GotoAsync("${emptyHTML}")`);
test(`should print context options method override in ${testFramework} if options were passed`, async ({ runCLI, server }) => {
const cli = runCLI([`--target=csharp-${testFramework}`, '--color-scheme=dark', server.EMPTY_PAGE]);
await cli.waitFor(`Page.GotoAsync("${server.EMPTY_PAGE}")`);
expect(cli.text()).toContain(` public override BrowserNewContextOptions ContextOptions()
{
return new BrowserNewContextOptions
Expand Down Expand Up @@ -239,9 +237,9 @@ for (const testFramework of ['nunit', 'mstest'] as const) {
});
}

test(`should print a valid basic program in mstest`, async ({ runCLI }) => {
const cli = runCLI([`--target=csharp-mstest`, '--color-scheme=dark', emptyHTML]);
await cli.waitFor(`Page.GotoAsync("${emptyHTML}")`);
test(`should print a valid basic program in mstest`, async ({ runCLI, server }) => {
const cli = runCLI([`--target=csharp-mstest`, '--color-scheme=dark', server.EMPTY_PAGE]);
await cli.waitFor(`Page.GotoAsync("${server.EMPTY_PAGE}")`);
const expected = `using Microsoft.Playwright.MSTest;
using Microsoft.Playwright;

Expand All @@ -259,15 +257,15 @@ public class Tests : PageTest
[TestMethod]
public async Task MyTest()
{
await Page.GotoAsync("${emptyHTML}");
await Page.GotoAsync("${server.EMPTY_PAGE}");
}
}`;
expect(cli.text()).toContain(expected);
});

test(`should print a valid basic program in nunit`, async ({ runCLI }) => {
const cli = runCLI([`--target=csharp-nunit`, '--color-scheme=dark', emptyHTML]);
await cli.waitFor(`Page.GotoAsync("${emptyHTML}")`);
test(`should print a valid basic program in nunit`, async ({ runCLI, server }) => {
const cli = runCLI([`--target=csharp-nunit`, '--color-scheme=dark', server.EMPTY_PAGE]);
await cli.waitFor(`Page.GotoAsync("${server.EMPTY_PAGE}")`);
const expected = `using Microsoft.Playwright.NUnit;
using Microsoft.Playwright;

Expand All @@ -286,7 +284,7 @@ public class Tests : PageTest
[Test]
public async Task MyTest()
{
await Page.GotoAsync("${emptyHTML}");
await Page.GotoAsync("${server.EMPTY_PAGE}");
}
}`;
expect(cli.text()).toContain(expected);
Expand Down
32 changes: 15 additions & 17 deletions tests/library/inspector/cli-codegen-java.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
*/

import fs from 'fs';
import path from 'path';
import { test, expect } from './inspectorTest';

const emptyHTML = new URL('file://' + path.join(__dirname, '..', '..', 'assets', 'empty.html')).toString();
const launchOptions = (channel: string) => {
return channel ? `.setChannel("${channel}")\n .setHeadless(false)` : '.setHeadless(false)';
};

test('should print the correct imports and context options', async ({ runCLI, channel, browserName }) => {
const cli = runCLI(['--target=java', emptyHTML]);
test('should print the correct imports and context options', async ({ runCLI, channel, browserName, server }) => {
const cli = runCLI(['--target=java', server.EMPTY_PAGE]);
const expectedResult = `import com.microsoft.playwright.*;
import com.microsoft.playwright.options.*;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
Expand All @@ -39,17 +37,17 @@ public class Example {
await cli.waitFor(expectedResult);
});

test('should print the correct context options for custom settings', async ({ runCLI, browserName }) => {
const cli = runCLI(['--color-scheme=light', '--target=java', emptyHTML]);
test('should print the correct context options for custom settings', async ({ runCLI, server }) => {
const cli = runCLI(['--color-scheme=light', '--target=java', server.EMPTY_PAGE]);
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setColorScheme(ColorScheme.LIGHT));`;
await cli.waitFor(expectedResult);
});

test('should print the correct context options when using a device', async ({ browserName, runCLI }) => {
test('should print the correct context options when using a device', async ({ browserName, runCLI, server }) => {
test.skip(browserName !== 'chromium');

const cli = runCLI(['--device=Pixel 2', '--target=java', emptyHTML]);
const cli = runCLI(['--device=Pixel 2', '--target=java', server.EMPTY_PAGE]);
await cli.waitFor(`.setViewportSize(411, 731));`);
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setDeviceScaleFactor(2.625)
Expand All @@ -60,10 +58,10 @@ test('should print the correct context options when using a device', async ({ br
expect(cli.text().replace(/(.*Chrome\/)(.*?)( .*)/m, '$1XXXX$3')).toContain(expectedResult);
});

test('should print the correct context options when using a device and additional options', async ({ browserName, runCLI }) => {
test('should print the correct context options when using a device and additional options', async ({ browserName, runCLI, server }) => {
test.skip(browserName !== 'webkit');

const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=java', emptyHTML]);
const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=java', server.EMPTY_PAGE]);
await cli.waitFor(`.setViewportSize(414, 715));`);
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setColorScheme(ColorScheme.LIGHT)
Expand All @@ -75,11 +73,11 @@ test('should print the correct context options when using a device and additiona
expect(cli.text().replace(/(.*Version\/)(.*?)( .*)/m, '$1XXXX$3')).toContain(expectedResult);
});

test('should print load/save storage_state', async ({ runCLI, browserName }, testInfo) => {
test('should print load/save storage_state', async ({ runCLI, server }, testInfo) => {
const loadFileName = testInfo.outputPath('load.json');
const saveFileName = testInfo.outputPath('save.json');
await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8');
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=java', emptyHTML]);
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=java', server.EMPTY_PAGE]);
const expectedResult1 = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setStorageStatePath(Paths.get(${JSON.stringify(loadFileName)})));`;
await cli.waitFor(expectedResult1);
Expand Down Expand Up @@ -115,8 +113,8 @@ test('should work with --save-har and --save-har-glob as java-junit', async ({ r
expect(json.log.creator.name).toBe('Playwright');
});

test('should print the correct imports in junit', async ({ runCLI, channel, browserName }) => {
const cli = runCLI(['--target=java-junit', emptyHTML]);
test('should print the correct imports in junit', async ({ runCLI, server }) => {
const cli = runCLI(['--target=java-junit', server.EMPTY_PAGE]);
const expectedImportResult = `import com.microsoft.playwright.junit.UsePlaywright;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.options.*;
Expand All @@ -126,8 +124,8 @@ import static com.microsoft.playwright.assertions.PlaywrightAssertions.*;`;
await cli.waitFor(expectedImportResult);
});

test('should print a valid basic program in junit', async ({ runCLI, channel, browserName }) => {
const cli = runCLI(['--target=java-junit', emptyHTML]);
test('should print a valid basic program in junit', async ({ runCLI, server }) => {
const cli = runCLI(['--target=java-junit', server.EMPTY_PAGE]);
const expectedResult = `import com.microsoft.playwright.junit.UsePlaywright;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.options.*;
Expand All @@ -139,7 +137,7 @@ import static com.microsoft.playwright.assertions.PlaywrightAssertions.*;
public class TestExample {
@Test
void test(Page page) {
page.navigate("${emptyHTML}");
page.navigate("${server.EMPTY_PAGE}");
}
}`;
await cli.waitFor(expectedResult);
Expand Down
29 changes: 13 additions & 16 deletions tests/library/inspector/cli-codegen-javascript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
*/

import fs from 'fs';
import path from 'path';
import { test, expect } from './inspectorTest';

const emptyHTML = new URL('file://' + path.join(__dirname, '..', '..', 'assets', 'empty.html')).toString();

const launchOptions = (channel: string) => {
return channel ? `channel: '${channel}',\n headless: false` : 'headless: false';
};

test('should print the correct imports and context options', async ({ browserName, channel, runCLI }) => {
const cli = runCLI(['--target=javascript', emptyHTML]);
test('should print the correct imports and context options', async ({ browserName, channel, runCLI, server }) => {
const cli = runCLI(['--target=javascript', server.EMPTY_PAGE]);
const expectedResult = `const { ${browserName} } = require('playwright');

(async () => {
Expand All @@ -36,8 +33,8 @@ test('should print the correct imports and context options', async ({ browserNam
await cli.waitFor(expectedResult);
});

test('should print the correct context options for custom settings', async ({ browserName, channel, runCLI }) => {
const cli = runCLI(['--color-scheme=light', '--target=javascript', emptyHTML]);
test('should print the correct context options for custom settings', async ({ browserName, channel, runCLI, server }) => {
const cli = runCLI(['--color-scheme=light', '--target=javascript', server.EMPTY_PAGE]);
const expectedResult = `const { ${browserName} } = require('playwright');

(async () => {
Expand All @@ -51,10 +48,10 @@ test('should print the correct context options for custom settings', async ({ br
});


test('should print the correct context options when using a device', async ({ browserName, channel, runCLI }) => {
test('should print the correct context options when using a device', async ({ browserName, channel, runCLI, server }) => {
test.skip(browserName !== 'chromium');

const cli = runCLI(['--device=Pixel 2', '--target=javascript', emptyHTML]);
const cli = runCLI(['--device=Pixel 2', '--target=javascript', server.EMPTY_PAGE]);
const expectedResult = `const { chromium, devices } = require('playwright');

(async () => {
Expand All @@ -67,10 +64,10 @@ test('should print the correct context options when using a device', async ({ br
await cli.waitFor(expectedResult);
});

test('should print the correct context options when using a device and additional options', async ({ browserName, channel, runCLI }) => {
test('should print the correct context options when using a device and additional options', async ({ browserName, channel, runCLI, server }) => {
test.skip(browserName !== 'webkit');

const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=javascript', emptyHTML]);
const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=javascript', server.EMPTY_PAGE]);
const expectedResult = `const { webkit, devices } = require('playwright');

(async () => {
Expand All @@ -84,9 +81,9 @@ test('should print the correct context options when using a device and additiona
await cli.waitFor(expectedResult);
});

test('should save the codegen output to a file if specified', async ({ browserName, channel, runCLI }, testInfo) => {
test('should save the codegen output to a file if specified', async ({ browserName, channel, runCLI, server }, testInfo) => {
const tmpFile = testInfo.outputPath('script.js');
const cli = runCLI(['--output', tmpFile, '--target=javascript', emptyHTML], {
const cli = runCLI(['--output', tmpFile, '--target=javascript', server.EMPTY_PAGE], {
autoExitWhen: 'await page.goto', // We have to wait for the initial navigation to be recorded.
});
await cli.waitForCleanExit();
Expand All @@ -99,7 +96,7 @@ test('should save the codegen output to a file if specified', async ({ browserNa
});
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('${emptyHTML}');
await page.goto('${server.EMPTY_PAGE}');
await page.close();

// ---------------------
Expand All @@ -108,11 +105,11 @@ test('should save the codegen output to a file if specified', async ({ browserNa
})();`);
});

test('should print load/save storageState', async ({ browserName, channel, runCLI }, testInfo) => {
test('should print load/save storageState', async ({ browserName, channel, runCLI, server }, testInfo) => {
const loadFileName = testInfo.outputPath('load.json');
const saveFileName = testInfo.outputPath('save.json');
await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8');
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=javascript', emptyHTML]);
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=javascript', server.EMPTY_PAGE]);
const expectedResult1 = `const { ${browserName} } = require('playwright');

(async () => {
Expand Down
Loading
Loading