Search code examples
node.jspuppeteerrequireinstanceoftypeof

NodeJS check typeOf node/module (Puppeteer)


i am struggling with type checking at nodejs. I plan to use the library Puppeteer to test my own web pages. For this I would like to check in the methods that the function arguments have the correct type.

How do I access the type "Page" from the module "Puppeteer"?

Here's a code example

const pup = require('puppeteer');

/**
 * 
 * @param {puppeteer.page} page Puppeter Browser Page (Tab)
 */
async function navigateToPage(page){
    if(page instanceof page) throw("The passed variable page is not typeOf puppeteer.page")

} 


Solution

  • i did this to get the class name :

    const puppeteer = require('puppeteer');
    
    (async () => {
        const browser = await puppeteer.launch({headless: true, args: ['--no-sandbox']});
        const page = await browser.newPage();
        console.log(page.constructor.name);
        process.exit(0);
        // show Page
    })();
    

    And i found a file in source code defining this class :

    https://github.com/puppeteer/puppeteer/blob/main/src/common/Page.ts

    but when i explore the node module folder i found it here : 'puppeteer/lib/cjs/puppeteer/common/Page'

    nevertheless, this code seems to work.

    const puppeteer = require('puppeteer');
    const {Page} = require('puppeteer/lib/cjs/puppeteer/common/Page');
    
    (async () => {
        const browser = await puppeteer.launch({headless: true, args: ['--no-sandbox']});
        const page = await browser.newPage();
        console.log(page instanceof Page);
    
    })();
    

    what do you try to achieve with that ?