如何在木偶操作员自动化的同时在页面上执行js功能?

时间:2018-06-04 07:40:32

标签: javascript puppeteer

假设脚本已导航到某个页面。如何在该脚本中执行js函数?

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);

1 个答案:

答案 0 :(得分:7)

使用.evaluate()函数。

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);