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

[Question]: How to get the right BrowserType from a device name? #1496

Closed
Rafiot opened this issue Aug 15, 2022 · 8 comments · Fixed by #1497
Closed

[Question]: How to get the right BrowserType from a device name? #1496

Rafiot opened this issue Aug 15, 2022 · 8 comments · Fixed by #1497
Assignees

Comments

@Rafiot
Copy link

Rafiot commented Aug 15, 2022

Your question

I noticed that the CLI is able to figure out the right BrowserType to use when it is launched from the command line:

playwright open --device="Desktop Safari" wikipedia.org  # Webkit
playwright open --device="Desktop Firefox" wikipedia.org  # Firefox
playwright open --device="Desktop Chrome" wikipedia.org  # Chrome

But the documentation seems to say that I have to initialize a BrowserType before I can pass the settings to the context, which partially defeats the purpose of the device settings.

I can implement my own logic do initialize the right BrowserType for each device, but as playwright open can already do that, that seems superfluous.

@Rafiot Rafiot changed the title [Question]: How to get the right BrowserType from a device? [Question]: How to get the right BrowserType from a device name? Aug 15, 2022
@rwoll
Copy link
Member

rwoll commented Aug 15, 2022

You can get it from the devices object: https://playwright.dev/python/docs/emulation#devices. It's a property in the dict returned when you do playwright.devices["Desktop Safari"]

@rwoll rwoll added the triaging label Aug 15, 2022
@Rafiot
Copy link
Author

Rafiot commented Aug 15, 2022

I can get the settings, but I still need to initialize a browser (browser = await playwright.webkit.launch(headless=False)), and then initialize the context (context = await browser.new_context(**pixel_2)).

In my case, I want to start from the device name (say 'Pixel 2') and get the right BrowserType. And afaik, I can't figure that out without parsing the user agent, or manually mapping 'Pixel 2' to webkit in my code.

@rwoll
Copy link
Member

rwoll commented Aug 15, 2022

Gotcha—looks like we are missing the defaultBrowserType property in the devices entries that are available in Node and could be used to programmatically pick playwright.{webkit,chromium,firefox}.

@rwoll rwoll self-assigned this Aug 15, 2022
@rwoll rwoll added v1.26 and removed triaging labels Aug 15, 2022
@Rafiot
Copy link
Author

Rafiot commented Aug 15, 2022

\o/ nice, thank you!

@rwoll
Copy link
Member

rwoll commented Aug 15, 2022

For now, you can get the defaultBrowserType from https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json, but it should be exposed on playwright.devices['Pixel 2'] in the future.

@Rafiot
Copy link
Author

Rafiot commented Aug 15, 2022

Excellent, I'll use the dump for now and switch to that when available.

@rwoll
Copy link
Member

rwoll commented Aug 15, 2022

@Rafiot Just shipped in v1.25.0 🎉 ! You can use it like the test does: https://github.com/microsoft/playwright-python/pull/1497/files

@Rafiot
Copy link
Author

Rafiot commented Aug 16, 2022

Nice, thanks! And thinks for the hint, I didn't know I could get a browser like that: playwright[device_type]

mxschmitt pushed a commit to mxschmitt/playwright-python that referenced this issue Aug 16, 2022
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 a pull request may close this issue.

2 participants