12
12
13
13
import java .io .File ;
14
14
import java .io .IOException ;
15
+ import java .util .regex .Pattern ;
15
16
16
17
public class ScreenshotUtils {
17
18
18
19
private final String methodName ;
19
20
private final String className ;
20
21
private final CustomScreencaptureProcessor customProcesssor ;
22
+ private final Pattern SCREENSHOT_NAME_VALIDATION = Pattern .compile ("[a-zA-Z0-9_-]+" );
21
23
22
24
ScreenshotUtils () {
23
25
StackTraceElement testClass = findTestClassTraceElement (Thread .currentThread ().getStackTrace ());
@@ -27,14 +29,19 @@ public class ScreenshotUtils {
27
29
}
28
30
29
31
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
+ }
38
45
}
39
46
}
40
47
0 commit comments