puppeteer app stuck in mobile while running in headless mode

32 Views Asked by At

While running the application in headless mode mobile screen is returned instead of desktop screen. I have tried fixing the useragent and keeping the mobile option false but nothing helps.

const puppeteer = require('puppeteer');
const puppeteerOptions = require('../../../package.json').puppeteerOptions;

    /**
     * To Manage the Puppeteer browser and page objects.
     */
    class BrowserScope {
        constructor(args) {
            this.browser = null;
            this.config = null;
            this.page = null;
            this.worldParameters = args && args.parameters ? args.parameters : {};
            this.attach = args && args.attach ? args.attach : null;
        }

        async init() {
            const defaultOptions = {
                args: ['--start-maximized', '--disable-gpu', '--no-sandbox', '--disable-dev-shm-         usage', '--disable-setuid-sandbox'],
                ignoreHTTPSErrors: true,
                "defaultViewport": null
            }
            this.close();
            puppeteerOptions.headless == "new" && puppeteerOptions.isMobile == false ?
            puppeteerOptions.defaultViewport = { "width": 1300, "height": 1800 } :
            puppeteerOptions.defaultViewport = { "width": 1300, "height": 1800 };

            this.config = { ...defaultOptions, ...puppeteerOptions, ...this.worldParameters };
            if ('browserWSEndpoint' in this.config) {
                this.browser = await puppeteer.connect(this.config);
            } else {
                this.browser = await puppeteer.launch(this.config);
            }
            this.page = await this.browser.newPage();
            this.page.emulate({
                name: 'Desktop 1920x1080',
                viewport: {
                width: 1920,
                height: 1080
                },
              userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
              });
        }

        async close() {
            if (this.browser) {
                if ('browserWSEndpoint' in this.config) {
                    await this.browser.disconnect();
                } else {
                    let pages = await this.browser.pages();
                    await Promise.all(pages.map(page => page.close()));
                    await this.browser.close();
                }
            }

            this.browser = null;
            this.config = null;
            this.page = null;
        }
    }

    module.exports = BrowserScope;
0

There are 0 best solutions below