Skip to content

Commit 732bb32

Browse files
Added type check for snapshot and screenshot (#249)
1 parent bbe6e85 commit 732bb32

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/main/java/io/percy/selenium/Percy.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public class Percy {
4646
// for logging
4747
private static String LABEL = "[\u001b[35m" + (PERCY_DEBUG ? "percy:java" : "percy") + "\u001b[39m]";
4848

49+
// Type of session automate/web
50+
protected String sessionType = null;
51+
4952
// Is the Percy server running or not
5053
private boolean isPercyEnabled = healthcheck();
5154

@@ -154,6 +157,7 @@ public void snapshot(String name, @Nullable List<Integer> widths, Integer minHei
154157

155158
public void snapshot(String name, Map<String, Object> options) {
156159
if (!isPercyEnabled) { return; }
160+
if ("automate".equals(sessionType)) { throw new RuntimeException("Invalid function call - snapshot(). Please use screenshot() function while using Percy with Automate. For more information on usage of PercyScreenshot, refer https://docs.percy.io/docs/integrate-functional-testing-with-visual-testing"); }
157161

158162
Map<String, Object> domSnapshot = null;
159163

@@ -187,6 +191,8 @@ public void screenshot(String name) throws UnsupportedOperationException {
187191
*/
188192
public void screenshot(String name, Map<String, Object> options) throws UnsupportedOperationException {
189193
if (!isPercyEnabled) { return; }
194+
if ("web".equals(sessionType)) { throw new RuntimeException("Invalid function call - screenshot(). Please use snapshot() function for taking screenshot. screenshot() should be used only while using Percy with Automate. For more information on usage of snapshot(), refer doc for your language https://docs.percy.io/docs/end-to-end-testing"); }
195+
190196
List<String> driverArray = Arrays.asList(driver.getClass().toString().split("\\$")); // Added to handle testcase (mocked driver)
191197
Iterator<String> driverIterator = driverArray.iterator();
192198
String driverClass = driverIterator.next();
@@ -264,6 +270,10 @@ private boolean healthcheck() {
264270

265271
return false;
266272
}
273+
HttpEntity entity = response.getEntity();
274+
String responseString = EntityUtils.toString(entity, "UTF-8");
275+
JSONObject responseObject = new JSONObject(responseString);
276+
sessionType = (String) responseObject.optString("type", null);
267277

268278
return true;
269279
} catch (Exception ex) {

src/test/java/io/percy/selenium/SdkTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.junit.jupiter.api.AfterAll;
99
import org.junit.jupiter.api.AfterEach;
10+
import org.junit.jupiter.api.BeforeEach;
1011
import org.junit.jupiter.api.BeforeAll;
1112
import org.junit.jupiter.api.Test;
1213
import static org.junit.jupiter.api.Assertions.*;
@@ -48,6 +49,11 @@ public static void testTeardown() {
4849
TestServer.shutdown();
4950
}
5051

52+
@BeforeEach
53+
public void setSessionType() {
54+
percy.sessionType = "web";
55+
}
56+
5157
@Test
5258
public void takesLocalAppSnapshotWithProvidedName() {
5359
driver.get(TEST_URL);
@@ -123,6 +129,7 @@ public void takeScreenshot() {
123129
} catch (Exception e) {
124130
}
125131
percy = spy(new Percy(mockedDriver));
132+
percy.sessionType = "automate";
126133
when(mockedDriver.getSessionId()).thenReturn(new SessionId("123"));
127134
when(mockedDriver.getCommandExecutor()).thenReturn(commandExecutor);
128135
DesiredCapabilities capabilities = new DesiredCapabilities();
@@ -141,6 +148,7 @@ public void takeScreenshotWithOptions() {
141148
} catch (Exception e) {
142149
}
143150
percy = spy(new Percy(mockedDriver));
151+
percy.sessionType = "automate";
144152
when(mockedDriver.getSessionId()).thenReturn(new SessionId("123"));
145153
when(mockedDriver.getCommandExecutor()).thenReturn(commandExecutor);
146154
DesiredCapabilities capabilities = new DesiredCapabilities();
@@ -155,4 +163,19 @@ public void takeScreenshotWithOptions() {
155163
percy.screenshot("Test", options);
156164
verify(percy).request(eq("/percy/automateScreenshot"), any() , eq("Test"));
157165
}
166+
167+
@Test
168+
public void takeSnapshotThrowErrorForPOA() {
169+
percy.sessionType = "automate";
170+
Throwable exception = assertThrows(RuntimeException.class, () -> percy.snapshot("Test"));
171+
assertEquals("Invalid function call - snapshot(). Please use screenshot() function while using Percy with Automate. For more information on usage of PercyScreenshot, refer https://docs.percy.io/docs/integrate-functional-testing-with-visual-testing", exception.getMessage());
172+
}
173+
174+
@Test
175+
public void takeScreenshotThrowErrorForWeb() {
176+
RemoteWebDriver mockedDriver = mock(RemoteWebDriver.class);
177+
percy = spy(new Percy(mockedDriver));
178+
Throwable exception = assertThrows(RuntimeException.class, () -> percy.screenshot("Test"));
179+
assertEquals("Invalid function call - screenshot(). Please use snapshot() function for taking screenshot. screenshot() should be used only while using Percy with Automate. For more information on usage of snapshot(), refer doc for your language https://docs.percy.io/docs/end-to-end-testing", exception.getMessage());
180+
}
158181
}

0 commit comments

Comments
 (0)