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

[Question]When I log in with global-setup, shouldn't it be auto-login? #19504

Closed
YusuffOzt opened this issue Dec 16, 2022 · 1 comment
Closed

Comments

@YusuffOzt
Copy link

YusuffOzt commented Dec 16, 2022

I m handling spec files with my core.ts file . My spec file is channels.e2e.spec. When these spec file called from npm script, it navigates to my channels.e2e.ts

I am doing the login process in the global-setup.ts file. When the npm script runs, the login process is works good in global-setup.ts file. Then the browser closes.

My question is exactly this: I want to navigate to the first test in my channels.e2e.ts file, but a blank browser page opens after global-setup.ts file worked good. So it doesn't click the button which is my first action in NavigateChannelsPage test. As far as I understand; the login process in the first global-setup.ts file works, the browser closes, then the new browser opens, but it cannot find which url to redirect to. Normally, when I log in with global-setup, shouldn't it be auto-login? By the way,After all operations in global-setup.ts are running, data is written to storageState.json as follows. But when I look the storageStage.json, cookie is empty as below.Should it be filled?

{
  "cookies": [],
  "origins": []
}

my npm script

"pw:staging": "NAME=test@domain.com PASS=test npx playwright test channels.spec.ts"

this is my global-setup.ts

import LoginPageRepository from "./playwright/pages/login"
import { chromium, FullConfig } from '@playwright/test';

async function globalSetup(config: FullConfig) {
    const { baseURL, storageState, headless } = config.projects[0].use;
    const browser = await chromium.launch({ headless });
    const page = await browser.newPage();
        await page.goto(`${baseURL!}/login`)
        await page.locator(LoginPageRepository.emailTextField).type(process.env.NAME!)
        await page.locator(LoginPageRepository.passwordTextField).type(process.env.PASS!)
        await page.locator(LoginPageRepository.loginButton).click()
		await page.context().storageState({ path: storageState as string });
		await browser.close();
	
	}


export default globalSetup;

this is my playwright.config.ts

import { type PlaywrightTestConfig, devices } from "@playwright/test";
const config: PlaywrightTestConfig = {
  globalSetup: require.resolve('./global-setup'),
  reporter: [ ['html', { outputFolder: 'report' }] ],
  use: {
    headless: false,
    baseURL: 'https://test/beta',
    storageState: 'storageState.json',
    ignoreHTTPSErrors: true
  },
  projects: [
    {
      name: "chromium",
      use: {
        ...devices["Desktop Chrome"],
        launchOptions: {
          args: ["--disable-web-security"],
        },
      },
    }
  ],
};
export default config;

this is my channels.spec.ts file;

import Core from "../../../../core";
import { Identity } from "./identity.api";

Core.create("Identity", Identity);

This is my channels.e2e.ts file ;

import { expect } from "@playwright/test"
import ChannelsPageRepository from "../../pages/channels"
import { Login } from "../login/login.e2e"
import { faker } from '@faker-js/faker';
export const Channels = {
    States: {
        fakeName:{} as string
    },
    API: {},
    UI: {
        NavigateChannelsPage: {
            id:1,
            notes:"navigates channels page",
            tag:"",
            run: async ({ page }) => {
                await page.locator(ChannelsPageRepository.channelsButton).click()
                await expect(page).toHaveURL(/\/channels/); 
               
            }
        },
        ViewEmailChannels: {
            id:2,
            notes:"click view my email channels button and navigate mail channels page",
            tag:"",
            run: async ({ page }) => {
                await Channels.UI.NavigateChannelsPage.run({page})
                await page.locator(ChannelsPageRepository.viewMyEmailChannelButton).click()
                await expect(page).toHaveURL(/\/a_channels/)
            }
        },
    }
}

this my core.ts file (I am managing all spec files here using Core.create method)

import { test } from "@playwright/test";

const Core = {
  create(name: string, config: any) {
    return (() => {
      if (config.API) {
        test.describe(`${name}`, async () => {
        config.beforeAll && test.beforeAll(config.beforeAll);
        config.afterAll && test.afterAll(config.afterAll);
        config.beforeEach && test.beforeEach(config.beforeEach);
        config.afterEach && test.afterEach(config.afterEach);
          await Promise.all(
            Object.keys(config.API).map((t) => {
              let testContent = config.API[t];
              let desc = `API- ${t} - #${testContent.id}# - ${testContent.notes}`;
              let skip = testContent.skip || config.API.skip;
              let only = testContent.only;
              if (skip) {
                test.skip(desc, async () => {
                  return testContent.run();
                });
              }
              if (only) {
                test.only(desc, async () => {
                  return testContent.run();
                });
              }
              if (!skip && !only) {
                test(desc, async () => {
                  return testContent.run();
                });
              }
            })
          );
        });
      }
      if (config.UI) {
        test.describe(`${name}`, async () => {
          config.beforeAll && test.beforeAll(config.beforeAll);
          config.afterAll && test.afterAll(config.afterAll);
          config.beforeEach && test.beforeEach(config.beforeEach);
          config.afterEach && test.afterEach(config.afterEach);
          await Promise.all(
            Object.keys(config.UI).map((c) => {
              let testContent = config.UI[c];
              let desc = `UI- ${c} - #${testContent.id}# - ${testContent.notes}`;
              let skip = testContent.skip || config.UI.skip;
              let only = testContent.only;
              if (skip) {
                test.skip(desc, async () => {
                  return testContent.run();
                });
              }
              if (only) {
                test.only(desc, async () => {
                  return testContent.run();
                });
              }
              if (!skip && !only) {
                test(desc, async ({ page }) => {
                  return testContent.run({ page });
                });
              }
            })
          );
        });
      }
    })();
  },
};

export default Core;

@YusuffOzt
Copy link
Author

solved problem added timeout into global-setup;
await page.waitForTimeout(10000)
await page.context().storageState({ path: storageState as string });
await browser.close();

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

1 participant