microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
65.49k stars 3.57k forks source link

[Feature] ability to get sessionId when using selenium #27385

Closed DudaGod closed 11 months ago

DudaGod commented 11 months ago

Hello.

I use playwright with browsers from remote cloud (based on selenoid). These browsers save log to the file with session id (available by url to cloud with passed session id). I want to display session id in html report attachments. The problem is that I don't have access to this value, which is inited here - https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/chromium/chromium.ts#L209C46-L210.

I connect to my browsers using fixture and I want to use it to get sessionId. So I want users just connect this fixture and don't think about anything else.

I see the following solutions:

const test = base.extend({ playwright: [async ({playwright}, use) => { const playwrightImpl = playwright._toImpl(playwright);

    playwrightImpl.chromium._launchWithSeleniumHub = () => {
        console.log('my implementation');
    };

    await use(playwright);
}, {scope: 'worker'}],

})

test('test page', async ({page}) => { await page.goto('about:blank'); });


I don't like it either because I don't want to get into private methods.

What solutions can you offer?
pavelfeldman commented 11 months ago

What we should do is move the _launchWithSeleniumHub out into the userland or into the fixture layer. That way you would have the control over the wiring of Playwright and Selenium together.

DudaGod commented 11 months ago

What we should do is move the _launchWithSeleniumHub out into the userland or into the fixture layer. That way you would have the control over the wiring of Playwright and Selenium together.

That sounds good. Of course, it would be more convenient not to write the same logic from _launchWithSeleniumHub by yourself, but I remember that you don't really want to support remote launch because Selenium can remove support CDP at any time.

Having access to override public _launchWithSeleniumHub or use fixture will I be able to implement a connection via w3c protocol to any desired browser? For example we have android emulators on linux containers in which we test chrome and native applications. Or iOS simulators on mac mini in which we test safari.

They don't support CDP and it means that I should implement commands using w3c.

pavelfeldman commented 11 months ago

That sounds good. Of course, it would be more convenient not to write the same logic from _launchWithSeleniumHub by yourself, but I remember that you don't really want to support remote launch because Selenium can remove support CDP at any time.

Yes, as discussed, you should move this code into your own fixture.

They don't support CDP and it means that I should implement commands using w3c.

If you want to run Playwright against non-CDP, you need to implement pretty much entirely new browser flavor. This is not something we would take on or recommend doing to you.

DudaGod commented 11 months ago

Why you close it? Based on you propolsal, I concluded that it is necessary to make the _launchWithSeleniumHub method public and available from browser for example. Like this:

import { test as base } from '@playwright/test';

const test = base.extend({
    browser: [async ({browser}, use) => {
        browser.launchWithSeleniumHub = () => {
            console.log('my implementation');
        };

        await use(browser);
    }, {scope: 'worker'}],
})

Yes, as discussed, you should move this code into your own fixture.

I can do it, but I don't want to use private methods which can be renamed/removed/etc at any time.