1
0
mirror of synced 2026-08-04 14:36:55 +00:00

fix: avoid NPE when saving storage state (#235)

This commit is contained in:
JB Nizet
2021-01-29 20:13:52 +01:00
committed by GitHub
parent 1da197fdf5
commit 68525d1426
2 changed files with 6 additions and 13 deletions
@@ -21,9 +21,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.*;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Consumer;
@@ -320,14 +318,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
JsonElement json = sendMessage("storageState");
StorageState storageState = gson().fromJson(json, StorageState.class);
if (options != null && options.path != null) {
try {
Files.createDirectories(options.path.getParent());
try (FileWriter writer = new FileWriter(options.path.toFile())) {
writer.write(json.toString());
}
} catch (IOException e) {
throw new PlaywrightException("Failed to write storage state to file", e);
}
Utils.writeToFile(json.toString().getBytes(StandardCharsets.UTF_8), options.path);
}
return storageState;
});
@@ -21,8 +21,10 @@ import com.google.gson.JsonObject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
@@ -112,7 +114,7 @@ public class TestBrowserContextStorageState extends TestBase {
" }]\n" +
" }]\n" +
"}\n", JsonObject.class);
try (FileReader reader = new FileReader(path.toFile())) {
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));