简体   繁体   中英

How to load multiple storageStates into a single context in playwright

so i want to load multiple storageStates into a single context in playwright but when i do this

const context = await browser.newContext({
      storageState: "telegram.json",storageState: "google.json"
    });

or this

const context = await browser.newContext({
      storageState: ["telegram.json","google.json"]
    });

none of the states is loaded.

But the code below will load one storageState

const context = await browser.newContext({
      storageState: "telegram.json"
    });

How do i achieve this?

You can't load multiple storageState like that but you can do this which should achieve the same result:

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
});

Here we are just using the spread (...) operator to merge the two storageState objects.

Note that:

If both objects have a property with the same name, then the second object property overwrites the first.

If that is a problem you can try _.merge from lodash .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM