测试需要剧作家访问麦克风的网站
出现相关弹出窗口,并且似乎忽略了所谓的授予权限。可以手动授予权限,但这似乎违背了自动化的精神。
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context(permissions=['microphone'])
...
由于某种原因,通过上下文授予权限不起作用。权限弹出窗口仍然出现。
我还尝试使用剧作家录制脚本录制演练,但它不是授予麦克风权限的录制。
您缺少一些命令行 flags 告诉 chrome 模拟有麦克风。尝试一下这个示例。
from playwright.sync_api import sync_playwright
def run(playwright):
chromium = playwright.chromium
browser = chromium.launch(headless=False, args=['--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream'])
context = browser.new_context()
context.grant_permissions(permissions=['microphone'])
page = context.new_page()
page.goto("https://permission.site/")
page.click('#microphone')
page.pause()
# other actions...
browser.close()
with sync_playwright() as playwright:
run(playwright)
如果有人使用 Javascript 环境而不是 Python 登陆这里,我确实发现这似乎可行:
test.beforeEach(async ({ context }) => {
await context.grantPermissions(['microphone', 'camera']);
});
只需在测试前添加:
test('some test description', async ({ page }) => {
await page.goto('/some-page-needing-mic-access');
await expect(page.getByText('Some text')).toBeVisible();
}
这里是相关文档:https://playwright.dev/docs/emulation#permissions
以下是您可以添加的权限列表: https://playwright.dev/docs/api/class-browsercontext#browser-context-grant-permissions