Skip to content

Commit fbc3767

Browse files
committed
chore: throw validation error for screenshotName
1 parent 4773611 commit fbc3767

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

app/src/androidTest/java/com/sample/browserstack/samplecalculator/ScreenshotUtils.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212

1313
import java.io.File;
1414
import java.io.IOException;
15+
import java.util.regex.Pattern;
1516

1617
public class ScreenshotUtils {
1718

1819
private final String methodName;
1920
private final String className;
2021
private final CustomScreencaptureProcessor customProcesssor;
22+
private final Pattern SCREENSHOT_NAME_VALIDATION = Pattern.compile("[a-zA-Z0-9_-]+");
2123

2224
ScreenshotUtils() {
2325
StackTraceElement testClass = findTestClassTraceElement(Thread.currentThread().getStackTrace());
@@ -27,14 +29,19 @@ public class ScreenshotUtils {
2729
}
2830

2931
public void captureScreenshot(String screenshotName) {
30-
ScreenCapture capture = Screenshot.capture();
31-
capture.setFormat(Bitmap.CompressFormat.PNG);
32-
capture.setName(screenshotName);
33-
34-
try {
35-
customProcesssor.process(capture);
36-
} catch (IOException e) {
37-
e.printStackTrace();
32+
// ScreenshotName failing to pass this regex matcher will be skipped.
33+
if (!SCREENSHOT_NAME_VALIDATION.matcher(screenshotName).matches()) {
34+
throw new IllegalArgumentException("ScreenshotName must match " + SCREENSHOT_NAME_VALIDATION.pattern() + ".");
35+
} else {
36+
ScreenCapture capture = Screenshot.capture();
37+
capture.setFormat(Bitmap.CompressFormat.PNG);
38+
capture.setName(screenshotName);
39+
40+
try {
41+
customProcesssor.process(capture);
42+
} catch (IOException e) {
43+
throw new RuntimeException("Unable to capture screenshot.", e);
44+
}
3845
}
3946
}
4047

0 commit comments

Comments
 (0)