You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How can I reuse locator variables in different tests?
Something like:
// @ts-checkconst{ test, expect }=require('@playwright/test');test.beforeEach(async({ page })=>{awaitpage.goto('[desired URL]');});// I want to make this variable global to be able to use it within all the tests.constsignInBtn=page.getByTestId('some-button');// how to resolve 'page' here??test.describe('My set of tests',()=>{test('My test 1',async({ page })=>{awaitexpect(page).toHaveTitle(/Some-Title/);awaitexpect(signInBtn).toBeEnabled();// I wanna use the variable here...});test('My test 2',async({ page })=>{awaitsignInBtn.click();// ...and here, without having to define everytime inside each test.});});
Activity
Nav-2d commentedon Jan 10, 2023
You can define the variable as
let signInBtnat the top level and then assign and reuse it as needed.gauravgandhi1315 commentedon Jan 10, 2023
You can define all your locators in one separate file. From there, you can export and then use it in all your test scripts.
aslushnikov commentedon Jan 10, 2023
Gustavo Andrade (@1UpGus) there are ultimately 2 ways to do this.
helpers, and call it in every test:This POM approach also was described in one of our release videos:
https://www.youtube.com/watch?v=LczBDR0gOhk&list=PLQ6Buerc008fTzmUIPkPo3Eis0vjx7ykn&index=15&t=322s
Hope this helps! Please do not hesitate to file new issues if you think we can be helpful.