在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/網(wǎng)絡(luò)安全/ jest、puppeteer 使用超時(shí)

jest、puppeteer 使用超時(shí)

Timeout - Async callback was not invoked within the 5000ms timeout specified

最近在弄UI測(cè)試,使用的是jest和puppeteer。主要是測(cè)試登錄頁(yè)面。

跟著網(wǎng)上的教程搗鼓了許久,有個(gè)錯(cuò)誤沒(méi)搞懂怎么解決。

錯(cuò)誤如下:

 Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

      63 | });
      64 |
    > 65 | test('login failure', async () => {
         | ^
      66 |   await page.waitForSelector('#username', {
      67 |         timeout: 5000,
      68 |       });

      at Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:85:20)
      at Object.test (src/tests/login.test.js:65:1)

測(cè)試的代碼如下:

const puppeteer = require('puppeteer');

let browser;
let page;

beforeAll(async () => {
  browser = await puppeteer.launch({
    headless: false,
    slowMo: 200,
  });
  page = await browser.newPage();
});

afterAll(() => {
  browser.close();
});

test('open page', async () => {
  await page.goto('localhost:8000/web/login');
});

test('login failure', async () => {
  await page.waitForSelector('#username', {
        timeout: 5000,
      });
  await page.type('#username', 'test');
  await page.type('#password', '1111');
  await page.click('#submit');
  await page.waitForSelector('.ant-message-notice',{ timeout: 5000});
});

研究到現(xiàn)在,倒是把協(xié)議那個(gè)弄好了

協(xié)議那方面的錯(cuò), 我將page.goto('http://localhost:8000/web/login')改成page.goto('localhost:8000/web/login'就Ok了~

超時(shí)這個(gè)問(wèn)題卻一直沒(méi)有進(jìn)展,目前我認(rèn)為是jest的一次測(cè)試默認(rèn)就是不能超過(guò)5s。因?yàn)槲椰F(xiàn)在將步驟簡(jiǎn)化了,把驗(yàn)證登錄時(shí)將輸入的用戶名和密碼變得簡(jiǎn)單,從而將測(cè)試縮在5s之內(nèi)。

 PASS  src/tests/login.test.js (9.914s)
  √ open page (2080ms)
  √ login failure (4981ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        10.871s
Ran all test suites.

所以,有沒(méi)有人了解這方面QAQ,難道是修改jest的配置嗎?還是需要怎么解決,jest的配置也不知道在哪里解決OTZ

回答
編輯回答
不二心

超時(shí)問(wèn)題解決~ 看了這段話,我就豁然開(kāi)朗了~
4 個(gè)主要的生命周期函數(shù):

afterAll(fn, timeout): 當(dāng)前文件中的所有測(cè)試執(zhí)行完成后執(zhí)行 fn, 如果 fn 是 promise,jest 會(huì)等待 timeout 毫秒,默認(rèn) 5000
afterEach(fn, timeout): 每個(gè) test 執(zhí)行完后執(zhí)行 fn,timeout 含義同上
beforeAll(fn, timeout): 同 afterAll,不同之處在于在所有測(cè)試開(kāi)始前執(zhí)行
beforeEach(fn, timeout): 同 afterEach,不同之處在于在每個(gè)測(cè)試開(kāi)始前執(zhí)行

因此我就在想,是不是 每次test后面也是可以設(shè)置timeout呢
圖片描述

然后就pass了~

2017年6月26日 14:59
編輯回答
解夏

首先要把timeout的概念區(qū)分下。
jest這類測(cè)試框架的timeout是指每個(gè)case之間的超時(shí)時(shí)間 (比如before操作過(guò)程則timeout)
puppeteer 這類at框架的timeout是指,select某個(gè)dom節(jié)點(diǎn)時(shí)候的超時(shí)時(shí)間 (比如找一個(gè)button找不到則timeout)
另外at里還有個(gè)概念你估計(jì)已經(jīng)混淆了,就是wait time,指讓頁(yè)面等待一會(huì)兒不做任何操作,你可以把它想象成sleep,puppeteer里應(yīng)該有類似sleep的api供你調(diào)用,puppeteer的每個(gè)api可能也有timeout的參數(shù)供你單獨(dú)設(shè)置,puppeteer 我不熟你自己查查文檔,至少selenium里是有這個(gè)功能的

2018年8月9日 05:47