Skip to content

Commit 64d9c82

Browse files
authored
chore: add examples project (#150)
1 parent 0f130d4 commit 64d9c82

File tree

7 files changed

+214
-0
lines changed

7 files changed

+214
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ To run Playwright simply add two dependencies to your Maven project:
3939

4040
## Examples
4141

42+
You can find Maven project with the examples [here](./examples).
43+
4244
#### Page screenshot
4345

4446
This code snippet navigates to whatsmyuseragent.org in Chromium, Firefox and WebKit, and saves 3 screenshots.

examples/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Examples
2+
3+
This directory contains sample [`pom.xml`](./pom.xml) and source code for the examples from the landing [page](../README.md).
4+
5+
You can run them in terminal like this:
6+
7+
```sh
8+
mvn compile exec:java -Dexec.mainClass=org.example.PageScreenshot
9+
```

examples/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>examples</artifactId>
9+
<version>0.1-SNAPSHOT</version>
10+
<name>Playwright Client Examples</name>
11+
<dependencies>
12+
<dependency>
13+
<groupId>com.microsoft.playwright</groupId>
14+
<artifactId>playwright</artifactId>
15+
<version>0.170.2</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>com.microsoft.playwright</groupId>
19+
<artifactId>driver-bundle</artifactId>
20+
<version>0.170.2</version>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-compiler-plugin</artifactId>
28+
<version>3.1</version>
29+
<configuration>
30+
<source>1.8</source>
31+
<target>1.8</target>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.example;
18+
19+
import com.microsoft.playwright.*;
20+
21+
public class EvaluateInBrowserContext {
22+
public static void main(String[] args) throws Exception {
23+
Playwright playwright = Playwright.create();
24+
BrowserType browserType = playwright.firefox();
25+
Browser browser = browserType.launch();
26+
BrowserContext context = browser.newContext();
27+
Page page = context.newPage();
28+
page.navigate("https://www.example.com/");
29+
Object dimensions = page.evaluate("() => {\n" +
30+
" return {\n" +
31+
" width: document.documentElement.clientWidth,\n" +
32+
" height: document.documentElement.clientHeight,\n" +
33+
" deviceScaleFactor: window.devicePixelRatio\n" +
34+
" }\n" +
35+
"}");
36+
System.out.println(dimensions);
37+
browser.close();
38+
playwright.close();
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.example;
18+
19+
import com.microsoft.playwright.*;
20+
21+
public class InterceptNetworkRequests {
22+
public static void main(String[] args) throws Exception {
23+
Playwright playwright = Playwright.create();
24+
BrowserType browserType = playwright.webkit();
25+
Browser browser = browserType.launch();
26+
BrowserContext context = browser.newContext();
27+
Page page = context.newPage();
28+
page.route("**", route -> {
29+
System.out.println(route.request().url());
30+
route.continue_();
31+
});
32+
page.navigate("http://todomvc.com");
33+
browser.close();
34+
playwright.close();
35+
}
36+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.example;
18+
19+
import com.microsoft.playwright.*;
20+
21+
import java.nio.file.Paths;
22+
23+
import static java.util.Arrays.asList;
24+
25+
public class MobileAndGeolocation {
26+
public static void main(String[] args) throws Exception {
27+
Playwright playwright = Playwright.create();
28+
BrowserType browserType = playwright.chromium();
29+
Browser browser = browserType.launch();
30+
DeviceDescriptor pixel2 = playwright.devices().get("Pixel 2");
31+
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
32+
.withViewport(pixel2.viewport().width(), pixel2.viewport().height())
33+
.withUserAgent(pixel2.userAgent())
34+
.withDeviceScaleFactor(pixel2.deviceScaleFactor())
35+
.withIsMobile(pixel2.isMobile())
36+
.withHasTouch(pixel2.hasTouch())
37+
.withLocale("en-US")
38+
.withGeolocation(new Geolocation(41.889938, 12.492507))
39+
.withPermissions(asList("geolocation")));
40+
Page page = context.newPage();
41+
page.navigate("https://www.openstreetmap.org/");
42+
page.click("a[data-original-title=\"Show My Location\"]");
43+
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("colosseum-pixel2.png")));
44+
browser.close();
45+
playwright.close();
46+
}
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.example;
18+
19+
import com.microsoft.playwright.*;
20+
21+
import java.nio.file.Paths;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
25+
public class PageScreenshot {
26+
public static void main(String[] args) throws Exception {
27+
Playwright playwright = Playwright.create();
28+
List<BrowserType> browserTypes = Arrays.asList(
29+
playwright.chromium(),
30+
playwright.webkit(),
31+
playwright.firefox()
32+
);
33+
for (BrowserType browserType : browserTypes) {
34+
Browser browser = browserType.launch();
35+
BrowserContext context = browser.newContext(
36+
new Browser.NewContextOptions().withViewport(800, 600));
37+
Page page = context.newPage();
38+
page.navigate("http://whatsmyuseragent.org/");
39+
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("screenshot-" + browserType.name() + ".png")));
40+
browser.close();
41+
}
42+
playwright.close();
43+
}
44+
}

0 commit comments

Comments
 (0)