Rayrun
← Back to Discord Forum

Session issue with login over APU

Hey guys,

I am coming from Cypress after working in it for 5 years and I though that on new project I would like to start using something new like playwright. Also I have read that it can handle print, which is happening always after every transaction.

But what I struggle with now the most is that I have to login in to the app over API. This login has to happen everytime a new test is is executed. This is because we have internal BE API for clearing the DB and setting data (in our case profiles) on the BE side.

So I already did this in Cypress and it is working actually flawlessly but in playwright it seems I am missing something. I will present you code snippets now:

This is the test:

// deposit.sepc.ts
import { test } from '@playwright/test';
import { TransactionPage } from '../../pom/transactions/transaction';
import { login } from '../../lib/common';

test.beforeEach(async ({ page }) => {
    const cookies = {
        name: 'host',
        value: 'localhost:8085',
        domain: 'localhost',
        path: '/',
    };

    const browserContext = await page.context();
    await browserContext.addCookies([cookies]);

    const log = await browserContext.cookies();
    console.log(log);

    await login('admin@casino.local', 'admin');
});
test.describe('Deposit tests', () => {
    test('Deposit ', async ({ page }) => {
        const transaction = new TransactionPage(page);

        await transaction.goto(1, 'deposit', 10000000);
        await transaction.fillAmount(100);
        await transaction.fillNotes('test');
        await transaction.clickSubmit();
    });
});

This is the login function:

import {Page, request, expect} from '@playwright/test';

const BE_URL = 'http://localhost:8085/api/';

export async function login(email: string, password: string) {
    const context = await request.newContext({
        baseURL: BE_URL,
    });

    const login = await context.post(`session/login`, {
        data: {
            'email': email,
            'password': password,
            'readOnly': false,
            'loginMethod': 'EMAIL+PASSWORD',
            'rememberMe': false,
        }
    });
    expect(login.ok());
}

As you can see I have to setup the cookies for host: localhost:8085 that is because of the BE is running locally on different port then the FE.

But I do manage to login via API (I get 201), but then when getting redirected to the deposit transaction. The session is lost. And I get session 401.

In cypress this is handled automatically but in playwright, I am missing something.

Any feedback will be appreciated. Thanks

This thread is trying to answer question "Why does the session get lost when redirected to the deposit transaction after logging in via API using Playwright?"

4 replies

Try passing page to your login func, drop request import and use await page.request.fetch(...) instead of creating request context.

@skorp32: Will try, thanks!

Will try, thanks!

It indeed worked thanks!

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.