Search code examples
c#playwrightplaywright-dotnet

Can Playwright show a message in the browser to a user?


When using Selenium, a message like this would show up in the browser:

Selenium Browser Message

Chrome is being controlled by automated test software.

Using Playwright, I'd like to show a similar message, except I'd like to provide the text/HTML.

Is that possible? If so, how?

(I'm using C#)

I've googled for different APIs but cant seem to find what I'm looking for.


Solution

  • While it is not possible as mentioned by hardkoded; there may be a walk around. Here is my suggestion, but it is typescript - I believe you can modify it to C#.

    test.describe('Javascript Evaulate Method', () => {
        test('I am lucky!', async ({ page }) => {
        await page.goto('https://google.co.uk');
    
        // walk around to show the message
        await page.evaluate(() => {
            const bar = document.createElement('div');
            bar.textContent = 'Chrome is being controlled by automated test software.';
            bar.style.cssText = 'position: fixed; top: 0; width: 100%; background: lightgrey; color: black; text-align: left; padding: 8px; font-size: 14px; border-bottom: 1px solid grey; z-index: 9999;';
            document.body.style.paddingTop = '30px';
            document.documentElement.prepend(bar);
        });
    
        // dummy test
        const luckyButton = await page.locator('input[name="btnI"]');
        await expect(luckyButton).toBeVisible();
    
        });
    });
    

    Basically you would use page.evaluate() method to trigger a javascript code which generate the message bar. You can modify the text and its style. In this demonstration, I used that method in a test block; but you may want to call it from beforeEach etc.