我正在运行一个 Playwright 测试,向
http://localhost:3000/somePage
发出请求,并希望通过代理(Fiddler 代理,这样我可以检查流量,但这不是重点)运行该请求。
在我的
playwright.config.ts
我有:
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
proxy: {
server: 'http://127.0.0.1:8888'
}
},
},
]
proxy
键是我在设置项目时添加到 Playwright 生成的配置文件中的内容。
当我运行测试时,出现以下错误并且测试无法运行:
browser.newContext: Browser needs to be launched with the global proxy. If all contexts override the proxy, global proxy will be never used and can be any string, for example "launch({ proxy: { server: 'http://per-context' } })"
在网上搜索,除了几个很久以前就关闭的 github 问题外,几乎没有发现什么。它似乎在抱怨它应该使用代理,但只有......当我告诉它使用代理时。
当我从配置中删除代理时,测试运行得很好。我错过了什么?
通过回避问题解决了这个问题:我使用了 Firefox。
user_agent = 'Mozilla/5.0 (Window...../537.36'
browser = await p.firefox.launch()
context = await browser.new_context(user_agent=user_agent, proxy={
"server": "1xx.xxx.xx.xxx:3xxx"
})
page = await context.new_page()