-
Notifications
You must be signed in to change notification settings - Fork 34
/
DeviceEmulationTest.java
81 lines (70 loc) · 3.36 KB
/
DeviceEmulationTest.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
package tests.usecases.devtools;
import aquality.selenium.browser.AqualityServices;
import aquality.selenium.browser.devtools.EmulationHandling;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.devtools.v131.emulation.Emulation;
import org.openqa.selenium.devtools.v131.emulation.model.DisplayFeature;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import tests.BaseTest;
import theinternet.TheInternetPage;
import java.util.Optional;
import java.util.function.Supplier;
public class DeviceEmulationTest extends BaseTest {
private static final int WIDTH = 600;
private static final int HEIGHT = 1000;
private static final boolean IS_MOBILE = true;
private static final double SCALE_FACTOR = 50;
private static final DisplayFeature DISPLAY_FEATURE = new DisplayFeature(DisplayFeature.Orientation.HORIZONTAL, 0, 0);
@Override
@BeforeMethod
protected void beforeMethod() {
getBrowser().maximize();
}
private static EmulationHandling emulation() {
return AqualityServices.getBrowser().devTools().emulation();
}
@Test
public void setAndClearDeviceMetricsOverrideTest() {
checkDeviceMetricsOverride(() -> emulation().setDeviceMetricsOverride(
WIDTH, HEIGHT, SCALE_FACTOR, IS_MOBILE));
}
@Test
public void setAndClearDeviceMetricsOverrideWithVersionSpecificParametersTest() {
checkDeviceMetricsOverride(() -> {
AqualityServices.getBrowser().devTools().sendCommand(Emulation.setDeviceMetricsOverride(
WIDTH, HEIGHT, SCALE_FACTOR, IS_MOBILE, Optional.empty(), Optional.empty(), Optional.empty(),
Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(),
Optional.of(DISPLAY_FEATURE), Optional.empty()
));
AqualityServices.getLogger().info("success");
});
}
@Test
public void setAndClearDeviceMetricsOverrideWithParametersMapTest() {
ImmutableMap.Builder<String, Object> params = ImmutableMap.builder();
params.put("width", WIDTH);
params.put("height", HEIGHT);
params.put("deviceScaleFactor", SCALE_FACTOR);
params.put("mobile", IS_MOBILE);
params.put("displayFeature", DISPLAY_FEATURE);
checkDeviceMetricsOverride(() -> emulation().setDeviceMetricsOverride(params.build()));
}
private void checkDeviceMetricsOverride(Runnable setAction) {
Supplier<Long> getWindowHeight = () -> getScriptResultOrDefault("getWindowSize.js", 0L);
final int initialValue = getWindowHeight.get().intValue();
if (initialValue == HEIGHT) {
throw new IllegalArgumentException("To check that override works, initial value should differ from the new one");
}
setAction.run();
navigate(TheInternetPage.TABLES);
Assert.assertEquals(getWindowHeight.get(), HEIGHT, "Browser height should match to override value");
emulation().clearDeviceMetricsOverride();
AqualityServices.getConditionalWait().waitFor(() -> {
AqualityServices.getBrowser().refresh();
return getWindowHeight.get() == initialValue;
});
Assert.assertEquals(getWindowHeight.get(), initialValue, "Browser height should match to initial value after clear");
}
}