javascript - 如何在 playwright 中将多个 storageStates 加载到单个上下文中
问题描述
所以我想将多个 storageStates 加载到剧作家的单个上下文中,但是当我这样做时
const context = await browser.newContext({
storageState: "telegram.json",storageState: "google.json"
});
或这个
const context = await browser.newContext({
storageState: ["telegram.json","google.json"]
});
没有加载任何状态。
但是下面的代码会加载一个 storageState
const context = await browser.newContext({
storageState: "telegram.json"
});
我如何实现这一目标?
解决方案
你不能storageState
像那样加载多个,但你可以这样做应该达到相同的结果:
const fs = require('fs-extra');
const telegramState = JSON.parse(fs.readFileSync('telegram.json'));
const googleState = JSON.parse(fs.readFileSync('google.json'));
// merge them into one
let state = { ...telegramState, ...googleState }
const context = await browser.newContext({
storageState: state
});
这里我们只是使用了 spread (...) 操作符来合并两个 storageState 对象。
注意:
如果两个对象都有同名的属性,则第二个对象属性将覆盖第一个。
如果这是一个问题,您可以_.merge
从lodash尝试。
推荐阅读
- html - 如何覆盖 Bootstrap 的默认字体大小?
- python - 我如何邀请自己到我的不和谐机器人所在的服务器?
- reactjs - 为什么我的自定义域间歇性地得到 ERR_CONNECTION_REFUSED,但 Netlify 站点很好?
- flutter - 在颤动中出错:-getter 在 null 上被调用
- python - Python 中的导入问题 | 从 Splitwise.models.bill 导入 Bill ModuleNotFoundError:没有名为“Splitwise”的模块
- json - 在 json 中获取数组长度,Nifi
- sql - 查询以获取圈子 postgres 内的所有功能
- cypress - 我收到 cypress 错误“TypeError 无法读取未定义的属性‘数据’”
- opencv - 对 AKAZE 参数有什么解释吗?
- python - 带有用户输入和自定义按钮的 Tkinter 对话框