1
0
mirror of synced 2026-08-06 23:46:54 +00:00

fix: change storage state type to String (#250)

This commit is contained in:
Yury Semikhatsky
2021-02-02 18:05:57 -08:00
committed by GitHub
parent a577e62ece
commit b60bbc8b88
9 changed files with 75 additions and 141 deletions
@@ -218,11 +218,14 @@ public interface Browser extends AutoCloseable {
*/
public RecordVideo recordVideo;
/**
* Populates context with given storage state. This method can be used to initialize context with logged-in information
* obtained via [{@code method: BrowserContext.storageState}]. Either a path to the file with saved storage, or an object with
* the following fields:
* Populates context with given storage state. This option can be used to initialize context with logged-in information
* obtained via [{@code method: BrowserContext.storageState}].
*/
public String storageState;
/**
* Populates context with given storage state. This option can be used to initialize context with logged-in information
* obtained via [{@code method: BrowserContext.storageState}]. Path to the file with saved storage state.
*/
public BrowserContext.StorageState storageState;
public Path storageStatePath;
/**
* Changes the timezone of the context. See
@@ -307,13 +310,11 @@ public interface Browser extends AutoCloseable {
this.recordVideo = new RecordVideo();
return this.recordVideo;
}
public NewContextOptions withStorageState(BrowserContext.StorageState storageState) {
public NewContextOptions withStorageState(String storageState) {
this.storageState = storageState;
this.storageStatePath = null;
return this;
}
public NewContextOptions withStorageState(Path storageStatePath) {
this.storageState = null;
public NewContextOptions withStorageStatePath(Path storageStatePath) {
this.storageStatePath = storageStatePath;
return this;
}
@@ -507,11 +508,14 @@ public interface Browser extends AutoCloseable {
*/
public RecordVideo recordVideo;
/**
* Populates context with given storage state. This method can be used to initialize context with logged-in information
* obtained via [{@code method: BrowserContext.storageState}]. Either a path to the file with saved storage, or an object with
* the following fields:
* Populates context with given storage state. This option can be used to initialize context with logged-in information
* obtained via [{@code method: BrowserContext.storageState}].
*/
public String storageState;
/**
* Populates context with given storage state. This option can be used to initialize context with logged-in information
* obtained via [{@code method: BrowserContext.storageState}]. Path to the file with saved storage state.
*/
public BrowserContext.StorageState storageState;
public Path storageStatePath;
/**
* Changes the timezone of the context. See
@@ -596,13 +600,11 @@ public interface Browser extends AutoCloseable {
this.recordVideo = new RecordVideo();
return this.recordVideo;
}
public NewPageOptions withStorageState(BrowserContext.StorageState storageState) {
public NewPageOptions withStorageState(String storageState) {
this.storageState = storageState;
this.storageStatePath = null;
return this;
}
public NewPageOptions withStorageState(Path storageStatePath) {
this.storageState = null;
public NewPageOptions withStorageStatePath(Path storageStatePath) {
this.storageStatePath = storageStatePath;
return this;
}
@@ -54,46 +54,6 @@ public interface BrowserContext extends AutoCloseable {
}
}
class StorageState {
public List<AddCookie> cookies;
public List<OriginState> origins;
public static class OriginState {
public final String origin;
public List<LocalStorageItem> localStorage;
public static class LocalStorageItem {
public String name;
public String value;
public LocalStorageItem(String name, String value) {
this.name = name;
this.value = value;
}
}
public OriginState(String origin) {
this.origin = origin;
}
public OriginState withLocalStorage(List<LocalStorageItem> localStorage) {
this.localStorage = localStorage;
return this;
}
}
public StorageState() {
cookies = new ArrayList<>();
origins = new ArrayList<>();
}
public List<AddCookie> cookies() {
return this.cookies;
}
public List<OriginState> origins() {
return this.origins;
}
}
void onClose(Consumer<BrowserContext> handler);
void offClose(Consumer<BrowserContext> handler);
@@ -431,13 +391,13 @@ public interface BrowserContext extends AutoCloseable {
* @param offline Whether to emulate network being offline for the browser context.
*/
void setOffline(boolean offline);
default StorageState storageState() {
default String storageState() {
return storageState(null);
}
/**
* Returns storage state for this browser context, contains current cookies and local storage snapshot.
*/
StorageState storageState(StorageStateOptions options);
String storageState(StorageStateOptions options);
default void unroute(String url) { unroute(url, null); }
default void unroute(Pattern url) { unroute(url, null); }
default void unroute(Predicate<String> url) { unroute(url, null); }
@@ -658,8 +658,9 @@ public interface BrowserType {
* context will automatically close the browser.
*
* @param userDataDir Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for
* [Chromium](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md) and
* [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#User_Profile).
* [Chromium](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md#introduction) and
* [Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#User_Profile). Note that Chromium's user
* data directory is the **parent** directory of the "Profile Path" seen at {@code chrome://version}.
*/
BrowserContext launchPersistentContext(Path userDataDir, LaunchPersistentContextOptions options);
/**
@@ -313,12 +313,12 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
}
@Override
public StorageState storageState(StorageStateOptions options) {
public String storageState(StorageStateOptions options) {
return withLogging("BrowserContext.storageState", () -> {
JsonElement json = sendMessage("storageState");
StorageState storageState = gson().fromJson(json, StorageState.class);
String storageState = json.toString();
if (options != null && options.path != null) {
Utils.writeToFile(json.toString().getBytes(StandardCharsets.UTF_8), options.path);
Utils.writeToFile(storageState.getBytes(StandardCharsets.UTF_8), options.path);
}
return storageState;
});
@@ -16,6 +16,7 @@
package com.microsoft.playwright.impl;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.*;
@@ -23,6 +24,8 @@ import com.microsoft.playwright.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -90,14 +93,23 @@ class BrowserImpl extends ChannelOwner implements Browser {
options = new NewContextOptions();
}
if (options.storageStatePath != null) {
try (FileReader reader = new FileReader(options.storageStatePath.toFile())) {
options.storageState = gson().fromJson(reader, BrowserContext.StorageState.class);
try {
byte[] bytes = Files.readAllBytes(options.storageStatePath);
options.storageState = new String(bytes, StandardCharsets.UTF_8);
options.storageStatePath = null;
} catch (IOException e) {
throw new PlaywrightException("Failed to read storage state from file", e);
}
}
JsonObject storageState = null;
if (options.storageState != null) {
storageState = new Gson().fromJson(options.storageState, JsonObject.class);
options.storageState = null;
}
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
if (storageState != null) {
params.add("storageState", storageState);
}
JsonElement result = sendMessage("newContext", params);
BrowserContextImpl context = connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("context").get("guid").getAsString());
if (options.recordVideo != null) {
@@ -43,9 +43,10 @@ public class TestBrowserContextStorageState extends TestBase {
page.evaluate("localStorage['name1'] = 'value1';");
page.navigate("https://www.domain.com");
page.evaluate("localStorage['name2'] = 'value2';");
BrowserContext.StorageState storageState = context.storageState();
assertJsonEquals("[{\n" +
String storageState = context.storageState();
assertJsonEquals("{" +
"cookies:[]," +
"origins:[{\n" +
" origin: 'https://www.example.com',\n" +
" localStorage: [{\n" +
" name: 'name1',\n" +
@@ -57,15 +58,22 @@ public class TestBrowserContextStorageState extends TestBase {
" name: 'name2',\n" +
" value: 'value2'\n" +
" }]\n" +
"}]", storageState.origins());
"}]}", new Gson().fromJson(storageState, JsonObject.class));
}
@Test
void shouldSetLocalStorage() {
BrowserContext.StorageState storageState = new BrowserContext.StorageState();
storageState.origins.add(new BrowserContext.StorageState.OriginState("https://www.example.com")
.withLocalStorage(Arrays.asList(
new BrowserContext.StorageState.OriginState.LocalStorageItem("name1", "value1"))));
String storageState = "{\n" +
" origins: [\n" +
" {\n" +
" origin: 'https://www.example.com',\n" +
" localStorage: [{\n" +
" name: 'name1',\n" +
" value: 'value1'\n" +
" }]\n" +
" }\n" +
" ]\n" +
"}";
BrowserContext context = browser.newContext(new Browser.NewContextOptions().withStorageState(storageState));
Page page = context.newPage();
page.route("**/*", route -> {
@@ -90,7 +98,7 @@ public class TestBrowserContextStorageState extends TestBase {
" return document.cookie;\n" +
"}");
Path path = tempDir.resolve("storage-state.json");
BrowserContext.StorageState state = context.storageState(new BrowserContext.StorageStateOptions().withPath(path));
context.storageState(new BrowserContext.StorageStateOptions().withPath(path));
JsonObject expected = new Gson().fromJson(
"{\n" +
" \"cookies\":[\n" +
@@ -117,7 +125,7 @@ public class TestBrowserContextStorageState extends TestBase {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(path.toFile()), StandardCharsets.UTF_8)) {
assertEquals(expected, new Gson().fromJson(reader, JsonObject.class));
}
BrowserContext context2 = browser.newContext(new Browser.NewContextOptions().withStorageState(path));
BrowserContext context2 = browser.newContext(new Browser.NewContextOptions().withStorageStatePath(path));
Page page2 = context2.newPage();
page2.route("**/*", route -> {
route.fulfill(new Route.FulfillOptions().withBody("<html></html>"));