Hey there, I'm currently working on a basic test using POM.
Here is a snippet from one of my PageObjects Class:
import { Expect, Page, Locator } from "@playwright/test";
export class InventoryPage {
readonly page: Page;
readonly addToCartBtn: Locator;
readonly addToCartBtnAll: Promise<Array<Locator>>;
readonly cartIcon: Locator;
constructor(page: Page) {
this.page = page;
this.addToCartBtn = page.locator("button.btn_inventory");
this.addToCartBtnAll = page.locator("button.btn_inventory").all();
this.cartIcon = page.locator(".shopping_cart_badge");
}
async addAllItemsToCart() {
for (let item of await this.addToCartBtnAll) await item.click();
}
async addRandomItemToCart() {
await this.addToCartBtn
.nth(Math.floor(Math.random() * (await this.addToCartBtnAll).length))
.click();
}
}
The issue arises with the following lines:
readonly addToCartBtnAll: Promise<Array<Locator>>;
this.addToCartBtnAll = page.locator("button.btn_inventory").all();
Whenever I create an instance of this class within a test in the beforeEach hook, the test fails prior to execution...
Error: locator.all: Execution context was destroyed, most likely because of a navigation
at InventoryPage
Does anyone have any insights into what might be causing this issue with the locator().all()
function in the page object?
I've managed to find a workaround for now by avoiding the
Promise<Array<Locator>>
type and using .all()
elsewhere in the code, but I am still curious as to why Playwright seems to have trouble with this specific type in constructors...