1

async function launchBrowser() {
    console.log("Launching Browser")
    try {
        let browser = await chromium.launch({
            args: [
                "--disable-web-security",
                "--use-fake-ui-for-media-stream",
                "--use-fake-device-for-media-stream",
                "--allow-file-access",
                "--disable-gpu",
                "--incognito",
                "--mute-audio",
                "--disable-dev-shm-usage"
            ],
            headless: localIsHeadless,
            slowMo: 100,
            timeout: 60000
        });

        return browser;
    } catch (err) {
        return err;
    }

}

async function newPage(browser, url) {
    console.log("New Page")
    const page = await browser.newPage();

    console.log("Setting Viewport")
    await page.setViewportSize({
        width: 1440,
        height: 900,
    });

    console.log("Granting Permissions")
    await page.context().grantPermissions(['camera', 'microphone']);

    console.log("Go to url : " + url)
    await page.goto(url);

    return page;
}

Since browser.launch() was inside a try catch block, it was throwing error as I was running in headful mode.

Removed the launchBrowser code outside try catch and it return an error that I was running browser in headful mode. Switch it to headless and started working.

A Hassan
  • 119
  • 10
  • It's rather unusual to `return err`. If the launch fails, let the function throw so the caller can handle this event with a `catch`. Probably, something is throwing in the container, but instead of catching and logging it so you can see what the problem is, the caller is treating `err` as if the function returned a `browser` instance, hence the titular error. Also, please show the calling code--it's not clear how you're using these functions. In general, don't write premature abstractions--use mainline Puppeteer code until you have a basic POC working on the container, then add functions. – ggorlen Aug 08 '23 at 14:16
  • I have a main file in the container, say 'xyz.js' which uses one commonHelper.js file where I have functions to be called from xyz.js. Now funny thing is, if I run this code outside the docker container, it works like charm but as soon as I create an image and run it inside the container, it starts to throuw this browser.newPage() is not a function – A Hassan Aug 08 '23 at 19:02
  • 1
    That much is already clear. Please re-read my comment. I'm asking how exactly you're calling it (please [edit] the post to show the exact code). Is the calling code checking the type of the return value to make sure it's not an error, and printing the error message if it is? If not, then you're suppressing the error message and doing an illegal operation that's bound to fail, i.e. `errorObject.newPage()`. In other words, your code is misleading you into thinking `.newPage()` is the problem when the problem is likely something else, but you can't tell because you've suppressed the actual error. – ggorlen Aug 08 '23 at 19:04
  • This is the exact same code from my commonHelper file which I'm calling from my xyz file as follows `browser = await commonHelper.launchBrowser();` `page = await commonHelper.newPage(browser, url);` – A Hassan Aug 08 '23 at 19:17
  • 1
    Thanks, well, there's the likely problem: the browser fails to launch on Docker for some reason, but instead of logging it, `launchBrowser` returns the error instead of the browser as if everything was OK, and the caller passes the error into `newPage` instead of the browser. `newPage` runs `errorObject.newPage()`, which is not a function, and there's the crash. Remove the `try`/`catch` from `launchBrowser` and run it again so you can see what the actual problem is, then please update your post with the latest code and the error message from that run. – ggorlen Aug 08 '23 at 19:44
  • 1
    I think we got to the root cause after removing 1launchBrowser` from try catch. The error is `Looks like you launched a headed browser without having a XServer running` – A Hassan Aug 09 '23 at 03:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254859/discussion-between-a-hassan-and-ggorlen). – A Hassan Aug 09 '23 at 03:46
  • 1
    That's the root cause. You could try `headless: true,`, or try suggestions in existing threads like [1](https://stackoverflow.com/questions/74266883/why-did-a-plawright-python-app-run-in-docker-failed-headless-false/75271849#75271849), [2](https://github.com/microsoft/playwright/issues/14250), [3](https://github.com/microsoft/playwright/discussions/21148). I suggest updating your code and question title, since `browser.newPage() is not a function` is no longer relevant and wasn't the real issue. You're more likely to get help by focusing on the actual error you just shared. – ggorlen Aug 09 '23 at 05:18
  • 1
    This has been fixed by setting headless true. Thanks @ggorlen for the assist. – A Hassan Aug 09 '23 at 07:07

0 Answers0