Skip to content

Commit 0485994

Browse files
authored
docs: add geolocation, eval and interception examples (#132)
1 parent b40cb21 commit 0485994

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

README.md

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public class PageScreenshot {
4545
playwright.firefox()
4646
);
4747
for (BrowserType browserType : browserTypes) {
48-
Browser browser = browserType.launch(
49-
new BrowserType.LaunchOptions().withHeadless(false));
48+
Browser browser = browserType.launch();
5049
BrowserContext context = browser.newContext(
5150
new Browser.NewContextOptions().withViewport(800, 600));
5251
Page page = context.newPage();
@@ -59,6 +58,94 @@ public class PageScreenshot {
5958
}
6059
```
6160

61+
#### Mobile and geolocation
62+
63+
This snippet emulates Mobile Chromium on a device at a given geolocation, navigates to openstreetmap.org, performs action and takes a screenshot.
64+
65+
```java
66+
import com.microsoft.playwright.*;
67+
import java.nio.file.Paths;
68+
import static java.util.Arrays.asList;
69+
70+
public class MobileAndGeolocation {
71+
public static void main(String[] args) throws Exception {
72+
Playwright playwright = Playwright.create();
73+
BrowserType browserType = playwright.chromium();
74+
Browser browser = browserType.launch();
75+
DeviceDescriptor pixel2 = playwright.devices().get("Pixel 2");
76+
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
77+
.withViewport(pixel2.viewport().width(), pixel2.viewport().height())
78+
.withUserAgent(pixel2.userAgent())
79+
.withDeviceScaleFactor(pixel2.deviceScaleFactor())
80+
.withIsMobile(pixel2.isMobile())
81+
.withHasTouch(pixel2.hasTouch())
82+
.withLocale("en-US")
83+
.withGeolocation(new Geolocation(41.889938, 12.492507))
84+
.withPermissions(asList("geolocation")));
85+
Page page = context.newPage();
86+
page.navigate("https://www.openstreetmap.org/");
87+
page.click("a[data-original-title=\"Show My Location\"]");
88+
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("colosseum-pixel2.png")));
89+
browser.close();
90+
playwright.close();
91+
}
92+
}
93+
```
94+
95+
#### Evaluate in browser context
96+
97+
This code snippet navigates to example.com in Firefox, and executes a script in the page context.
98+
99+
```java
100+
import com.microsoft.playwright.*;
101+
102+
public class EvaluateInBrowserContext {
103+
public static void main(String[] args) throws Exception {
104+
Playwright playwright = Playwright.create();
105+
BrowserType browserType = playwright.firefox();
106+
Browser browser = browserType.launch(new BrowserType.LaunchOptions().withHeadless(false));
107+
BrowserContext context = browser.newContext();
108+
Page page = context.newPage();
109+
page.navigate("https://www.example.com/");
110+
Object dimensions = page.evaluate("() => {\n" +
111+
" return {\n" +
112+
" width: document.documentElement.clientWidth,\n" +
113+
" height: document.documentElement.clientHeight,\n" +
114+
" deviceScaleFactor: window.devicePixelRatio\n" +
115+
" }\n" +
116+
"}");
117+
System.out.println(dimensions);
118+
browser.close();
119+
playwright.close();
120+
}
121+
}
122+
```
123+
124+
#### Intercept network requests
125+
126+
This code snippet sets up request routing for a WebKit page to log all network requests.
127+
128+
```java
129+
import com.microsoft.playwright.*;
130+
131+
public class InterceptNetworkRequests {
132+
public static void main(String[] args) throws Exception {
133+
Playwright playwright = Playwright.create();
134+
BrowserType browserType = playwright.webkit();
135+
Browser browser = browserType.launch();
136+
BrowserContext context = browser.newContext();
137+
Page page = context.newPage();
138+
page.route("**", route -> {
139+
System.out.println(route.request().url());
140+
route.continue_();
141+
});
142+
page.navigate("http://todomvc.com");
143+
browser.close();
144+
playwright.close();
145+
}
146+
}
147+
```
148+
62149
## Notes
63150

64151
Follow [the instructions](https://github.com/microsoft/playwright-java/blob/master/CONTRIBUTING.md#getting-code) to build the project from source and install driver.

0 commit comments

Comments
 (0)