-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathEmulationHandling.java
More file actions
285 lines (257 loc) · 12.1 KB
/
EmulationHandling.java
File metadata and controls
285 lines (257 loc) · 12.1 KB
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
package aquality.selenium.browser.devtools;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.v142.dom.model.RGBA;
import org.openqa.selenium.devtools.v142.emulation.Emulation;
import org.openqa.selenium.devtools.v142.emulation.model.MediaFeature;
import org.openqa.selenium.devtools.v142.emulation.model.ScreenOrientation;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Implementation of version-independent emulation DevTools commands.
* Currently, only non-experimental extensions are implemented.
* For more information, @see <a href="https://chromedevtools.github.io/devtools-protocol/tot/Emulation/">DevTools Emulation documentation</a>.
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public class EmulationHandling {
private final DevToolsHandling tools;
/**
* Initializes a new instance of the {@link NetworkHandling} class.
*
* @param tools Instance of {@link DevToolsHandling}.
*/
public EmulationHandling(DevToolsHandling tools) {
this.tools = tools;
}
/**
* Tells whether emulation is supported.
*
* @return true if the emulation is supported, false otherwise.
*/
@Deprecated
public boolean canEmulate() {
return tools.sendCommand(Emulation.canEmulate());
}
/**
* Overrides the GeoLocation Position or Error. Omitting any of the parameters emulates position unavailable.
*
* @param latitude Latitude of location
* @param longitude Longitude of location
* @param accuracy Accuracy of the location
*/
public void setGeolocationOverride(double latitude, double longitude, double accuracy) {
setGeolocationOverride(Optional.of(latitude), Optional.of(longitude), Optional.of(accuracy), Optional.empty(),
Optional.empty(), Optional.empty(), Optional.empty());
}
/**
* Overrides the GeoLocation Position. Accuracy of the geoLocation is set to 1 meaning 100% accuracy.
* Omitting any of the parameters emulates position unavailable.
*
* @param latitude Latitude of location
* @param longitude Longitude of location
*/
public void setGeolocationOverride(double latitude, double longitude) {
setGeolocationOverride(latitude, longitude, 1);
}
/**
* Overrides the GeoLocation Position or Error. Omitting any of the parameters emulates position unavailable.
*
* @param latitude Latitude of location
* @param longitude Longitude of location
* @param altitude Altitude of location
* @param accuracy Accuracy of the location
* @param altitudeAccuracy Altitude accuracy of the location
* @param heading Heading of location
* @param speed Speed of location
*/
public void setGeolocationOverride(Optional<Number> latitude, Optional<Number> longitude, Optional<Number> accuracy,
Optional<Number> altitude, Optional<Number> altitudeAccuracy, Optional<Number> heading,
Optional<Number> speed) {
tools.sendCommand(Emulation.setGeolocationOverride(latitude, longitude, accuracy, altitude, altitudeAccuracy, heading, speed));
}
/**
* Clears the overridden GeoLocation Position and Error.
*/
public void clearGeolocationOverride() {
tools.sendCommand(Emulation.clearGeolocationOverride());
}
/**
* Overrides the values of device screen dimensions.
*
* @param params Version-specific set of parameters. For example, take a look at {@link Emulation#setDeviceMetricsOverride}
*/
public void setDeviceMetricsOverride(Map<String, Object> params) {
tools.sendCommand(new Command<Void>("Emulation.setDeviceMetricsOverride", params));
}
/**
* Overrides the values of device screen dimensions.
*
* @param width Value to override window.screen.width
* @param height Value to override window.screen.height
* @param deviceScaleFactor Overriding device scale factor value. 0 disables the override.
* @param mobile Whether to emulate mobile device. This includes viewport meta tag, overlay scrollbars, text auto-sizing and more.
*/
public void setDeviceMetricsOverride(Integer width, Integer height, Number deviceScaleFactor, Boolean mobile) {
setDeviceMetricsOverride(width, height, deviceScaleFactor, mobile, Optional.empty(), Optional.empty());
}
/**
* Overrides the values of device screen dimensions.
*
* @param width Value to override window.screen.width
* @param height Value to override window.screen.height
* @param deviceScaleFactor Overriding device scale factor value. 0 disables the override.
* @param mobile Whether to emulate mobile device. This includes viewport meta tag, overlay scrollbars, text auto-sizing and more.
* @param screenOrientationType Orientation type.
* Allowed Values (in any case): portraitPrimary, portraitSecondary, landscapePrimary, landscapeSecondary.
* @param screenOrientationAngle Orientation angle. Set only if orientation type was set.
*/
public void setDeviceMetricsOverride(Integer width, Integer height, Number deviceScaleFactor, Boolean mobile,
Optional<String> screenOrientationType, Optional<Integer> screenOrientationAngle) {
Optional<ScreenOrientation> screenOrientation = Optional.empty();
if (screenOrientationType.isPresent() && StringUtils.isNotEmpty(screenOrientationType.get())) {
Integer angle = 0;
if (screenOrientationAngle.isPresent()) {
angle = screenOrientationAngle.get();
}
screenOrientation = Optional.of(new ScreenOrientation(ScreenOrientation.Type.fromString(screenOrientationType.get()), angle));
}
tools.sendCommand(Emulation.setDeviceMetricsOverride(width, height, deviceScaleFactor, mobile, Optional.empty(), Optional.empty(), Optional.empty(),
Optional.empty(), Optional.empty(), Optional.empty(), screenOrientation, Optional.empty(), Optional.empty(), Optional.empty()));
}
/**
* Clears the overridden device metrics.
*/
public void clearDeviceMetricsOverride() {
tools.sendCommand(Emulation.clearDeviceMetricsOverride());
}
/**
* Overrides the values of user agent.
*
* @param params Version-specific set of parameters.
* For example, take a look at {@link Emulation#setUserAgentOverride}
*/
public void setUserAgentOverride(Map<String, Object> params) {
tools.sendCommand(new Command<Void>("Emulation.setUserAgentOverride", params));
}
/**
* Overrides the values of user agent.
*
* @param userAgent User agent to use.
*/
public void setUserAgentOverride(String userAgent) {
setUserAgentOverride(userAgent, Optional.empty(), Optional.empty());
}
/**
* Overrides the values of user agent.
*
* @param userAgent User agent to use.
* @param acceptLanguage Browser language to emulate.
* @param platform The platform navigator.platform should return.
*/
public void setUserAgentOverride(String userAgent, Optional<String> acceptLanguage, Optional<String> platform) {
tools.sendCommand(Emulation.setUserAgentOverride(userAgent, acceptLanguage, platform, Optional.empty()));
}
/**
* Disables script execution in the page.
*/
public void setScriptExecutionDisabled() {
setScriptExecutionDisabled(true);
}
/**
* Switches script execution in the page.
*
* @param value Whether script execution should be disabled in the page.
*/
public void setScriptExecutionDisabled(boolean value) {
tools.sendCommand(Emulation.setScriptExecutionDisabled(value));
}
/**
* Enables touch on platforms which do not support them.
*/
public void setTouchEmulationEnabled() {
setTouchEmulationEnabled(true);
}
/**
* Enables touch on platforms which do not support them.
*
* @param enabled Whether the touch event emulation should be enabled.
*/
public void setTouchEmulationEnabled(boolean enabled) {
setTouchEmulationEnabled(enabled, Optional.empty());
}
/**
* Enables touch on platforms which do not support them.
*
* @param enabled Whether the touch event emulation should be enabled.
* @param maxTouchPoints Maximum touch points supported. Defaults to one.
*/
public void setTouchEmulationEnabled(boolean enabled, Optional<Integer> maxTouchPoints) {
tools.sendCommand(Emulation.setTouchEmulationEnabled(enabled, maxTouchPoints));
}
/**
* Emulates the given media type or media feature for CSS media queries.
*
* @param params Version-specific set of parameters. For example, take a look at {@link Emulation#setEmulatedMedia}
*/
public void setEmulatedMedia(Map<String, Object> params) {
tools.sendCommand(new Command<Void>("Emulation.setEmulatedMedia", params));
}
/**
* Emulates the given media type or media feature for CSS media queries.
*
* @param media Media type to emulate. Empty string disables the override.
* Possible values: braille, embossed, handheld, print, projection, screen, speech, tty, tv.
* @param mediaFeatures Media features to emulate.
*/
public void setEmulatedMedia(String media, Map<String, String> mediaFeatures) {
setEmulatedMedia(Optional.of(media), Optional.of(mediaFeatures));
}
/**
* Emulates the given media type or media feature for CSS media queries.
*
* @param media Media type to emulate. Empty string disables the override.
* Possible values: braille, embossed, handheld, print, projection, screen, speech, tty, tv.
* @param mediaFeatures Media features to emulate.
*/
public void setEmulatedMedia(Optional<String> media, Optional<Map<String, String>> mediaFeatures) {
List<MediaFeature> featureList = mediaFeatures.orElse(Collections.emptyMap()).entrySet().stream()
.map((Map.Entry<String, String> entry) -> new MediaFeature(entry.getKey(), entry.getValue())).collect(Collectors.toList());
tools.sendCommand(Emulation.setEmulatedMedia(media, Optional.of(featureList)));
}
/**
* Disables emulated media override.
*/
public void disableEmulatedMediaOverride() {
setEmulatedMedia(Collections.singletonMap("media", ""));
}
/**
* Sets an override of the default background color of the frame. This override is used if the content does not specify one.
*
* @param red The red component, in the [0-255] range.
* @param green The green component, in the [0-255] range.
* @param blue The blue component, in the [0-255] range.
*/
public void setDefaultBackgroundColorOverride(int red, int green, int blue) {
setDefaultBackgroundColorOverride(red, green, blue, Optional.empty());
}
/**
* Sets an override of the default background color of the frame. This override is used if the content does not specify one.
*
* @param red The red component, in the [0-255] range.
* @param green The green component, in the [0-255] range.
* @param blue The blue component, in the [0-255] range.
* @param alpha The alpha component, in the [0-1] range (default: 1).
*/
public void setDefaultBackgroundColorOverride(int red, int green, int blue, Optional<Number> alpha) {
tools.sendCommand(Emulation.setDefaultBackgroundColorOverride(Optional.of(new RGBA(red, green, blue, alpha))));
}
/**
* Clears an override of the default background color of the frame.
*/
public void clearDefaultBackgroundColorOverride() {
tools.sendCommand(Emulation.setDefaultBackgroundColorOverride(Optional.empty()));
}
}