首页 > 解决方案 > playwright-python - `handleSIGTERM=False` 导致 UnhandledPromiseRejectionWarning 并关闭浏览器

问题描述

我收到 UnhandledPromiseRejectionWarning 错误,并且在尝试模拟不应关闭浏览器的测试失败时关闭浏览器。

文档说我应该将handleSIGTERM设置为 False 并且它不应该在运行结束时关闭浏览器,对吧?所以我做到了(见代码示例)。

handleSIGTERM : Optional[bool] 在 SIGTERM 上关闭浏览器进程。默认为真。

Environment:
node v15.0.1
Python 3.8.0
MacOS Catalina 10.15.7 
playwright 0.152.0

代码示例:

import asyncio

from playwright import async_playwright


async def main():
    playwright = await async_playwright().start()
    browser = await playwright.chromium.launch(headless=False, handleSIGTERM=False, handleSIGHUP=False)
    page = await browser.newPage()
    page.setDefaultTimeout(timeout=5000)

    await page.goto('https://google.com/')

    # the following row simulates test failure
    await page.waitForSelector('input[name="q"]', state='hidden')

    await browser.close()
    await playwright.stop()


if __name__ == "__main__":
    asyncio.run(main())

意外的行为是浏览器关闭并且节点显示以下输出:

(node:28715) UnhandledPromiseRejectionWarning: Error: write EPIPE
    at afterWriteDispatched (internal/stream_base_commons.js:154:25)
    at writeGeneric (internal/stream_base_commons.js:145:3)
    at Socket._writeGeneric (net.js:786:11)
    at Socket._write (net.js:798:8)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at Socket.Writable.write (_stream_writable.js:318:11)
    at Transport.send (/snapshot/playwright-cli/node_modules/playwright/lib/protocol/transport.js:38:25)
    at DispatcherConnection.onmessage (/snapshot/playwright-cli/lib/driver.js:49:61)
    at DispatcherConnection.sendMessageToClient (/snapshot/playwright-cli/node_modules/playwright/lib/dispatchers/dispatcher.js:130:14)
(node:28715) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:28715) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:28715) UnhandledPromiseRejectionWarning: Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:399:19)
    at writeOrBuffer (_stream_writable.js:387:5)
    at Socket.Writable.write (_stream_writable.js:318:11)
    at Transport.send (/snapshot/playwright-cli/node_modules/playwright/lib/protocol/transport.js:38:25)
    at DispatcherConnection.onmessage (/snapshot/playwright-cli/lib/driver.js:49:61)
    at DispatcherConnection.sendMessageToClient (/snapshot/playwright-cli/node_modules/playwright/lib/dispatchers/dispatcher.js:130:14)
    at BrowserContextDispatcher._dispatchEvent (/snapshot/playwright-cli/node_modules/playwright/lib/dispatchers/dispatcher.js:70:26)
    at CRBrowserContext.<anonymous> (/snapshot/playwright-cli/node_modules/playwright/lib/dispatchers/browserContextDispatcher.js:33:18)
    at CRBrowserContext.emit (events.js:315:20)
    at CRBrowserContext._didCloseInternal (/snapshot/playwright-cli/node_modules/playwright/lib/server/browserContext.js:102:14)
(node:28715) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

你知道我做错了什么吗?

标签: playwright

解决方案


推荐阅读