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

Playwright Multiple user authentication" Screen is launching a blank page. #619

Open
InduKrish opened this issue Jul 10, 2023 · 3 comments

Comments

@InduKrish
Copy link

InduKrish commented Jul 10, 2023

We are using Playwright testing library for our test framework, and it is really helpful, now we are trying to do authentication once then reuse session storage, storage state (cookies) for the rest of the tests instead of manually logging in every time.

here is the link for reference.
https://playwright.dev/docs/auth

I am able to capture session storage and reuse it for logging in and perform only playwright page queries , not able to execute screen queries(eg: queryByTestId())..

i see the test opens a blank page and the other page with the actual URL.
looks like the issue is that the screen fixture opens up a blank page, and it is searching for the locator on the blank page.
if i remove screen fixture from the code below, it works ok, but adding a screen fixture opens up a blank page, and queryByTestId() is looking to search for the element on the blank page, instead of searching the element on the other window that opens the actual URL.

Can you please advise how to get rid of the blank page or how to make screen point to the actual page instead of the blank page? Can you please assist?

const base = require("@playwright/test");
import {locatorFixtures as fixtures} from '@playwright-testing-library/test/fixture';
const it = base.test.extend(fixtures);
const {expect} = it;

import {UseBidAndAwardActor} from '../web/actors/BidAndAwardActors';
import Env from "../web/utils/Env"

export const test = it.extend({

crewIFPlannerActor: async ({browser, context, screen},use) => {
       context = await browser.newContext({ storageState: './storage-state2.json' });
    
        const sessionStorage = process.env.SESSION_STORAGE;
        await context.addInitScript(storage => {
            const entries = JSON.parse(storage);
            Object.keys(entries).forEach(key => {
                window.sessionStorage.setItem(key, entries[key]);
            });
        }, sessionStorage);

      //adding session storage to the context and creating a page using that context, to avoid manual login.
       const page = await context.newPage({ignoreHTTPSErrors: true});

// this initializes screen in the constructor,
        const crewIFPlannerActor = UseBidAndAwardActor(page, page, 'IF Planner Actor', `${Env.IF_PLANNER_USERNAME}`, `${Env.IF_PLANNER_PASSWORD}`,  screen, page);
        crewIFPlannerActor.with('page', page);
        crewIFPlannerActor.with('screen', screen);
        crewIFPlannerActor.with('within', page);

        await use(crewIFPlannerActor);
   
    },
});

export {expect} from '@playwright/test';

Test:

test.only('user test fixture', async ({ crewIFPlannerActor, isMobile }) => {

    const page = crewIFPlannerActor.states('page');
     await crewIFPlannerActor.attemptsTo(Navigate.to(Env.CREW_PLANNER_BASE_URL));
});
``
@BrandonCowart
Copy link

I am having this same issue. Using the Playwright dev Authentication document, I am passing the Playwright page object to my fixture where I set my authenticated context state, I set the context, and when I call new_page() it opens an entirely new blank page with that context, leaving another blank page without.

I have tried closing the first page when I create the second, but my test still sees the first page, and still fails because my test runs against the blank page.

I also tried just not passing the page object to the fixture where I make the new page (just mashing buttons at this point), which did not resolve the issue.

Closing the initial page would be ideal, but if there was some way to just get the test to ignore the first one, I'd take that.

@InduKrish
Copy link
Author

InduKrish commented Jun 7, 2024 via email

@BrandonCowart
Copy link

@InduKrish I'm still figuring this out, but closing the initial page seems to close the actual window (while in --headed mode I can see the actual browser window close). However, the test still seems to execute against a blank window (with url= about:blank).

I believe this is because I am still using Playwright's default Page fixture, because of the way I define my tests

def test_dashboard_initial_state(page: Page, dashboard_home_page: DashboardHomePage) -> None:

I'm honestly not sure how to operate without that, though. If I take out the default page fixture, and just try to use my own DashboardHomePage object, I need to re-define all of the default functionality like .locator() in order to use it.

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

No branches or pull requests

2 participants