microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.42k stars 3.63k forks source link

[BUG] Not able to capture JS error on Playwright Page #16648

Closed ghost closed 2 years ago

ghost commented 2 years ago

Context:

Code Snippet

I am trying to capture JS error on Playwright Page using window.onerror event, but window.onerror event is not firing during these JS errors.

Does window.onerror event fires automatically if JS errors occur on a playwright page For example: Pseudo code

window.onerror = function () {
    console.log("JS errors captured");
}

str=""; //str is a Javascript with error
try{
  PlaywrightPage.EvaluateAsync(str);
}
catch
{
  Console.writeLine("Exception occurred");
}

Expected Outcome: JS errors captured Exception Occurred

Actual Outcome: Exception Occurred

Follow up Question What are other ways to capture JS errors if we open a page on browser launched using Playwright? (Other than window.onerror() )

Meir017 commented 2 years ago

Try this - https://playwright.dev/dotnet/docs/api/class-page#page-event-page-error

ghost commented 2 years ago

Hi,

Page event error is not working as well. Because exception is caught in catch block itself. Pseudocode:

try{
  PlaywrightPage.EvaluateAsync(str);
}
catch
{
  Console.writeLine("Exception occurred");
}

this.PlaywrightPage.PageError += (_, exception) =>
{
  Console.WriteLine("Uncaught exception at PageError event: " + exception);
};

Output: Exception occured

Can you please confirm that if window.onerror event fires whenever exception occurs on a page launched using playwright? Like this- https://www.tutorialspoint.com/What-is-onerror-Method-in-JavaScript