There is an element that loads up after a few seconds the form is submitted(page doesn't reload after submission). I want to assert that this element has some text value in it. Value doesn't matter but there should be some text in the element.
Currently I am asserting it like this -
await expect(await targetPage.locatorToSomeElement.innerText()).toBeTruthy();
OR this -
await expect(targetPage.locatorToSomeElement).toBeTruthy();
targetPageis class instance of Page Object Model class TargetPage and
locatorToSomeElement is locator with value page.getByTestId("some-span-element");
Please let me know the best way to achieve this. Please check below points as well.
The first statement seems like a manual assertion? await is inside expect? It's a bad practice? https://playwright.dev/docs/best-practices#use-web-first-assertions
toBeTruthy() is an assertion that doesn't not auto wait so I believe second statement will always be true?
Instead of using toHaveText with complex regex , you can simply use toContainText in places where exact string is unknown.
Note:
Unlike toHaveText toContainText, when specified with the partial text, it will assert and confirm the element with its presence. You don't need to provide the exact text like toHaveText.
Ultimately both gives you an option to use regex to use them with flexibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With