Global function declared in background page does not get called via evaluateHandle

Gaurang Tandon

I have a global function in background page like so:

window.myfn = function(){
    return new Promise((resolve, reject) => { stuff; });
};

I am using Jest and Puppeteer. Consider this in my test.js file:

async function getBackgroundPage() {
    const targets = await browser.targets(),
        backgroundPageTarget = targets.find(
            target => target.type() === "background_page",
        ),
        backgroundPage = await backgroundPageTarget.page();

    return backgroundPage;
}

async function sendUpdate() {
    const bgPage = await getBackgroundPage(),
        argString = `() => window.myfn()`;
    await bgPage.evaluateHandle(argString);
    await sleep(30000);
}

getBackgroundPage is literally copied from the docs, so I hope it's correct. However, sendUpdate() does not work as expected. Ideally, window.myfn should be called but actually it is never called. I know this because:

  1. I put several console.logs in it, so if it was called there should have been some log output.
  2. The 30second sleep gives me enough time to goto chrome://extensions, and open the background page console via Inspect Views, and I could not find any log outputs there.

There are no errors in both the background page, and the Jest console. What is the problem then? Why is that global function not being called?

Thomas Dondorf

There is a minor difference between giving a function and giving a string to the evaluateHandle (and evaluate) function.

If you pass a function, the function will be called inside the page. If you pass a string, the string will be executed in the page context. This means, that these two lines do different things:

await bgPage.evaluate(() => console.log("test"););
await bgPage.evaluate('() => console.log("test");');

The first line will execute the function (and run console.log) inside the page. However, the second line will only declare the function and not call it. Therefore, if we pass a string we have to do one of these things:

await bgPage.evaluate('console.log("test")'); // directly run console.log
await bgPage.evaluate('(() => console.log("test"))()'); // execute the function

Fixing your code

Coming back to your code, this means you either have directly call window.myfn() or you pass the argument as a function:

await bgPage.evaluateHandle('window.myfn()'); // directly call the function
await bgPage.evaluateHandle(() => window.myfn()); // or pass as a function and not as string

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Display alert on main page when function is called

分類Dev

Why does inline function need to be declared static if it uses fprintf?

分類Dev

When does onCreateView() get called in this app?

分類Dev

How does dhclient get called under 12.04

分類Dev

How to get the Google Analytics global function name?

分類Dev

can't get the global variable to work on a function

分類Dev

Get element from which onclick function is called

分類Dev

HttpPost function in controller doesn't get called

分類Dev

which value does javascript function takes if the function is called in the given example?

分類Dev

Can't get returning string value of a method called via reflection

分類Dev

setColorFilter does not work after setting background via setBackgroundResource

分類Dev

Can a base class be declared in C++ such that classes derived from it can only be created via a creation function?

分類Dev

In DB2, how can I declare a variable to use in multiple declared global temporary tables within the function..?

分類Dev

Php function appearing not declared

分類Dev

Function pointer "was not declared in this scope"

分類Dev

Javascript: Function does not get fired

分類Dev

How to get the arguments to a php function via grep?

分類Dev

Android - does onPostResume get called after fragment is resumed

分類Dev

Flutter : TextField with FocusNode does not get called when clicked outside of TextField

分類Dev

Global low-level mouse hook doesn't get called when UWP desktop app is out of focus

分類Dev

GET does not send data completely to second page

分類Dev

jQuery does not work when reload the page content via ajax

分類Dev

Global page in smartface

分類Dev

When does a function get added to the call stack?

分類Dev

Exporting a function declared inside asynchronous function

分類Dev

How to pass variable to a function declared in another function

分類Dev

body onload does not see js function on the same page

分類Dev

Yii index page does not print values inside a function

分類Dev

Is it possible to get the background color of an element that is being set via an external css file?

Related 関連記事

  1. 1

    Display alert on main page when function is called

  2. 2

    Why does inline function need to be declared static if it uses fprintf?

  3. 3

    When does onCreateView() get called in this app?

  4. 4

    How does dhclient get called under 12.04

  5. 5

    How to get the Google Analytics global function name?

  6. 6

    can't get the global variable to work on a function

  7. 7

    Get element from which onclick function is called

  8. 8

    HttpPost function in controller doesn't get called

  9. 9

    which value does javascript function takes if the function is called in the given example?

  10. 10

    Can't get returning string value of a method called via reflection

  11. 11

    setColorFilter does not work after setting background via setBackgroundResource

  12. 12

    Can a base class be declared in C++ such that classes derived from it can only be created via a creation function?

  13. 13

    In DB2, how can I declare a variable to use in multiple declared global temporary tables within the function..?

  14. 14

    Php function appearing not declared

  15. 15

    Function pointer "was not declared in this scope"

  16. 16

    Javascript: Function does not get fired

  17. 17

    How to get the arguments to a php function via grep?

  18. 18

    Android - does onPostResume get called after fragment is resumed

  19. 19

    Flutter : TextField with FocusNode does not get called when clicked outside of TextField

  20. 20

    Global low-level mouse hook doesn't get called when UWP desktop app is out of focus

  21. 21

    GET does not send data completely to second page

  22. 22

    jQuery does not work when reload the page content via ajax

  23. 23

    Global page in smartface

  24. 24

    When does a function get added to the call stack?

  25. 25

    Exporting a function declared inside asynchronous function

  26. 26

    How to pass variable to a function declared in another function

  27. 27

    body onload does not see js function on the same page

  28. 28

    Yii index page does not print values inside a function

  29. 29

    Is it possible to get the background color of an element that is being set via an external css file?

ホットタグ

アーカイブ