0

I have a file structure that looks like this

Folder Structure

I have a file called "login.js" that will contain a function that logs into the page. Currently it looks like this


// login.js
const { chromium } = require('playwright');


async function Login() {
  const browser = await chromium.launch({
    headless: false,
    args: ['--no-sandbox', '--disable-setuid-sandbox'],
  });
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('http://test.local/');
  return true;
}

/*
This is just a example of logging in and not complet
*/

I want to export it so all my other tests can continue AFTER this one function logs in. Once it successfully logs in, tests such as 'example.spec.js' can get all the cookies/headers from the login script and continue

How can I do that?

1 Answers1

0

You should be doing this.

// login.js
const { chromium } = require('playwright');


module.exports = async function login() {
  const browser = await chromium.launch({
    headless: false,
    args: ['--no-sandbox', '--disable-setuid-sandbox'],
  });
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('http://test.local/');
  return true;
}

Then you can access it in another file like this.

const login = require('./test.js'); // path has to be altered based on your folder structure
login();
Raju
  • 2,299
  • 2
  • 16
  • 19