drashland / sinco

Browser Automation and Testing Tool for Deno, written in full TypeScript
https://drash.land/sinco
MIT License
57 stars 3 forks source link

Investigate: passing a str to evaluate that should return an obj, returns nothing #81

Closed ebebbington closed 3 years ago

ebebbington commented 3 years ago

Summary

What:

As it stands, you can pass a command as a string to evaluatePage where that command returns an object, eg:

await sinco.evaluatePage(`document.querySelector('a').getBoundingClientRect()`) // the command in the dom returns an obj

But the value property doesnt exist on the response. I think i've seen this before, if a command that returns an object is passed to Runtime.evaluate wont return anything

What I propose is we investigate using Runtime.callFunctionOn for both strings AND functions, but this needs ot be tested to see if we cna do that. For example im thinking:

evaluate(command: str | Function) {
  // Logic for handling if a string will be removed

  if (command is a string) command = () => command
  // Then existing logic
  const { executionContextId } = await this.sendWebSocketMessage(
        "Page.createIsolatedWorld",
        {
          frameId: this.frame_id,
        },
      );

      const { result } = await this.sendWebSocketMessage(
        "Runtime.callFunctionOn",
        {
          functionDeclaration: pageCommand.toString(),
          executionContextId: executionContextId,
          returnByValue: true,
          awaitPromise: true,
          userGesture: true,
        },
      );
      return result.value;
}
evaluate(doc.querySelector(selector))

Why:

I think it's just a limitation of that runtime method

Acceptance Criteria

Below is a list of tasks that must be completed before this issue can be closed.

ebebbington commented 3 years ago

I've investigated, and found that there isn't anything we can do. Thats just the nature of the API, it won't return class based objects, BUT it would return a pure object, eg something.toJson() or { name: 2 }.

Tested with puppeteer, and i experienced the same thing

Closing because there's nothing that we can do