微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

playwright-python-`handleSIGTERM = False`导致UnhandledPromiseRejectionWarning并关闭浏览器

如何解决playwright-python-`handleSIGTERM = False`导致UnhandledPromiseRejectionWarning并关闭浏览器

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

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

handleSIGTERM:可选[bool]在SIGTERM上关闭浏览器进程。 认为true。

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,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

您知道我要怎么做吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。