I’m facing with the following issue.
I’m trying to execute multiple tests using Playwright Java and Cucumber, but only one scenario executes successfully, others fails as couldn’t find elements on Page.
Below the code of 2 classes: CucumberHook and LoginPOM. What am I doing wrong?
public class CucumberHook {
public static ThreadLocal<Playwright> playwright = new ThreadLocal<>();
public static ThreadLocal<Browser> browser = new ThreadLocal<>();
public static ThreadLocal<BrowserContext> browserContext = new ThreadLocal<>();
public static ThreadLocal<Page> page = new ThreadLocal<>();
@BeforeAll
public static void beforeAll() {
playwright.set(Playwright.create());
browser.set(playwright.get().chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(false).setChannel("chrome")));
}
@Before
public static void beforeScenario(Scenario scenario){
browserContext.set(browser.get().newContext());
page.set(browserContext.get().newPage());
page.get().navigate("https://www.saucedemo.com/");
}
public class LoginPOM {
private LoginPOM(){
}
private static LoginPOM INSTANCE;
public static LoginPOM getInstance() {
if (INSTANCE == null) {
INSTANCE = new LoginPOM();
}
return INSTANCE;
}
Page page = CucumberHook.page.get();
{
elementsMap.put("userNameInputField", page.locator("#user-name"));
elementsMap.put("passwordInputField", page.locator("#password"));
elementsMap.put("loginBtn", page.locator("#login-button"));
}
private Locator appLogo = page.locator(".app_logo");
private Locator errorMsg = page.locator("//*[@data-test='error']");
}
New contributor
Orxan Qəhrəmanlı is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.