-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowserr.java
332 lines (290 loc) · 12.4 KB
/
Browserr.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.github.sudharsan_selvaraj.cdp;
import com.github.sudharsan_selvaraj.SeleniumTest;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.devtools.v85.page.Page;
import org.openqa.selenium.devtools.v91.browser.Browser;
import org.openqa.selenium.devtools.v91.browser.model.*;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import static org.testng.Assert.*;
public class Browserr extends SeleniumTest {
/**
* Close browser gracefully.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-close">Browser.close</a>
*/
@Test
public void browserClose() {
getDevTools().send(Browser.close());
sleep(1000);
try {
getDriver().getTitle();
fail("Exception not thrown after destroying browser session using close command");
} catch (Exception e) {
System.out.println(e.getMessage());
assertTrue(e.getMessage().contains("chrome not reachable"));
}
}
/**
* Returns browser version information.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-getVersion">Browser.getVersion</a>
*/
@Test
public void getVersion() {
Browser.GetVersionResponse response = getDevTools().send(Browser.getVersion());
assertNotEquals(response.getJsVersion(), "");
assertEquals(response.getProtocolVersion(), "1.3");
assertNotEquals(response.getRevision(), "");
assertTrue(response.getUserAgent().contains("Chrome/"));
assertTrue(response.getProduct().contains("Chrome/"));
}
/**
* Cancel a download if in progress.
* <p>
* <b>Note:</b>
* Currently disabled because of error
* <pre>{"id":7,"error":{"code":-32601,"message":"'Browser.cancelDownload' wasn't found"}</pre>
* <p/>
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-cancelDownload">Browser.cancelDownload</a>
*/
@Test(enabled = false)
public void cancelDownload() {
String downloadedFileName = "chromedriver_mac64.zip";
deleteFromDownloadFolder(downloadedFileName);
getDevTools().send(Browser.setDownloadBehavior(
Browser.SetDownloadBehaviorBehavior.ALLOW,
Optional.empty(),
Optional.of(getDownloadDirectory()),
Optional.of(true)
));
getDevTools().send(Page.enable());
getDevTools().addListener(Page.downloadWillBegin(), response -> {
getDevTools().send(Browser.cancelDownload(
response.getGuid(),
Optional.empty()
));
});
getDriver().get("https://chromedriver.storage.googleapis.com/index.html?path=91.0.4472.19/");
findElement(By.linkText(downloadedFileName)).click();
sleep(10000);
}
/**
* Crashes browser on the main thread.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-crash">Browser.crash</a>
*/
@Test
public void crash() {
try {
getDevTools().send(Browser.crash());
fail("Exception not thrown after crashing browser session using crash command");
} catch (Exception e) {
assertTrue(e.getMessage().contains("java.util.concurrent.TimeoutException"));
}
}
/**
* Invoke custom browser commands used by telemetry.
* <p>
* <b>Note:</b>
* Currently disabled because of error
* <pre>{"error":{"code":-32600,"message":"Browser command not supported. BrowserCommandId: closeTabSearch"}}</pre>
* <p/>
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-executeBrowserCommand">Browser.executeBrowserCommand</a>
*/
@Test(enabled = false)
public void executeBrowserCommand() {
getDevTools().send(Browser.executeBrowserCommand(BrowserCommandId.CLOSETABSEARCH));
}
/**
* Returns the command line switches for the browser process if, and only if --enable-automation is on the commandline.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-getBrowserCommandLine">getBrowserCommandLine</a>
*/
@Test
public void getBrowserCommandLine() {
List<String> arguments = getDevTools().send(Browser.getBrowserCommandLine());
assertEquals(arguments.get(arguments.size() - 1), "data:,");
}
/**
* Get a Chrome histogram by name.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-getHistogram">getHistogram</a>
*/
@Test
public void getHistogram() {
Histogram histogram = getDevTools().send(Browser.getHistogram("NetworkService.TimeToFirstResponse", Optional.empty()));
assertEquals(histogram.getName(), "NetworkService.TimeToFirstResponse");
}
/**
* Get Chrome histograms.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-getHistograms">getHistograms</a>
*/
@Test
public void getHistograms() {
List<Histogram> histograms = getDevTools().send(Browser.getHistograms(Optional.empty(), Optional.empty()));
assertTrue(histograms.stream().map(histogram -> histogram.getName()).collect(Collectors.toList()).contains("NetworkService.TimeToFirstResponse"));
}
/**
* Get position and size of the browser window.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-getWindowBounds">getWindowBounds</a>
*/
@Test
public void getWindowBounds() {
getDriver().manage().window().setSize(new Dimension(1200, 700));
Bounds bounds = getDevTools().send(Browser.getWindowBounds(new WindowID(1)));
assertEquals(bounds.getHeight().get().intValue(), 700);
assertEquals(bounds.getWidth().get().intValue(), 1200);
assertEquals(bounds.getWindowState().get().toString(), "normal");
}
/**
* Get the browser window that contains the devtools target.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-getWindowForTarget">getWindowForTarget</a>
*/
@Test
public void getWindowForTarget() {
getDriver().manage().window().setSize(new Dimension(1200, 700));
Browser.GetWindowForTargetResponse response = getDevTools().send(Browser.getWindowForTarget(Optional.empty()));
assertEquals(response.getBounds().getHeight().get().intValue(), 700);
assertEquals(response.getBounds().getWidth().get().intValue(), 1200);
assertEquals(response.getBounds().getWindowState().get().toString(), "normal");
assertEquals(response.getWindowId().toString(), "1");
}
/**
* Grant specific permissions to the given origin and reject all others.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-grantPermissions">grantPermissions</a>
*/
@Test
public void grantPermissions() {
By permissionRequestButton = By.xpath(".//button[text()='Click here to allow access to microphone identifiers']");
getDriver().get("https://mictests.com/");
findElement(By.cssSelector(".done_micDetectedOne"));
assertTrue(findElement(permissionRequestButton).isDisplayed());
getDevTools().send(
Browser.grantPermissions(
Collections.singletonList(PermissionType.AUDIOCAPTURE),
Optional.empty(),
Optional.empty()
)
);
getDriver().navigate().refresh();
findElement(By.cssSelector(".done_micDetectedOne"));
assertEquals(getDriver().findElements(permissionRequestButton).size(), 0);
}
/**
* Reset all permission management for all origins.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-resetPermissions">resetPermissions</a>
*/
@Test
public void resetPermissions() {
By permissionRequestButton = By.xpath(".//button[text()='Click here to allow access to microphone identifiers']");
getDevTools().send(
Browser.grantPermissions(
Collections.singletonList(PermissionType.AUDIOCAPTURE),
Optional.empty(),
Optional.empty()
)
);
getDriver().get("https://mictests.com/");
findElement(By.cssSelector(".done_micDetectedOne"));
assertEquals(getDriver().findElements(permissionRequestButton).size(), 0);
getDevTools().send(Browser.resetPermissions(Optional.empty()));
getDriver().navigate().refresh();
findElement(By.cssSelector(".done_micDetectedOne"));
assertTrue(findElement(permissionRequestButton).isDisplayed());
}
/**
* Set dock tile details, platform-specific.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-setDockTile">setDockTile</a>
*/
@Test
public void setDockTile() throws IOException {
File icon = new File(getClass()
.getClassLoader()
.getResource("testninja_logo.png")
.getFile());
byte[] fileContent = FileUtils.readFileToByteArray(icon);
String encodedString = Base64
.getEncoder()
.encodeToString(fileContent);
getDevTools().send(Browser.setDockTile(Optional.of("TestNinja"), Optional.of(encodedString)));
sleep(10000);
}
/**
* Set the behavior when downloading a file.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-setDownloadBehavior">setDownloadBehavior</a>
*/
@Test
public void setDownloadBehavior() {
String downloadedFileName = "chromedriver_mac64.zip";
deleteFromDownloadFolder(downloadedFileName);
getDevTools().send(Browser.setDownloadBehavior(
Browser.SetDownloadBehaviorBehavior.ALLOW,
Optional.empty(),
Optional.of(getDownloadDirectory()),
Optional.of(true)
));
getDriver().get("https://chromedriver.storage.googleapis.com/index.html?path=91.0.4472.19/");
findElement(By.linkText(downloadedFileName)).click();
sleep(15000);
assertTrue(new File(getDownloadDirectory() + File.separator + downloadedFileName).exists());
}
/**
* Set permission settings for given origin.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-setPermission">setPermission</a>
*/
@Test
public void setPermission() {
By permissionRequestButton = By.xpath(".//button[text()='Click here to allow access to microphone identifiers']");
getDriver().get("https://mictests.com/");
findElement(By.cssSelector(".done_micDetectedOne"));
assertTrue(findElement(permissionRequestButton).isDisplayed());
getDevTools().send(
Browser.setPermission(
new PermissionDescriptor("microphone",
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty()
),
PermissionSetting.GRANTED,
Optional.empty(),
Optional.empty()
)
);
getDriver().navigate().refresh();
findElement(By.cssSelector(".done_micDetectedOne"));
assertEquals(getDriver().findElements(permissionRequestButton).size(), 0);
}
/**
* Set position and/or size of the browser window.
*
* @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-setWindowBounds">setWindowBounds</a>
*/
@Test
public void setWindowBounds() {
getDevTools().send(Browser.setWindowBounds(new WindowID(1),
new Bounds(Optional.empty(),
Optional.empty(),
Optional.of(1200),
Optional.of(700),
Optional.empty()))
);
assertEquals(getDriver().manage().window().getSize(), new Dimension(1200, 700));
}
}