Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Playwright JavaScript - How can I reuse locator variables within different tests?

$
0
0

I want to be able to use a locator variable within all the tests without having to define it every time inside each test.Something like:

// @ts-checkconst { test, expect } = require('@playwright/test');test.beforeEach( async ({ page }) => {  await page.goto('[desired URL]');  });// I want to make this variable global to be able to use it within all the tests.const signInBtn = page.getByTestId('some-button'); // how to resolve 'page' here??test.describe('My set of tests', () => {  test('My test 1', async ({ page }) => {    await expect(page).toHaveTitle(/Some-Title/);    await expect(signInBtn).toBeEnabled();     // I wanna use the variable here...  });  test('My test 2', async ({ page }) => {    await signInBtn.click();   // ...and here, without having to define it every time inside each test.  });});

PS: This snippet is just an example to pass the idea, not the actual project, pls don't be attached to it.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>