1

I am running a Script based in Puppeteer, Chai and Mocha that runs locally fine, but not in Google Cloud functions.

I can´t make chai work in Google Cloud Functions.

This is the error I get:

Screenshot Error in Logs

and this is Code I am using in index.js :

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */

const puppeteer = require('puppeteer');
//const expect = require('chai')

describe('My First Puppeteer Test', () => {
    let browser
    let page

    before(async function () {
        browser = await puppeteer.launch({
            headless: true,
            devtools: false,
        })
        page = await browser.newPage()
        await page.setDefaultTimeout(100000)
        await page.setDefaultNavigationTimeout(20000)
    })

    after(async function () {
        await browser.close()
    })

    beforeEach(async function () {
        //Runs before each Test step
    })

    afterEach(async function () {
        //Runs after each Test step
    })

    it('login', async function () {
        await page.goto('https://my.website.com/user/')

        await page.goto('https://my.website.com/user/')
        await page.waitForSelector('#mat-input-0')
        await page.type('#mat-input-0', 'MyUsername')
        await page.waitForTimeout(1000)
        await page.waitForSelector('#mat-input-1')
        await page.type('#mat-input-1', 'MyPassword')

        await page.waitForTimeout(1000)

        await page.click(
            'body > sbnb-root > sbnb-login > section > article > sbnb-login-form > form > button'
        )
        await page.waitForTimeout(10000)
    })

    it('Go To metrics', async function () {
        await page.goto('https://my.website.com/stadistic')

        //Click Reviews
        await page.waitForSelector(
            'body > sbnb-root > sbnb-exports > div > main > section.create-export-container > div:nth-child(2) > section:nth-child(2) > div > div:nth-child(1) > div > a'
        )
        await page.click(
            'body > sbnb-root > sbnb-exports > div > main > section.create-export-container > div:nth-child(2) > section:nth-child(2) > div > div.text__small'
        )
        await page.waitForTimeout(10000)




        // Click Single File

        await page.waitForSelector(
            'body > sbnb-root > sbnb-exports > div > main > section.create-export-container > div:nth-child(8) > div:nth-child(1)'
        )
        await page.click(
            'body > sbnb-root > sbnb-exports > div > main > section.create-export-container > div:nth-child(8) > div:nth-child(1)'
        )
        await page.waitForTimeout(10000)


    })



    it('Click send and close', async function () {

    

        // Click Send File

        await page.waitForSelector(
            'body > sbnb-root > sbnb-exports > div > main > section.create-export-container > div.export-final > button'
        )
        await page.click(
            'body > sbnb-root > sbnb-exports > div > main > section.create-export-container > div.export-final > button'
        )
        await page.waitForTimeout(80000)

        
    })

    
})

and this is how my package.json looks like:

{
    "name": "sample-http",
    "version": "0.0.1",
    "scripts": {
        "test": "mocha --reporter spec"
    },
    "dependencies": {
        "chai": "^4.3.4",
        "mocha": "^9.1.3",
        "puppeteer": "^1.20.0"
    }
}

How I can I make it work in Google Cloud Functions?

Some ideas about how to rewrite the code just using puppeteer in a way that it works?

ErnestoC
  • 2,660
  • 1
  • 6
  • 19
AlejandroRod
  • 83
  • 1
  • 11

1 Answers1

2

Based on the GCP documentation and a related question, it looks like the proper way to test a Cloud Function is to separate the testing and the actual code to be tested. My suggestion is to build your Puppeteer function and deploy it (here is a guide for using Puppeteer with Cloud functions), and then perform tests against it, as shown in the documentation.

As for the specific error you are seeing, it appears that it’s caused by not running the tests through the correct mocha commands. You will be able to run these commands by refactoring your code according to the GCP documentation.

ErnestoC
  • 2,660
  • 1
  • 6
  • 19