【问题标题】:How to load multiple storageStates into a single context in playwright如何在 playwright 中将多个 storageStates 加载到单个上下文中
【发布时间】:2021-09-27 16:44:10
【问题描述】:

所以我想在 playwright 中将多个 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"
    });

我如何做到这一点?

【问题讨论】:

    标签: javascript node.js selenium automation playwright


    【解决方案1】:

    你不能像那样加载多个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
    });
    

    这里我们只是使用扩展 (...) 运算符来合并两个 storageState 对象。

    注意:

    如果两个对象都有同名的属性,则第二个对象属性会覆盖第一个。

    如果这是一个问题,您可以从lodash 尝试_.merge

    【讨论】:

    • 状态已加载但无法正常工作,所以我尝试记录状态,这是 pastebin.pl/view/66707db8 记录的内容
    • @Spidy 哎呀!我忘了JSON.parse-试试我编辑的sn-p
    猜你喜欢
    • 2022-10-02
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2021-03-08
    • 2013-07-25
    • 1970-01-01
    相关资源
    最近更新 更多