Skip to content

Commit 2d179c6

Browse files
authored
feat: update to [email protected] (#134)
1 parent 8e750e5 commit 2d179c6

File tree

11 files changed

+153
-24
lines changed

11 files changed

+153
-24
lines changed

.github/workflows/verify_api.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ jobs:
2424
path: ~/.m2
2525
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
2626
restore-keys: ${{ runner.os }}-m2
27-
- name: Cache Downloaded Drivers
28-
uses: actions/cache@v2
29-
with:
30-
path: driver-bundle/src/main/resources/driver
31-
key: ${{ runner.os }}-drivers-${{ hashFiles('scripts/*') }}
32-
restore-keys: ${{ runner.os }}-drivers
3327
- name: Download drivers
3428
run: scripts/download_driver_for_all_platforms.sh
3529
- name: Regenerate APIs

api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,16 @@ void writeTo(List<String> output, String offset, String access) {
608608
return;
609609
}
610610
if (asList("Page.emulateMedia.params.media",
611-
"Page.emulateMedia.params.colorScheme").contains(jsonPath)) {
611+
"Page.emulateMedia.params.colorScheme").contains(jsonPath)) {
612612
output.add(offset + access + "Optional<" + type.toJava() + "> " + name + ";");
613613
return;
614614
}
615+
if (asList("Browser.newContext.options.storageState",
616+
"Browser.newPage.options.storageState").contains(jsonPath)) {
617+
output.add(offset + access + type.toJava() + " " + name + ";");
618+
output.add(offset + access + "Path " + name + "Path;");
619+
return;
620+
}
615621
output.add(offset + access + type.toJava() + " " + name + ";");
616622
}
617623

@@ -676,6 +682,20 @@ void writeBuilderMethod(List<String> output, String offset, String parentClass)
676682
output.add(offset + "}");
677683
return;
678684
}
685+
if (asList("Browser.newContext.options.storageState",
686+
"Browser.newPage.options.storageState").contains(jsonPath)) {
687+
output.add(offset + "public " + parentClass + " withStorageState(BrowserContext.StorageState storageState) {");
688+
output.add(offset + " this.storageState = storageState;");
689+
output.add(offset + " this.storageStatePath = null;");
690+
output.add(offset + " return this;");
691+
output.add(offset + "}");
692+
output.add(offset + "public " + parentClass + " withStorageState(Path storageStatePath) {");
693+
output.add(offset + " this.storageState = null;");
694+
output.add(offset + " this.storageStatePath = storageStatePath;");
695+
output.add(offset + " return this;");
696+
output.add(offset + "}");
697+
return;
698+
}
679699
if ("Route.continue.overrides.postData".equals(jsonPath)) {
680700
output.add(offset + "public ContinueOverrides withPostData(String postData) {");
681701
output.add(offset + " this.postData = postData.getBytes(StandardCharsets.UTF_8);");
@@ -762,7 +782,7 @@ void writeTo(List<String> output, String offset) {
762782
if ("Download".equals(jsonName)) {
763783
output.add("import java.io.InputStream;");
764784
}
765-
if (asList("Page", "Frame", "ElementHandle", "FileChooser", "Browser", "BrowserType", "Download", "Route", "Selectors").contains(jsonName)) {
785+
if (asList("Page", "Frame", "ElementHandle", "FileChooser", "Browser", "BrowserContext", "BrowserType", "Download", "Route", "Selectors").contains(jsonName)) {
766786
output.add("import java.nio.file.Path;");
767787
}
768788
output.add("import java.util.*;");
@@ -1159,9 +1179,9 @@ public class ApiGenerator {
11591179
List<String> lines = new ArrayList<>();
11601180
new Interface(entry.getValue().getAsJsonObject()).writeTo(lines, "");
11611181
String text = String.join("\n", lines);
1162-
FileWriter writer = new FileWriter(new File(dir, name + ".java"));
1163-
writer.write(text);
1164-
writer.close();
1182+
try (FileWriter writer = new FileWriter(new File(dir, name + ".java"))) {
1183+
writer.write(text);
1184+
}
11651185
}
11661186
}
11671187

api-generator/src/main/java/com/microsoft/playwright/tools/Types.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class Mapping {
119119
add("BrowserType.launchPersistentContext.options.downloadsPath", "string", "Path");
120120
add("BrowserType.launch.options.executablePath", "string", "Path");
121121
add("BrowserType.launch.options.downloadsPath", "string", "Path");
122+
add("BrowserContext.storageState.options.path", "string", "Path");
122123
add("ChromiumBrowser.startTracing.options.path", "string", "Path");
123124

124125
// Route
@@ -298,8 +299,8 @@ class Mapping {
298299

299300
add("BrowserContext.setGeolocation.geolocation", "null|Object", "Geolocation", new Empty());
300301
add("Browser.newContext.options.geolocation", "Object", "Geolocation", new Empty());
301-
add("Browser.newContext.options.storageState", "Object", "BrowserContext.StorageState", new Empty());
302-
add("Browser.newPage.options.storageState", "Object", "BrowserContext.StorageState", new Empty());
302+
add("Browser.newContext.options.storageState", "string|Object", "BrowserContext.StorageState", new Empty());
303+
add("Browser.newPage.options.storageState", "string|Object", "BrowserContext.StorageState", new Empty());
303304
add("Browser.newPage.options.geolocation", "Object", "Geolocation", new Empty());
304305
add("BrowserType.launchPersistentContext.options.geolocation", "Object", "Geolocation", new Empty());
305306
add("Download.saveAs.path", "string", "Path", new Empty());

playwright/src/main/java/com/microsoft/playwright/Browser.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,10 @@ public Proxy withPassword(String password) {
227227
*/
228228
public Proxy proxy;
229229
/**
230-
* Populates context with given storage state. This method can be used to initialize context with logged-in information obtained via browserContext.storageState().
230+
* Populates context with given storage state. This method can be used to initialize context with logged-in information obtained via browserContext.storageState([options]). Either a path to the file with saved storage, or an object with the following fields:
231231
*/
232232
public BrowserContext.StorageState storageState;
233+
public Path storageStatePath;
233234

234235
public NewContextOptions withAcceptDownloads(Boolean acceptDownloads) {
235236
this.acceptDownloads = acceptDownloads;
@@ -317,6 +318,12 @@ public Proxy setProxy() {
317318
}
318319
public NewContextOptions withStorageState(BrowserContext.StorageState storageState) {
319320
this.storageState = storageState;
321+
this.storageStatePath = null;
322+
return this;
323+
}
324+
public NewContextOptions withStorageState(Path storageStatePath) {
325+
this.storageState = null;
326+
this.storageStatePath = storageStatePath;
320327
return this;
321328
}
322329
}
@@ -494,9 +501,10 @@ public Proxy withPassword(String password) {
494501
*/
495502
public Proxy proxy;
496503
/**
497-
* Populates context with given storage state. This method can be used to initialize context with logged-in information obtained via browserContext.storageState().
504+
* Populates context with given storage state. This method can be used to initialize context with logged-in information obtained via browserContext.storageState([options]). Either a path to the file with saved storage, or an object with the following fields:
498505
*/
499506
public BrowserContext.StorageState storageState;
507+
public Path storageStatePath;
500508

501509
public NewPageOptions withAcceptDownloads(Boolean acceptDownloads) {
502510
this.acceptDownloads = acceptDownloads;
@@ -584,6 +592,12 @@ public Proxy setProxy() {
584592
}
585593
public NewPageOptions withStorageState(BrowserContext.StorageState storageState) {
586594
this.storageState = storageState;
595+
this.storageStatePath = null;
596+
return this;
597+
}
598+
public NewPageOptions withStorageState(Path storageStatePath) {
599+
this.storageState = null;
600+
this.storageStatePath = storageStatePath;
587601
return this;
588602
}
589603
}

playwright/src/main/java/com/microsoft/playwright/BrowserContext.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.microsoft.playwright;
1818

19+
import java.nio.file.Path;
1920
import java.util.*;
2021
import java.util.function.Consumer;
2122
import java.util.function.Predicate;
@@ -240,6 +241,17 @@ public GrantPermissionsOptions withOrigin(String origin) {
240241
return this;
241242
}
242243
}
244+
class StorageStateOptions {
245+
/**
246+
* The file path to save the storage state to. If {@code path} is a relative path, then it is resolved relative to current working directory. If no path is provided, storage state is still returned, but won't be saved to the disk.
247+
*/
248+
public Path path;
249+
250+
public StorageStateOptions withPath(Path path) {
251+
this.path = path;
252+
return this;
253+
}
254+
}
243255
/**
244256
* Adds cookies into this browser context. All pages within this context will have these cookies installed. Cookies can be
245257
* <p>
@@ -441,10 +453,13 @@ default void grantPermissions(List<String> permissions) {
441453
* @param offline Whether to emulate network being offline for the browser context.
442454
*/
443455
void setOffline(boolean offline);
456+
default StorageState storageState() {
457+
return storageState(null);
458+
}
444459
/**
445460
* Returns storage state for this browser context, contains current cookies and local storage snapshot.
446461
*/
447-
StorageState storageState();
462+
StorageState storageState(StorageStateOptions options);
448463
default void unroute(String url) { unroute(url, null); }
449464
default void unroute(Pattern url) { unroute(url, null); }
450465
default void unroute(Predicate<String> url) { unroute(url, null); }

playwright/src/main/java/com/microsoft/playwright/Page.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,11 +1540,9 @@ default void focus(String selector) {
15401540
Frame frameByUrl(String glob);
15411541
Frame frameByUrl(Pattern pattern);
15421542
/**
1543-
* Returns frame matching the criteria. Returns {@code null} if no frame matches.
1543+
* Returns frame matching the specified criteria. Either {@code name} or {@code url} must be specified.
15441544
* <p>
15451545
*
1546-
* <p>
1547-
* Returns frame matching the specified criteria. Either {@code name} or {@code url} must be specified.
15481546
* @param frameSelector Frame name or other frame lookup options.
15491547
*/
15501548
Frame frameByUrl(Predicate<String> predicate);

playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
import com.google.gson.JsonObject;
2222
import com.microsoft.playwright.*;
2323

24+
import java.io.File;
25+
import java.io.FileWriter;
26+
import java.io.IOException;
27+
import java.nio.file.Files;
2428
import java.util.*;
2529
import java.util.function.Consumer;
2630
import java.util.function.Predicate;
@@ -242,9 +246,20 @@ public void setOffline(boolean offline) {
242246
}
243247

244248
@Override
245-
public StorageState storageState() {
249+
public StorageState storageState(StorageStateOptions options) {
246250
JsonElement json = sendMessage("storageState");
247-
return gson().fromJson(json, StorageState.class);
251+
StorageState storageState = gson().fromJson(json, StorageState.class);
252+
if (options != null && options.path != null) {
253+
try {
254+
Files.createDirectories(options.path.getParent());
255+
try (FileWriter writer = new FileWriter(options.path.toFile())) {
256+
writer.write(json.toString());
257+
}
258+
} catch (IOException e) {
259+
throw new PlaywrightException("Failed to write storage state to file", e);
260+
}
261+
}
262+
return storageState;
248263
}
249264

250265
@Override

playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import com.google.gson.JsonObject;
2121
import com.microsoft.playwright.*;
2222

23+
import java.io.FileNotFoundException;
24+
import java.io.FileReader;
25+
import java.io.IOException;
2326
import java.util.ArrayList;
2427
import java.util.HashSet;
2528
import java.util.List;
@@ -74,6 +77,14 @@ public BrowserContextImpl newContext(NewContextOptions options) {
7477
if (options == null) {
7578
options = new NewContextOptions();
7679
}
80+
if (options.storageStatePath != null) {
81+
try (FileReader reader = new FileReader(options.storageStatePath.toFile())) {
82+
options.storageState = gson().fromJson(reader, BrowserContext.StorageState.class);
83+
options.storageStatePath = null;
84+
} catch (IOException e) {
85+
throw new PlaywrightException("Failed to read storage state from file", e);
86+
}
87+
}
7788
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
7889
if (options.extraHTTPHeaders != null) {
7990
params.remove("extraHTTPHeaders");

playwright/src/test/java/com/microsoft/playwright/TestBrowserContextStorageState.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616

1717
package com.microsoft.playwright;
1818

19+
import com.google.gson.Gson;
20+
import com.google.gson.JsonObject;
1921
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.io.TempDir;
2023

24+
import java.io.FileReader;
25+
import java.io.IOException;
26+
import java.nio.file.Path;
2127
import java.util.Arrays;
2228

2329
import static com.microsoft.playwright.Utils.assertJsonEquals;
@@ -68,4 +74,57 @@ void shouldSetLocalStorage() {
6874
assertEquals(mapOf("name1", "value1"), localStorage);
6975
context.close();
7076
}
77+
78+
@Test
79+
void shouldRoundTripThroughTheFile(@TempDir Path tempDir) throws IOException {
80+
Page page1 = context.newPage();
81+
page1.route("**/*", route -> {
82+
route.fulfill(new Route.FulfillResponse().withBody("<html></html>"));
83+
});
84+
page1.navigate("https://www.example.com");
85+
page1.evaluate("() => {\n" +
86+
" localStorage['name1'] = 'value1';\n" +
87+
" document.cookie = 'username=John Doe';\n" +
88+
" return document.cookie;\n" +
89+
"}");
90+
Path path = tempDir.resolve("storage-state.json");
91+
BrowserContext.StorageState state = context.storageState(new BrowserContext.StorageStateOptions().withPath(path));
92+
JsonObject expected = new Gson().fromJson(
93+
"{\n" +
94+
" \"cookies\":[\n" +
95+
" { \n" +
96+
" \"name\":\"username\",\n" +
97+
" \"value\":\"John Doe\",\n" +
98+
" \"domain\":\"www.example.com\",\n" +
99+
" \"path\":\"/\",\n" +
100+
" \"expires\":-1,\n" +
101+
" \"httpOnly\":false,\n" +
102+
" \"secure\":false,\n" +
103+
" \"sameSite\":\"None\"\n" +
104+
" }],\n" +
105+
" \"origins\":[\n" +
106+
" {\n" +
107+
" \"origin\":\"https://www.example.com\",\n" +
108+
" \"localStorage\":[\n" +
109+
" {\n" +
110+
" \"name\":\"name1\",\n" +
111+
" \"value\":\"value1\"\n" +
112+
" }]\n" +
113+
" }]\n" +
114+
"}\n", JsonObject.class);
115+
try (FileReader reader = new FileReader(path.toFile())) {
116+
assertEquals(expected, new Gson().fromJson(reader, JsonObject.class));
117+
}
118+
BrowserContext context2 = browser.newContext(new Browser.NewContextOptions().withStorageState(path));
119+
Page page2 = context2.newPage();
120+
page2.route("**/*", route -> {
121+
route.fulfill(new Route.FulfillResponse().withBody("<html></html>"));
122+
});
123+
page2.navigate("https://www.example.com");
124+
Object localStorage = page2.evaluate("window.localStorage");
125+
assertEquals(mapOf("name1", "value1"), localStorage);
126+
Object cookie = page2.evaluate("document.cookie");
127+
assertEquals("username=John Doe", cookie);
128+
context2.close();
129+
}
71130
}

scripts/CLI_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.170.0-next.1607623793189
1+
0.170.0-next.1608058598043

0 commit comments

Comments
 (0)