Skip to content
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

chore: add _browserTypes helpers to playwright #34611

Merged
merged 13 commits into from
Feb 7, 2025

Conversation

Skn0tt
Copy link
Member

@Skn0tt Skn0tt commented Feb 4, 2025

There's a couple of spots throughout the client where we iterate through all browser types and pages. This PR extract some logic onto Playwright. This came up earlier today, while discussing with Dima. We already have similar logic on the Playwright server side.

@Skn0tt Skn0tt requested a review from dgozman February 4, 2025 15:11
@Skn0tt Skn0tt self-assigned this Feb 4, 2025
for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit, playwright._bidiChromium, playwright._bidiFirefox])
(browserType as any)._defaultLaunchOptions = options;
for (const browserType of playwright._browserTypes())
browserType._defaultLaunchOptions = options;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move these to playwright right away!

contexts.push(...(browserType as any)._contexts);
const pages = contexts.map(ctx => ctx.pages()).flat();
await Promise.all(pages.map(page => this._screenshotPage(page, false)));
await Promise.all(this._playwright._allPages().map(page => this._screenshotPage(page, false)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment has been minimized.

(browserType as any)._defaultContextNavigationTimeout = navigationTimeout || 0;

for (const browserType of playwright._browserTypes()) {
browserType._defaultContextOptions = _combinedContextOptions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well move these to Playwright.

(playwright.request as any)._defaultContextOptions = { ..._combinedContextOptions };
(playwright.request as any)._defaultContextOptions.tracesDir = tracing().tracesDir();
(playwright.request as any)._defaultContextOptions.timeout = actionTimeout || 0;
playwright.request._defaultContextOptions = { ..._combinedContextOptions };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this one.

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

@dgozman dgozman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there!

@@ -47,7 +47,7 @@ export type FetchOptions = {

type NewContextOptions = Omit<channels.PlaywrightNewRequestOptions, 'extraHTTPHeaders' | 'clientCertificates' | 'storageState' | 'tracesDir'> & {
extraHTTPHeaders?: Headers,
storageState?: string | StorageState,
storageState?: string | SetStorageState,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unrelated, please move it into some IndexedDB pr instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's related! NewContextOptions is for writing, so it needs to use SetStorageState. This should've been fixed earlier, but was always covered because we had all these (... as any)s in the code.

@@ -73,4 +82,16 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel> {
static from(channel: channels.PlaywrightChannel): Playwright {
return (channel as any)._object;
}

_browserTypes(): BrowserType[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at least for the lines below - I made it private for now.

const launchOptions: channels.BrowserTypeLaunchParams = {
...options,
tracesDir: options.tracesDir ?? this._playwright._defaultTracesDir,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better update options variable at line 67 with the new tracesDir instead of changing launchOptions, because options is later used at line 77 and so we want all the values there be correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I moved tracesDir back into _defaultLaunchOptions, things should be good now.

assert(!(options as any).port, 'Cannot specify a port without launching as a server.');
options = { ...this._defaultLaunchOptions, ...this._defaultContextOptions, ...options };
options = { ...this._playwright._defaultLaunchOptions, ...this._playwright._defaultContextOptions, ...options };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing tracesDir here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've moved tracesDir back into _defaultLaunchOptions, so it should work now

@@ -81,13 +83,11 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
options.headless = headless;
if (channel !== undefined)
options.channel = channel;
options.tracesDir = tracing().tracesDir();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that _browserOptions is also used for connect, and we are now missing tracesDir there. Perhaps it was not the best idea to plumb it separately, sorry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tangled tracesDir back into _defaultLaunchOptions :D Hoping that works.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

@dgozman dgozman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with just a single comment.

for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit, playwright._bidiChromium, playwright._bidiFirefox])
(browserType as any)._defaultLaunchOptions = options;
playwright._defaultLaunchOptions = options;
playwright._defaultLaunchOptions.tracesDir = tracing().tracesDir();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this makes options exposed in the next line missing the tracesDir 😄

This comment has been minimized.

@Skn0tt Skn0tt merged commit 893e7bb into microsoft:main Feb 7, 2025
28 of 29 checks passed
Copy link
Contributor

github-actions bot commented Feb 7, 2025

Test results for "tests 1"

1 failed
❌ [webkit-page] › tests/page/page-leaks.spec.ts:82:5 › click should not leak @webkit-ubuntu-22.04-node18

7 flaky ⚠️ [firefox-page] › tests/page/page-evaluate.spec.ts:403:3 › should throw for too deep reference chain @firefox-ubuntu-22.04-node18
⚠️ [chromium-library] › tests/library/video.spec.ts:379:5 › screencast › should capture navigation @ubuntu-20.04-chromium-tip-of-tree
⚠️ [webkit-library] › tests/library/proxy.spec.ts:44:3 › should use proxy for second page @webkit-ubuntu-22.04-node18
⚠️ [webkit-page] › tests/page/locator-misc-2.spec.ts:101:3 › should take screenshot @webkit-ubuntu-22.04-node18
⚠️ [webkit-page] › tests/page/page-leaks.spec.ts:161:5 › waitFor should not leak @webkit-ubuntu-22.04-node18
⚠️ [webkit-page] › tests/page/page-set-input-files.spec.ts:147:3 › should upload large file @webkit-ubuntu-22.04-node18
⚠️ [playwright-test] › tests/ui-mode-test-watch.spec.ts:145:5 › should watch all @windows-latest-node18-1

37814 passed, 655 skipped
✔️✔️✔️

Merge workflow run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants