feat: update to cli@0.170.0-next.1608058598043 (#134)

This commit is contained in:
Yury Semikhatsky 2020-12-15 16:08:39 -08:00 committed by GitHub
parent 8e750e58df
commit 2d179c6fc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 153 additions and 24 deletions

View File

@ -24,12 +24,6 @@ jobs:
path: ~/.m2 path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2 restore-keys: ${{ runner.os }}-m2
- name: Cache Downloaded Drivers
uses: actions/cache@v2
with:
path: driver-bundle/src/main/resources/driver
key: ${{ runner.os }}-drivers-${{ hashFiles('scripts/*') }}
restore-keys: ${{ runner.os }}-drivers
- name: Download drivers - name: Download drivers
run: scripts/download_driver_for_all_platforms.sh run: scripts/download_driver_for_all_platforms.sh
- name: Regenerate APIs - name: Regenerate APIs

View File

@ -612,6 +612,12 @@ class Field extends Element {
output.add(offset + access + "Optional<" + type.toJava() + "> " + name + ";"); output.add(offset + access + "Optional<" + type.toJava() + "> " + name + ";");
return; return;
} }
if (asList("Browser.newContext.options.storageState",
"Browser.newPage.options.storageState").contains(jsonPath)) {
output.add(offset + access + type.toJava() + " " + name + ";");
output.add(offset + access + "Path " + name + "Path;");
return;
}
output.add(offset + access + type.toJava() + " " + name + ";"); output.add(offset + access + type.toJava() + " " + name + ";");
} }
@ -676,6 +682,20 @@ class Field extends Element {
output.add(offset + "}"); output.add(offset + "}");
return; return;
} }
if (asList("Browser.newContext.options.storageState",
"Browser.newPage.options.storageState").contains(jsonPath)) {
output.add(offset + "public " + parentClass + " withStorageState(BrowserContext.StorageState storageState) {");
output.add(offset + " this.storageState = storageState;");
output.add(offset + " this.storageStatePath = null;");
output.add(offset + " return this;");
output.add(offset + "}");
output.add(offset + "public " + parentClass + " withStorageState(Path storageStatePath) {");
output.add(offset + " this.storageState = null;");
output.add(offset + " this.storageStatePath = storageStatePath;");
output.add(offset + " return this;");
output.add(offset + "}");
return;
}
if ("Route.continue.overrides.postData".equals(jsonPath)) { if ("Route.continue.overrides.postData".equals(jsonPath)) {
output.add(offset + "public ContinueOverrides withPostData(String postData) {"); output.add(offset + "public ContinueOverrides withPostData(String postData) {");
output.add(offset + " this.postData = postData.getBytes(StandardCharsets.UTF_8);"); output.add(offset + " this.postData = postData.getBytes(StandardCharsets.UTF_8);");
@ -762,7 +782,7 @@ class Interface extends TypeDefinition {
if ("Download".equals(jsonName)) { if ("Download".equals(jsonName)) {
output.add("import java.io.InputStream;"); output.add("import java.io.InputStream;");
} }
if (asList("Page", "Frame", "ElementHandle", "FileChooser", "Browser", "BrowserType", "Download", "Route", "Selectors").contains(jsonName)) { if (asList("Page", "Frame", "ElementHandle", "FileChooser", "Browser", "BrowserContext", "BrowserType", "Download", "Route", "Selectors").contains(jsonName)) {
output.add("import java.nio.file.Path;"); output.add("import java.nio.file.Path;");
} }
output.add("import java.util.*;"); output.add("import java.util.*;");
@ -1159,9 +1179,9 @@ public class ApiGenerator {
List<String> lines = new ArrayList<>(); List<String> lines = new ArrayList<>();
new Interface(entry.getValue().getAsJsonObject()).writeTo(lines, ""); new Interface(entry.getValue().getAsJsonObject()).writeTo(lines, "");
String text = String.join("\n", lines); String text = String.join("\n", lines);
FileWriter writer = new FileWriter(new File(dir, name + ".java")); try (FileWriter writer = new FileWriter(new File(dir, name + ".java"))) {
writer.write(text); writer.write(text);
writer.close(); }
} }
} }

View File

@ -119,6 +119,7 @@ class Types {
add("BrowserType.launchPersistentContext.options.downloadsPath", "string", "Path"); add("BrowserType.launchPersistentContext.options.downloadsPath", "string", "Path");
add("BrowserType.launch.options.executablePath", "string", "Path"); add("BrowserType.launch.options.executablePath", "string", "Path");
add("BrowserType.launch.options.downloadsPath", "string", "Path"); add("BrowserType.launch.options.downloadsPath", "string", "Path");
add("BrowserContext.storageState.options.path", "string", "Path");
add("ChromiumBrowser.startTracing.options.path", "string", "Path"); add("ChromiumBrowser.startTracing.options.path", "string", "Path");
// Route // Route
@ -298,8 +299,8 @@ class Types {
add("BrowserContext.setGeolocation.geolocation", "null|Object", "Geolocation", new Empty()); add("BrowserContext.setGeolocation.geolocation", "null|Object", "Geolocation", new Empty());
add("Browser.newContext.options.geolocation", "Object", "Geolocation", new Empty()); add("Browser.newContext.options.geolocation", "Object", "Geolocation", new Empty());
add("Browser.newContext.options.storageState", "Object", "BrowserContext.StorageState", new Empty()); add("Browser.newContext.options.storageState", "string|Object", "BrowserContext.StorageState", new Empty());
add("Browser.newPage.options.storageState", "Object", "BrowserContext.StorageState", new Empty()); add("Browser.newPage.options.storageState", "string|Object", "BrowserContext.StorageState", new Empty());
add("Browser.newPage.options.geolocation", "Object", "Geolocation", new Empty()); add("Browser.newPage.options.geolocation", "Object", "Geolocation", new Empty());
add("BrowserType.launchPersistentContext.options.geolocation", "Object", "Geolocation", new Empty()); add("BrowserType.launchPersistentContext.options.geolocation", "Object", "Geolocation", new Empty());
add("Download.saveAs.path", "string", "Path", new Empty()); add("Download.saveAs.path", "string", "Path", new Empty());

View File

@ -227,9 +227,10 @@ public interface Browser {
*/ */
public Proxy proxy; public Proxy proxy;
/** /**
* Populates context with given storage state. This method can be used to initialize context with logged-in information obtained via browserContext.storageState(). * 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:
*/ */
public BrowserContext.StorageState storageState; public BrowserContext.StorageState storageState;
public Path storageStatePath;
public NewContextOptions withAcceptDownloads(Boolean acceptDownloads) { public NewContextOptions withAcceptDownloads(Boolean acceptDownloads) {
this.acceptDownloads = acceptDownloads; this.acceptDownloads = acceptDownloads;
@ -317,6 +318,12 @@ public interface Browser {
} }
public NewContextOptions withStorageState(BrowserContext.StorageState storageState) { public NewContextOptions withStorageState(BrowserContext.StorageState storageState) {
this.storageState = storageState; this.storageState = storageState;
this.storageStatePath = null;
return this;
}
public NewContextOptions withStorageState(Path storageStatePath) {
this.storageState = null;
this.storageStatePath = storageStatePath;
return this; return this;
} }
} }
@ -494,9 +501,10 @@ public interface Browser {
*/ */
public Proxy proxy; public Proxy proxy;
/** /**
* Populates context with given storage state. This method can be used to initialize context with logged-in information obtained via browserContext.storageState(). * 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:
*/ */
public BrowserContext.StorageState storageState; public BrowserContext.StorageState storageState;
public Path storageStatePath;
public NewPageOptions withAcceptDownloads(Boolean acceptDownloads) { public NewPageOptions withAcceptDownloads(Boolean acceptDownloads) {
this.acceptDownloads = acceptDownloads; this.acceptDownloads = acceptDownloads;
@ -584,6 +592,12 @@ public interface Browser {
} }
public NewPageOptions withStorageState(BrowserContext.StorageState storageState) { public NewPageOptions withStorageState(BrowserContext.StorageState storageState) {
this.storageState = storageState; this.storageState = storageState;
this.storageStatePath = null;
return this;
}
public NewPageOptions withStorageState(Path storageStatePath) {
this.storageState = null;
this.storageStatePath = storageStatePath;
return this; return this;
} }
} }

View File

@ -16,6 +16,7 @@
package com.microsoft.playwright; package com.microsoft.playwright;
import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -240,6 +241,17 @@ public interface BrowserContext {
return this; return this;
} }
} }
class StorageStateOptions {
/**
* 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.
*/
public Path path;
public StorageStateOptions withPath(Path path) {
this.path = path;
return this;
}
}
/** /**
* Adds cookies into this browser context. All pages within this context will have these cookies installed. Cookies can be * Adds cookies into this browser context. All pages within this context will have these cookies installed. Cookies can be
* <p> * <p>
@ -441,10 +453,13 @@ public interface BrowserContext {
* @param offline Whether to emulate network being offline for the browser context. * @param offline Whether to emulate network being offline for the browser context.
*/ */
void setOffline(boolean offline); void setOffline(boolean offline);
default StorageState storageState() {
return storageState(null);
}
/** /**
* Returns storage state for this browser context, contains current cookies and local storage snapshot. * Returns storage state for this browser context, contains current cookies and local storage snapshot.
*/ */
StorageState storageState(); StorageState storageState(StorageStateOptions options);
default void unroute(String url) { unroute(url, null); } default void unroute(String url) { unroute(url, null); }
default void unroute(Pattern url) { unroute(url, null); } default void unroute(Pattern url) { unroute(url, null); }
default void unroute(Predicate<String> url) { unroute(url, null); } default void unroute(Predicate<String> url) { unroute(url, null); }

View File

@ -1540,11 +1540,9 @@ public interface Page {
Frame frameByUrl(String glob); Frame frameByUrl(String glob);
Frame frameByUrl(Pattern pattern); Frame frameByUrl(Pattern pattern);
/** /**
* Returns frame matching the criteria. Returns {@code null} if no frame matches. * Returns frame matching the specified criteria. Either {@code name} or {@code url} must be specified.
* <p> * <p>
* *
* <p>
* Returns frame matching the specified criteria. Either {@code name} or {@code url} must be specified.
* @param frameSelector Frame name or other frame lookup options. * @param frameSelector Frame name or other frame lookup options.
*/ */
Frame frameByUrl(Predicate<String> predicate); Frame frameByUrl(Predicate<String> predicate);

View File

@ -21,6 +21,10 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.microsoft.playwright.*; import com.microsoft.playwright.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*; import java.util.*;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -242,9 +246,20 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
} }
@Override @Override
public StorageState storageState() { public StorageState storageState(StorageStateOptions options) {
JsonElement json = sendMessage("storageState"); JsonElement json = sendMessage("storageState");
return gson().fromJson(json, StorageState.class); 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);
}
}
return storageState;
} }
@Override @Override

View File

@ -20,6 +20,9 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.microsoft.playwright.*; import com.microsoft.playwright.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -74,6 +77,14 @@ class BrowserImpl extends ChannelOwner implements Browser {
if (options == null) { if (options == null) {
options = new NewContextOptions(); options = new NewContextOptions();
} }
if (options.storageStatePath != null) {
try (FileReader reader = new FileReader(options.storageStatePath.toFile())) {
options.storageState = gson().fromJson(reader, BrowserContext.StorageState.class);
options.storageStatePath = null;
} catch (IOException e) {
throw new PlaywrightException("Failed to read storage state from file", e);
}
}
JsonObject params = gson().toJsonTree(options).getAsJsonObject(); JsonObject params = gson().toJsonTree(options).getAsJsonObject();
if (options.extraHTTPHeaders != null) { if (options.extraHTTPHeaders != null) {
params.remove("extraHTTPHeaders"); params.remove("extraHTTPHeaders");

View File

@ -16,8 +16,14 @@
package com.microsoft.playwright; package com.microsoft.playwright;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import static com.microsoft.playwright.Utils.assertJsonEquals; import static com.microsoft.playwright.Utils.assertJsonEquals;
@ -68,4 +74,57 @@ public class TestBrowserContextStorageState extends TestBase {
assertEquals(mapOf("name1", "value1"), localStorage); assertEquals(mapOf("name1", "value1"), localStorage);
context.close(); context.close();
} }
@Test
void shouldRoundTripThroughTheFile(@TempDir Path tempDir) throws IOException {
Page page1 = context.newPage();
page1.route("**/*", route -> {
route.fulfill(new Route.FulfillResponse().withBody("<html></html>"));
});
page1.navigate("https://www.example.com");
page1.evaluate("() => {\n" +
" localStorage['name1'] = 'value1';\n" +
" document.cookie = 'username=John Doe';\n" +
" return document.cookie;\n" +
"}");
Path path = tempDir.resolve("storage-state.json");
BrowserContext.StorageState state = context.storageState(new BrowserContext.StorageStateOptions().withPath(path));
JsonObject expected = new Gson().fromJson(
"{\n" +
" \"cookies\":[\n" +
" { \n" +
" \"name\":\"username\",\n" +
" \"value\":\"John Doe\",\n" +
" \"domain\":\"www.example.com\",\n" +
" \"path\":\"/\",\n" +
" \"expires\":-1,\n" +
" \"httpOnly\":false,\n" +
" \"secure\":false,\n" +
" \"sameSite\":\"None\"\n" +
" }],\n" +
" \"origins\":[\n" +
" {\n" +
" \"origin\":\"https://www.example.com\",\n" +
" \"localStorage\":[\n" +
" {\n" +
" \"name\":\"name1\",\n" +
" \"value\":\"value1\"\n" +
" }]\n" +
" }]\n" +
"}\n", JsonObject.class);
try (FileReader reader = new FileReader(path.toFile())) {
assertEquals(expected, new Gson().fromJson(reader, JsonObject.class));
}
BrowserContext context2 = browser.newContext(new Browser.NewContextOptions().withStorageState(path));
Page page2 = context2.newPage();
page2.route("**/*", route -> {
route.fulfill(new Route.FulfillResponse().withBody("<html></html>"));
});
page2.navigate("https://www.example.com");
Object localStorage = page2.evaluate("window.localStorage");
assertEquals(mapOf("name1", "value1"), localStorage);
Object cookie = page2.evaluate("document.cookie");
assertEquals("username=John Doe", cookie);
context2.close();
}
} }

View File

@ -1 +1 @@
0.170.0-next.1607623793189 0.170.0-next.1608058598043

View File

@ -6,8 +6,10 @@ set +x
trap "cd $(pwd -P)" EXIT trap "cd $(pwd -P)" EXIT
cd "$(dirname $0)/.." cd "$(dirname $0)/.."
echo "Updating api.json" PLAYWRIGHT_CLI=./driver-bundle/src/main/resources/driver/linux/playwright-cli
./driver-bundle/src/main/resources/driver/linux/playwright-cli print-api-json > ./api-generator/src/main/resources/api.json echo "Updating api.json from $($PLAYWRIGHT_CLI --version)"
$PLAYWRIGHT_CLI print-api-json > ./api-generator/src/main/resources/api.json
mvn compile -projects api-generator --no-transfer-progress mvn compile -projects api-generator --no-transfer-progress