Playwright - SSH Cert login for website through playwright

Viewed 128

I have a requirement where I need to use SSL Certification for each login to the website. I do create cert and key pem files. Not sure how to add it to the code for login.

The feature is open and yet to be worked on in github by the playwright team https://github.com/microsoft/playwright/issues/1799. There is a workaround mentioned where I need to add the certificate dynamically or add it as a fixture and call it to the class, both did not work I got request module not found error. Can someone help me out with that?


test('login test @certTest', async ({browser}) => {
    const fs = require('fs');
    const request = require('request');
    //const browser = await chromium.launch();
    const ctx = await browser.newContext();
    await ctx.route('**/*', (route, req) => {
        const options = {
            uri: req.url(),
            method: req.method(),
            headers: req.headers(),
            body: req.postDataBuffer(),
            timeout: 10000,
            followRedirect: false,
            agentOptions: {
                ca: fs.readFileSync('./certs/user_cert.pem')
               // pfx: fs.readFileSync('./certs/user_cert.p12'),
              // passphrase: fs.readFileSync('./certs/user_cert.p12.pwd'),
            },
        };
        let firstTry = true;
        const handler = function handler(err, resp, data) {
            if (err) {
                /* Strange random connection error on first request, do one re-try */
                if (firstTry) {
                    firstTry = false;
                    return request(options, handler);
                }
                console.error(`Unable to call ${options.uri}`, err.code, err);
                return route.abort();
            } else {
                return route.fulfill({
                    status: resp.statusCode,
                    headers: resp.headers,
                    body: data,
                });
            }
        };
        return request(options, handler);
    });
    const page = await ctx.newPage();

      await page.goto('https://my.page.net')

    const title = page.locator('title')

    await expect(title).toHaveText('My Title')
})```
0 Answers
Related