FileNameFormatter { public String format(PersistMetadata metadata) { StringBuffer sb = new StringBuffer(); sb.append(metadata.getMethodName()).append("_").append(metadata.getScreenshotId()).append("_"); Matcher matcher = PLACEHOLDER_PATTERN.matcher(format); while (matcher.find()) { String key = matcher.group(1); String value; Function<PersistMetadata, String> func = PLACEHOLDER_FUNCTIONS.get(key); if (func == null) { Object object = metadata.getCapabilities().getCapability(key); value = object == null ? null : object.toString(); } else { value = func.apply(metadata); } matcher.appendReplacement(sb, Strings.nullToEmpty(value)); } matcher.appendTail(sb); int index = sb.lastIndexOf(".") - 1; while (sb.charAt(index) == '_') { sb.deleteCharAt(index--); } String s = UNDERSCORE_PATTERN.matcher(sb).replaceAll("_"); return FILE_NAME_ESCAPE_PATTERN.matcher(s).replaceAll("-"); } FileNameFormatter(String format); String format(PersistMetadata metadata);  }
@Test public void testFormat_selector() throws Exception { PtlCapabilities capabilities = new PtlCapabilities(new HashMap<String, Object>()); capabilities.setPlatform(Platform.WINDOWS); capabilities.setBrowserName("firefox"); capabilities.setVersion("38"); PersistMetadata metadata = new PersistMetadata("testId", "testClass", "testMethod", "scId", new IndexDomSelector(SelectorType.TAG_NAME, "body", 1), null, capabilities); FileNameFormatter formatter = new FileNameFormatter( "{platformName}_{platformVersion}_{browserName}_{version}_{screenArea}.png"); String result = formatter.format(metadata); assertThat(result, is("testMethod_scId_WINDOWS_firefox_38_TAG_NAME_body_[1].png")); }
@Test public void testFormat_rectangle() throws Exception { PtlCapabilities capabilities = new PtlCapabilities(new HashMap<String, Object>()); capabilities.setPlatform(Platform.WINDOWS); capabilities.setBrowserName("firefox"); capabilities.setVersion("38"); PersistMetadata metadata = new PersistMetadata("testId", "testClass", "testMethod", "scId", null, new RectangleArea(0, 10, 100, 1000), capabilities); FileNameFormatter formatter = new FileNameFormatter( "{platformName}_{platformVersion}_{browserName}_{version}_{screenArea}.png"); String result = formatter.format(metadata); assertThat(result, is("testMethod_scId_WINDOWS_firefox_38_rect_0_10_100_1000.png")); }
@Test public void testFormat_selendroid() throws Exception { PtlCapabilities capabilities = new PtlCapabilities(new HashMap<String, Object>()); capabilities.setPlatform(Platform.ANDROID); capabilities.setBrowserName(""); capabilities.setCapability("deviceName", "ASUS Pad"); capabilities.setCapability("platformVersion", "4.0.3"); capabilities.setCapability("automationName", "Selendroid"); PersistMetadata metadata = new PersistMetadata("testId", "testClass", "testMethod", "scId", new IndexDomSelector(SelectorType.TAG_NAME, "body", 1), null, capabilities); FileNameFormatter formatter = new FileNameFormatter( "{platformName}_{platformVersion}_{browserName}_{version}_{screenArea}.png"); String result = formatter.format(metadata); assertThat(result, is("testMethod_scId_ANDROID_4.0.3_Selendroid_TAG_NAME_body_[1].png")); }
@Test public void testFormat_escape() throws Exception { PtlCapabilities capabilities = new PtlCapabilities(new HashMap<String, Object>()); capabilities.setPlatform(Platform.WINDOWS); capabilities.setBrowserName("firefox"); capabilities.setVersion("38"); PersistMetadata metadata = new PersistMetadata("testId", "testClass", "testMethod", "scId", new IndexDomSelector(SelectorType.CSS_SELECTOR, "1\\\\2/3:4*5?6\"7<8>9|0", 1), null, capabilities); FileNameFormatter formatter = new FileNameFormatter( "{platformName}_{platformVersion}_{browserName}_{version}_{screenArea}.png"); String result = formatter.format(metadata); assertThat(result, is("testMethod_scId_WINDOWS_firefox_38_CSS_SELECTOR_1-2-3-4-5-6-7-8-9-0_[1].png")); }