繁体   English   中英

Google Puppeteer和Mocha异步调用

[英]Google Puppeteer and Mocha async calls

JavaScript的新手,试图理解如何运行以下简单测试,该测试将加载Google主页并获取标题。 然后测试该标题。

const puppeteer = require("puppeteer");
var page_title = "blank";
assert = require("assert");

async function run() {
  const browser = await puppeteer.launch({ headless: true });
  const page = await browser.newPage();
  await page.goto("http://www.google.co.uk");
  page_title = await page.title();
  console.log("Page Title: ", page_title)
  await browser.close();
}

run();

describe("Google", function() {
  it("Title contains Google", async function() {
    assert.equal(page_title, "Google");
  });
});

问题是在获得page_title之前,describe / it块会运行。 请有人能建议我应该如何实际构造它吗?

您只需要阅读Mocha 文档 无需挖掘位于TOC上的更深层的异步代码

摩卡咖啡提供3种方式:

  • 打回来

    测试完成后,只需调用回调 即可 通过向它添加回调(通常命名为done )。

  • 诺言
  • async await

因此它使用asyncawait如下修改:

const puppeteer = require("puppeteer");
var page_title = "blank";
assert = require("assert");

describe("Google", function() {
    // this.timeout(0);
  it("Title contains Google", async ()=> {
        const browser = await puppeteer.launch();  //headless by default
        const page = await browser.newPage();
        await page.goto("http://www.google.co.uk");
        page_title = await page.title();
        console.log("Page Title: ", page_title);
        assert.equal(page_title, "Google");
        await browser.close()
  });
});

我的建议是快速阅读TOC上的所有说明,并异步阅读简短说明并等待

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM