playwright-community / playwright-go

Playwright for Go a browser automation library to control Chromium, Firefox and WebKit with a single API.
https://playwright-community.github.io/playwright-go/
MIT License
1.94k stars 144 forks source link

Ways to pass context from one browser instance to another #414

Closed dbelozerovx1 closed 4 months ago

dbelozerovx1 commented 4 months ago

Didn't find Questions tab in this repo so ill ask it here i guess

Is there any way to pass context from one playwright.Browser instance to another? Besides browser.NewContext(BrowserNewContextOptions{}), because state from ctx.StorageState() from browser is StorageState type, and for NewContext is OptionalStorageState and nested Cookie is also different types, its so frustrating, i think its not intended to work like this, manually build context every time doesnt seem okay for me

any suggestions, maybe im blind and cant see an easy way to solve my problem?

canstand commented 4 months ago

try share storage state with file:

storageFile := "storage.json"

sharedBrowserContextOption := playwright.BrowserNewContextOptions{}

ctx1, _ := browser.NewContext(sharedBrowserContextOption)

ctx1.StorageState(storageFile)  // save storage

sharedBrowserContextOptions.StoragePath = playwright.String(storageFile)

// reuse ctx options
ctx2, _ := browser.NewContext(sharedBrowserContextOptions)
dbelozerovx1 commented 4 months ago

that is one way but not actually what im trying to do, here some context, i need to pass context with storageState from one browser that did some logins/auth to new another browser so the new browser will has same sessions and wont need to login/auth to apps again