|
14 | 14 | import java.util.List; |
15 | 15 | import java.util.Set; |
16 | 16 | import java.util.Map; |
| 17 | +import java.util.concurrent.atomic.AtomicInteger; |
17 | 18 | import java.util.concurrent.atomic.AtomicReference; |
18 | 19 |
|
19 | 20 | import org.junit.jupiter.api.AfterAll; |
|
32 | 33 | import org.json.JSONObject; |
33 | 34 | import org.openqa.selenium.By; |
34 | 35 | import org.openqa.selenium.Cookie; |
| 36 | +import org.openqa.selenium.Dimension; |
35 | 37 | import org.openqa.selenium.JavascriptExecutor; |
36 | 38 | import org.openqa.selenium.Keys; |
37 | 39 | import org.openqa.selenium.WebDriver; |
@@ -938,6 +940,202 @@ public void createRegionTest() { |
938 | 940 | assertEquals(0.1, assertion.get("diffIgnoreThreshold")); |
939 | 941 | } |
940 | 942 |
|
| 943 | + @Test |
| 944 | + public void captureResponsiveDomResizesToCorrectWidthAndHeight() throws Exception { |
| 945 | + // Serve two widths: one with an explicit height and one without. |
| 946 | + HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); |
| 947 | + server.createContext("/percy/widths-config", exchange -> { |
| 948 | + byte[] body = "{\"widths\":[{\"width\":375,\"height\":812},{\"width\":1280}]}".getBytes("UTF-8"); |
| 949 | + exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, body.length); |
| 950 | + try (OutputStream os = exchange.getResponseBody()) { os.write(body); } |
| 951 | + }); |
| 952 | + server.start(); |
| 953 | + |
| 954 | + String originalAddress = getStaticStringField(Percy.class, "PERCY_SERVER_ADDRESS"); |
| 955 | + try { |
| 956 | + setStaticField(Percy.class, "PERCY_SERVER_ADDRESS", "http://localhost:" + server.getAddress().getPort()); |
| 957 | + |
| 958 | + RemoteWebDriver mockedDriver = mock(RemoteWebDriver.class); |
| 959 | + Percy mockedPercy = spy(new Percy(mockedDriver)); |
| 960 | + setField(mockedPercy, "domJs", "/* percy dom */"); |
| 961 | + setField(mockedPercy, "cliConfig", new JSONObject().put("snapshot", new JSONObject())); |
| 962 | + |
| 963 | + // Track actual setSize calls so window.resizeCount can echo the right value. |
| 964 | + AtomicInteger dimensionChangeCount = new AtomicInteger(0); |
| 965 | + WebDriver.Options driverOptions = mock(WebDriver.Options.class); |
| 966 | + WebDriver.Window driverWindow = mock(WebDriver.Window.class); |
| 967 | + when(mockedDriver.manage()).thenReturn(driverOptions); |
| 968 | + when(driverOptions.window()).thenReturn(driverWindow); |
| 969 | + when(driverWindow.getSize()).thenReturn(new Dimension(1024, 768)); |
| 970 | + doAnswer(inv -> { dimensionChangeCount.incrementAndGet(); return null; }) |
| 971 | + .when(driverWindow).setSize(any(Dimension.class)); |
| 972 | + |
| 973 | + when(mockedDriver.getCurrentUrl()).thenReturn("https://example.com"); |
| 974 | + when(mockedDriver.findElements(By.tagName("iframe"))).thenReturn(Collections.emptyList()); |
| 975 | + when(((JavascriptExecutor) mockedDriver).executeScript(any(String.class))).thenAnswer(invocation -> { |
| 976 | + String script = invocation.getArgument(0); |
| 977 | + if (script.equals("return window.resizeCount")) { |
| 978 | + return (long) dimensionChangeCount.get(); |
| 979 | + } |
| 980 | + if (script.startsWith("return PercyDOM.serialize(")) { |
| 981 | + Map<String, Object> snap = new HashMap<>(); |
| 982 | + snap.put("dom", "test"); |
| 983 | + return snap; |
| 984 | + } |
| 985 | + return null; |
| 986 | + }); |
| 987 | + |
| 988 | + Map<String, Object> options = new HashMap<>(); |
| 989 | + options.put("widths", Arrays.asList(375, 1280)); |
| 990 | + List<Map<String, Object>> snapshots = |
| 991 | + mockedPercy.captureResponsiveDom(mockedDriver, new HashSet<>(), options); |
| 992 | + |
| 993 | + ArgumentCaptor<Dimension> sizeCaptor = ArgumentCaptor.forClass(Dimension.class); |
| 994 | + // 3 calls expected: resize to 375x812, resize to 1280x768, restore 1024x768. |
| 995 | + verify(driverWindow, times(3)).setSize(sizeCaptor.capture()); |
| 996 | + List<Dimension> sizes = sizeCaptor.getAllValues(); |
| 997 | + |
| 998 | + // First width uses the explicit height returned by the server. |
| 999 | + assertEquals(375, sizes.get(0).getWidth()); |
| 1000 | + assertEquals(812, sizes.get(0).getHeight()); |
| 1001 | + |
| 1002 | + // Second width has no explicit height: falls back to currentHeight (768). |
| 1003 | + assertEquals(1280, sizes.get(1).getWidth()); |
| 1004 | + assertEquals(768, sizes.get(1).getHeight()); |
| 1005 | + |
| 1006 | + // Final call restores the original window size. |
| 1007 | + assertEquals(1024, sizes.get(2).getWidth()); |
| 1008 | + assertEquals(768, sizes.get(2).getHeight()); |
| 1009 | + |
| 1010 | + // Each snapshot must carry the width it was captured at. |
| 1011 | + assertEquals(2, snapshots.size()); |
| 1012 | + assertEquals(375, snapshots.get(0).get("width")); |
| 1013 | + assertEquals(1280, snapshots.get(1).get("width")); |
| 1014 | + } finally { |
| 1015 | + setStaticField(Percy.class, "PERCY_SERVER_ADDRESS", originalAddress); |
| 1016 | + server.stop(0); |
| 1017 | + } |
| 1018 | + } |
| 1019 | + |
| 1020 | + @Test |
| 1021 | + public void captureResponsiveDomSkipsResizeWhenDimensionsUnchanged() throws Exception { |
| 1022 | + // Return the exact same width as the initial window — no per-width resize should occur. |
| 1023 | + HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); |
| 1024 | + server.createContext("/percy/widths-config", exchange -> { |
| 1025 | + byte[] body = "{\"widths\":[{\"width\":1024}]}".getBytes("UTF-8"); |
| 1026 | + exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, body.length); |
| 1027 | + try (OutputStream os = exchange.getResponseBody()) { os.write(body); } |
| 1028 | + }); |
| 1029 | + server.start(); |
| 1030 | + |
| 1031 | + String originalAddress = getStaticStringField(Percy.class, "PERCY_SERVER_ADDRESS"); |
| 1032 | + try { |
| 1033 | + setStaticField(Percy.class, "PERCY_SERVER_ADDRESS", "http://localhost:" + server.getAddress().getPort()); |
| 1034 | + |
| 1035 | + RemoteWebDriver mockedDriver = mock(RemoteWebDriver.class); |
| 1036 | + Percy mockedPercy = spy(new Percy(mockedDriver)); |
| 1037 | + setField(mockedPercy, "domJs", "/* percy dom */"); |
| 1038 | + setField(mockedPercy, "cliConfig", new JSONObject().put("snapshot", new JSONObject())); |
| 1039 | + |
| 1040 | + AtomicInteger dimensionChangeCount = new AtomicInteger(0); |
| 1041 | + WebDriver.Options driverOptions = mock(WebDriver.Options.class); |
| 1042 | + WebDriver.Window driverWindow = mock(WebDriver.Window.class); |
| 1043 | + when(mockedDriver.manage()).thenReturn(driverOptions); |
| 1044 | + when(driverOptions.window()).thenReturn(driverWindow); |
| 1045 | + when(driverWindow.getSize()).thenReturn(new Dimension(1024, 768)); |
| 1046 | + doAnswer(inv -> { dimensionChangeCount.incrementAndGet(); return null; }) |
| 1047 | + .when(driverWindow).setSize(any(Dimension.class)); |
| 1048 | + |
| 1049 | + when(mockedDriver.getCurrentUrl()).thenReturn("https://example.com"); |
| 1050 | + when(mockedDriver.findElements(By.tagName("iframe"))).thenReturn(Collections.emptyList()); |
| 1051 | + when(((JavascriptExecutor) mockedDriver).executeScript(any(String.class))).thenAnswer(invocation -> { |
| 1052 | + String script = invocation.getArgument(0); |
| 1053 | + if (script.equals("return window.resizeCount")) { |
| 1054 | + return (long) dimensionChangeCount.get(); |
| 1055 | + } |
| 1056 | + if (script.startsWith("return PercyDOM.serialize(")) { |
| 1057 | + Map<String, Object> snap = new HashMap<>(); |
| 1058 | + snap.put("dom", "test"); |
| 1059 | + return snap; |
| 1060 | + } |
| 1061 | + return null; |
| 1062 | + }); |
| 1063 | + |
| 1064 | + Map<String, Object> options = new HashMap<>(); |
| 1065 | + options.put("widths", Arrays.asList(1024)); |
| 1066 | + mockedPercy.captureResponsiveDom(mockedDriver, new HashSet<>(), options); |
| 1067 | + |
| 1068 | + // Only the final restore call should fire; no per-width resize. |
| 1069 | + ArgumentCaptor<Dimension> sizeCaptor = ArgumentCaptor.forClass(Dimension.class); |
| 1070 | + verify(driverWindow, times(1)).setSize(sizeCaptor.capture()); |
| 1071 | + Dimension restoreSize = sizeCaptor.getValue(); |
| 1072 | + assertEquals(1024, restoreSize.getWidth()); |
| 1073 | + assertEquals(768, restoreSize.getHeight()); |
| 1074 | + } finally { |
| 1075 | + setStaticField(Percy.class, "PERCY_SERVER_ADDRESS", originalAddress); |
| 1076 | + server.stop(0); |
| 1077 | + } |
| 1078 | + } |
| 1079 | + |
| 1080 | + @Test |
| 1081 | + public void captureResponsiveDomRefreshesDriverForEachWidthWhenReloadFlagSet() throws Exception { |
| 1082 | + HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); |
| 1083 | + server.createContext("/percy/widths-config", exchange -> { |
| 1084 | + byte[] body = "{\"widths\":[{\"width\":375},{\"width\":1280}]}".getBytes("UTF-8"); |
| 1085 | + exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, body.length); |
| 1086 | + try (OutputStream os = exchange.getResponseBody()) { os.write(body); } |
| 1087 | + }); |
| 1088 | + server.start(); |
| 1089 | + |
| 1090 | + String originalAddress = getStaticStringField(Percy.class, "PERCY_SERVER_ADDRESS"); |
| 1091 | + boolean originalReloadFlag = getStaticBooleanField(Percy.class, "PERCY_RESPONSIVE_CAPTURE_RELOAD_PAGE"); |
| 1092 | + try { |
| 1093 | + setStaticField(Percy.class, "PERCY_SERVER_ADDRESS", "http://localhost:" + server.getAddress().getPort()); |
| 1094 | + setStaticField(Percy.class, "PERCY_RESPONSIVE_CAPTURE_RELOAD_PAGE", true); |
| 1095 | + |
| 1096 | + RemoteWebDriver mockedDriver = mock(RemoteWebDriver.class); |
| 1097 | + Percy mockedPercy = spy(new Percy(mockedDriver)); |
| 1098 | + setField(mockedPercy, "domJs", "/* percy dom */"); |
| 1099 | + setField(mockedPercy, "cliConfig", new JSONObject().put("snapshot", new JSONObject())); |
| 1100 | + |
| 1101 | + WebDriver.Options driverOptions = mock(WebDriver.Options.class); |
| 1102 | + WebDriver.Window driverWindow = mock(WebDriver.Window.class); |
| 1103 | + WebDriver.Navigation navigation = mock(WebDriver.Navigation.class); |
| 1104 | + when(mockedDriver.manage()).thenReturn(driverOptions); |
| 1105 | + when(driverOptions.window()).thenReturn(driverWindow); |
| 1106 | + when(driverWindow.getSize()).thenReturn(new Dimension(1024, 768)); |
| 1107 | + when(mockedDriver.navigate()).thenReturn(navigation); |
| 1108 | + when(mockedDriver.getCurrentUrl()).thenReturn("https://example.com"); |
| 1109 | + when(mockedDriver.findElements(By.tagName("iframe"))).thenReturn(Collections.emptyList()); |
| 1110 | + |
| 1111 | + // After each reload resizeCount resets to 0, so the next changeWindowDimensionAndWait |
| 1112 | + // call uses resizeCount=1. Return 1L so the wait resolves without timing out. |
| 1113 | + when(((JavascriptExecutor) mockedDriver).executeScript(any(String.class))).thenAnswer(invocation -> { |
| 1114 | + String script = invocation.getArgument(0); |
| 1115 | + if (script.equals("return window.resizeCount")) { |
| 1116 | + return 1L; |
| 1117 | + } |
| 1118 | + if (script.startsWith("return PercyDOM.serialize(")) { |
| 1119 | + Map<String, Object> snap = new HashMap<>(); |
| 1120 | + snap.put("dom", "test"); |
| 1121 | + return snap; |
| 1122 | + } |
| 1123 | + return null; |
| 1124 | + }); |
| 1125 | + |
| 1126 | + Map<String, Object> options = new HashMap<>(); |
| 1127 | + options.put("widths", Arrays.asList(375, 1280)); |
| 1128 | + mockedPercy.captureResponsiveDom(mockedDriver, new HashSet<>(), options); |
| 1129 | + |
| 1130 | + // driver.navigate().refresh() must be called once per captured width. |
| 1131 | + verify(navigation, times(2)).refresh(); |
| 1132 | + } finally { |
| 1133 | + setStaticField(Percy.class, "PERCY_SERVER_ADDRESS", originalAddress); |
| 1134 | + setStaticField(Percy.class, "PERCY_RESPONSIVE_CAPTURE_RELOAD_PAGE", originalReloadFlag); |
| 1135 | + server.stop(0); |
| 1136 | + } |
| 1137 | + } |
| 1138 | + |
941 | 1139 | private static Object invokePrivate(Object target, String methodName, Class<?>[] paramTypes, Object... args) |
942 | 1140 | throws Exception { |
943 | 1141 | Method method = Percy.class.getDeclaredMethod(methodName, paramTypes); |
|
0 commit comments