There is a specific scenario I need to automate where if the title of a page is "Sample title", then I must mark the test as successful. Otherwise, if that condition is not met, I have to interact with another element on the page and verify if the title changes to "Sample title".
My Approach: I attempted to handle this using a try-catch block, but unfortunately, I kept encountering timeout errors. Here's a snippet of the code I used:
try {
await expect.soft(page).toHaveTitle("Sample title",{ timeout: 30000 })
}
catch (error){
await page.getByRole('link', { name: 'here' }).click();
await expect.soft(page).toHaveTitle("Sample title",{ timeout: 30000 })
}
I anticipated that the script would first attempt the code in the try block, and upon encountering a timeout error, it would proceed to the catch block. However, the execution never goes to the catch block, resulting in the following error message:
Error:
Test timeout of 30000ms exceeded.
Error: expect(locator).toHaveTitle(expected)
Locator: locator(':root')
Expected string: "Sample title"
Received string: "Different Title"
Call log:
- expect.soft.toHaveTitle with timeout 30000ms
- waiting for locator(':root')
- resolved locator to <html>…</html>
- unexpected value "dfretgrt"
...
> 19 |
20 | try {
21 | await expect.soft(page).toHaveTitle("Sample title",{ timeout: 30000 })
| ^
22 |
23 | }
24 | catch (error){