Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a js function on the page while automating in puppeteer?

Say the script has navigated to a certain page. How to execute a js function inside that script?

describe("TestSuite", () => {
  test("Login", async() => {
    await page.goto(APP);
    await page.waitForSelector("[name=loginForm]");
    await page.click("input[name=username]");
    await page.type("input[name=username]", user.username);
    await page.click("input[name=pwd]");
    await page.type("input[name=pwd]", user.pwd);
    await page.click("input[name=login]");
    await page.waitForSelector(".PageBodyVertical");

 // execute a js function x() here which is loaded with the page.

  }, 60000);
like image 214
dina Avatar asked Jun 04 '18 07:06

dina


1 Answers

Use the .evaluate() function.

describe("TestSuite", () => {
  test("Login", async() => {
    await page.goto(APP);
    await page.waitForSelector("[name=loginForm]");
    await page.click("input[name=username]");
    await page.type("input[name=username]", user.username);
    await page.click("input[name=pwd]");
    await page.type("input[name=pwd]", user.pwd);
    await page.click("input[name=login]");
    await page.waitForSelector(".PageBodyVertical");

    await page.evaluate( function(){
      x();
    } );

  }, 60000);
like image 175
Pjotr Raskolnikov Avatar answered Sep 30 '22 17:09

Pjotr Raskolnikov