I have a scenario to automate wherein if I see that the title of the page is “Example title”, then I can mark the test as passed.
Else, if that is not possible, I have to click on something else that is available on the page and then verify if the title is “Example title”.
My code:
I tried using the try catch block, but somehow always get the timeout error. Following is the code snippet
try {
await expect.soft(page).toHaveTitle("Example title",{ timeout: 30000 })
}
catch (error){
await page.getByRole('link', { name: 'here' }).click();
await expect.soft(page).toHaveTitle("Example title",{ timeout: 30000 })
}
What I expected was that script would execute the try block first, and when the timeout error occured it would be caught and the catch block would then be executed.
But the code never reaches the catch block. Instead I get the following error:
Error:
Test timeout of 30000ms exceeded.
Error: expect(locator).toHaveTitle(expected)
Locator: locator(':root')
Expected string: "Example title"
Received string: "Some Count Exceeded"
Call log:
- expect.soft.toHaveTitle with timeout 30000ms
- waiting for locator(':root')
- locator resolved to <html>…</html>
- unexpected value "dfretgrt"
...
19 |
20 | try {
> 21 | await expect.soft(page).toHaveTitle(“Example title”,{ timeout: 30000 })
| ^
22 |
23 | }
24 | catch (error){
Preetham is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.