3

I want to use testcafe to start browsers available through internal end external browser providers. I don't want any testing functionality. But I want to be able to use takeScreenshot, resizeWindow, etc.

Here is my code.

const pool = require("./node_modules/testcafe/lib/browser/provider/pool.js");

pool.getProvider("chrome").then((provider) => {
    const plugin = provider.plugin;

    console.log("open")

    plugin.openBrowser("foo", "http://google.de", "chrome").then(() => {
        console.log("done");

    }).catch(console.warn);;

    console.log("early");

}).catch(console.warn);

If I run or debug the file with this code, the open browser promise is never resolved. The console prints open and early and then the program exits.

If I copy and paste the code into an interactive node shell, the browser gets started. (I get an exception that connection is null but this code is never run if I start the file.

What am I doing wrong?

UPDATE: This is the code that uses async/await, but does not work either:

const pool = require("../node_modules/testcafe/lib/browser/provider/pool.js");

(async () => {
    const provider = await pool.getProvider("chrome");
    const plugin = provider.plugin;
    await plugin.openBrowser("foo", "http://google.de", "chrome");
    console.log("done");
})();
htho
  • 1,549
  • 1
  • 12
  • 33

1 Answers1

3

We advise against using this internal API as it can be changed at any time without notification. You can use the testcafe-browser-tools module instead.  

The plugin.openBrowser method is async. So, you need to await the result of the method execution. Please see TestCafe's runBrowser code for more details.

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
mlosev
  • 5,130
  • 1
  • 19
  • 31
  • Am I able to start any browser available through third party providers through testcafe-browser-tools? That is the requirement. – htho Apr 23 '19 at 14:53
  • You wrote: "The plugin.openBrowser method is async." Yes. I just use the Promise returned by the async function instead. I figured that might be easier to understand than an immediatly executed async function. (I tried that first) – htho Apr 23 '19 at 14:57