I'm trying to implement conditional code in playwright inside of page object. It depends on if element is found on page or not. The problem is - returned value is always the same, no matter if element is present or not. Here is my code:
this.pagePlaceholder = page.locator('.s-wrapper.s-wrapper_placeholder');
async clearUpPage() {
const placeholdersNumber = await this.pagePlaceholder.count();
console.log(placeholdersNumber);
if (placeholdersNumber < 1) {
await this.mainSection.hover({
position: {
x: 200,
y: 150,
},
});
await this.page.click('.section-options__btn._del:visible');
await expect(this.pagePlaceholder).toBeVisible();
}
}
So the problem is - count() always returns 0, even if element of that class is present on that page. I also tried solutions with "if (this.pagePlaceholder)" and "if (this.pagePlaceholder.isVisible())" and those also return same value no matter what. If I use 'waitForSelector" inside "if" - it throws timeout error if selector is not found, so test is failed instead of case being skipped.