Skip to content

Can IJSHandle.EvaluateFunctionAsync return null ? #2805

Closed

Description

@CaCTuCaTu4ECKuu

I'm trying to use PuppeteerSharp.Dom and I believe it takes parsing approach when it comes to returning generic values as evaluation results compared to PuppeteerSharp which consider original variable type from JS and throws when requesting something like int when evaluating result is not Number so I was updating repo and got a question

Here at line 71 IJSHandle.EvaluateFunctionAsync is called which is Task<JsonElement?> EvaluateFunctionAsync(string script, params object[] args)
But while I was testing I didn't see result as null a single time. For example getting non-existent smth. returns JsonElement and at that point I'm not sure what then should return null.

Looks like it's a mistake and JsonElement? must be changed to just JsonElement to remove confusion

And while I'm at it - did I get it right about translating/parsing types from evaluation results to generic types? I run some tests to check
await page.EvaluateFunctionAsync<int>("() => '123'") dont work but
await page.EvaluateFunctionAsync<int>("() => Number('123')") works

Activity

  1. kblok commented on Oct 25, 2024

    @kblok
    Member

    I think JsonElement? makes sense when the function returns no value at all.

    Regarding this:

    await page.EvaluateFunctionAsync<int>("() => '123'") dont work but
    

    That's part of switching to System.Text.Json in v19. System.Text.Json is more strict than Newtonsoft.

  2. CaCTuCaTu4ECKuu commented on Oct 25, 2024

    @CaCTuCaTu4ECKuu
    ContributorAuthor

    Yes it does. I'm mentioning it because I didn't catch an eye on anything that can return null while I was debugging.
    But it's a big and complex project that I tbh can't dedicate enough time so sorry for wasting time if I'm wrong.

    Basically JsonValueKind represent response and have Undefined and Null so my guess was that if some code that returns default and if it was missed during transition from JToken and result would never be null. Or it was intentional. Or there is a state when it will return null?

  3. kblok commented on Oct 25, 2024

    @kblok
    Member

    Yes it does. I'm mentioning it because I didn't catch an eye on anything that can return null while I was debugging. But it's a big and complex project that I tbh can't dedicate enough time so sorry for wasting time if I'm wrong.

    Basically JsonValueKind represent response and have Undefined and Null so my guess was that if some code that returns default and if it was missed during transition from JToken and result would never be null. Or it was intentional. Or there is a state when it will return null?

    Do you have any example to look at?

  4. CaCTuCaTu4ECKuu commented on Oct 25, 2024

    @CaCTuCaTu4ECKuu
    ContributorAuthor

    I just rerun tests with conditional breakpoints and I didnt catch null result even once
    So here is method that ensures result parsing and it's called on:

    While its all syntetic there is quite a set of tests and not a single time I catch JsonElement being null

    To check just copy solution, put breakpoints on mentioned lines and run all tests

    I can try to aquire null result myself just to check if I'm wrong, but I need a hint how to do it as I'm not so deep into PuppeteerSharp library and dont know internals

  5. CaCTuCaTu4ECKuu commented on Oct 26, 2024

    @CaCTuCaTu4ECKuu
    ContributorAuthor

    I just catched an exception because result while calling IJSHandle.EvaluateFunctionAsync with script (element, name) => { return element.getAttribute(name) and name argument of data-address on random page, but it wasn't expected behaviour because I accidently click something and wasn't able to reproduce this, and now I'm not even sure if I closed browser windows or just did something with page or navigation that messes with it.

    But I guess main question is resolved - method can return null value.
    However I'm still not sure if it can return null under regular chromium state

    Best I could get is JSHandle that says that it's value is null but
    image

  6. locked and limited conversation to collaborators on Oct 28, 2024
  7. converted this issue into a discussion #2808 on Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions