|
| 1 | +package io.github.bonigarcia.seljup; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.TestInfo; |
| 4 | +import org.junit.jupiter.api.TestTemplate; |
| 5 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 6 | +import org.junit.jupiter.api.extension.TestTemplateInvocationContext; |
| 7 | +import org.junit.platform.commons.util.Preconditions; |
| 8 | + |
| 9 | +import java.lang.annotation.ElementType; |
| 10 | +import java.lang.annotation.Retention; |
| 11 | +import java.lang.annotation.RetentionPolicy; |
| 12 | +import java.lang.annotation.Target; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.function.Function; |
| 15 | +import java.util.function.UnaryOperator; |
| 16 | + |
| 17 | +/** |
| 18 | + * The annotated method is a <em>test template</em> that should be repeated for each |
| 19 | + * <a href="https://bonigarcia.dev/selenium-jupiter/#template-tests">browser scenario</a> |
| 20 | + * with a configurable {@linkplain #name display name}. |
| 21 | + * |
| 22 | + * @see org.junit.jupiter.api.TestTemplate |
| 23 | + * |
| 24 | + * @see org.junit.jupiter.api.RepeatedTest |
| 25 | + */ |
| 26 | +@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) |
| 27 | +@Retention(RetentionPolicy.RUNTIME) |
| 28 | +@TestTemplate |
| 29 | +public @interface BrowserScenarioTest { |
| 30 | + |
| 31 | + /** |
| 32 | + * Placeholder for the {@linkplain TestInfo#getDisplayName display name} of |
| 33 | + * a {@code @BrowserScenarioTest} method: <code>{displayName}</code> |
| 34 | + */ |
| 35 | + String DISPLAY_NAME_PLACEHOLDER = "{displayName}"; |
| 36 | + |
| 37 | + /** |
| 38 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getType() browser type} of |
| 39 | + * a {@code @BrowserScenarioTest} method: <code>{type}</code> |
| 40 | + */ |
| 41 | + String TYPE_PLACEHOLDER = "{type}"; |
| 42 | + |
| 43 | + /** |
| 44 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getVersion() browser version} of |
| 45 | + * a {@code @BrowserScenarioTest} method: <code>{version}</code> |
| 46 | + */ |
| 47 | + String VERSION_PLACEHOLDER = "{version}"; |
| 48 | + |
| 49 | + /** |
| 50 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getArguments() browser arguments} of |
| 51 | + * a {@code @BrowserScenarioTest} method: <code>{arguments}</code> |
| 52 | + */ |
| 53 | + String ARGUMENTS_PLACEHOLDER = "{arguments}"; |
| 54 | + |
| 55 | + /** |
| 56 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getPreferences() browser preferences} of |
| 57 | + * a {@code @BrowserScenarioTest} method: <code>{preferences}</code> |
| 58 | + */ |
| 59 | + String PREFERENCES_PLACEHOLDER = "{preferences}"; |
| 60 | + |
| 61 | + /** |
| 62 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getCapabilities() capabilities} of |
| 63 | + * a {@code @BrowserScenarioTest} method: <code>{capabilities}</code> |
| 64 | + */ |
| 65 | + String CAPABILITIES_PLACEHOLDER = "{capabilities}"; |
| 66 | + |
| 67 | + /** |
| 68 | + * Placeholder for the {@linkplain BrowsersTemplate.Browser#getRemoteUrl() remoteUrl} of |
| 69 | + * a {@code @BrowserScenarioTest} method: <code>{remoteUrl}</code> |
| 70 | + */ |
| 71 | + String REMOTE_URL_PLACEHOLDER = "{remoteUrl}"; |
| 72 | + |
| 73 | + /** |
| 74 | + * default display name pattern for a browser scenario test: {@value} |
| 75 | + */ |
| 76 | + String DEFAULT_NAME = "{displayName} - {type} {version}"; |
| 77 | + |
| 78 | + /** |
| 79 | + * The display name for each browser scenario test. |
| 80 | + * |
| 81 | + * <h4>Supported placeholders</h4> |
| 82 | + * <ul> |
| 83 | + * <li>{@link #DISPLAY_NAME_PLACEHOLDER}</li> |
| 84 | + * <li>{@link #TYPE_PLACEHOLDER}</li> |
| 85 | + * <li>{@link #VERSION_PLACEHOLDER}</li> |
| 86 | + * <li>{@link #ARGUMENTS_PLACEHOLDER}</li> |
| 87 | + * <li>{@link #PREFERENCES_PLACEHOLDER}</li> |
| 88 | + * <li>{@link #CAPABILITIES_PLACEHOLDER}</li> |
| 89 | + * <li>{@link #REMOTE_URL_PLACEHOLDER}</li> |
| 90 | + * </ul> |
| 91 | + * |
| 92 | + * <p>Defaults to {@link #DEFAULT_NAME}, resulting in |
| 93 | + * names such as {@code "chat button - chrome latest [--window-size=1280,720]"} |
| 94 | + * |
| 95 | + * <p>Alternatively, you can provide a custom display name, optionally |
| 96 | + * using the aforementioned placeholders. |
| 97 | + * |
| 98 | + * @return a custom display name; never blank or consisting solely of |
| 99 | + * whitespace |
| 100 | + */ |
| 101 | + String name() default DEFAULT_NAME; |
| 102 | + |
| 103 | + /** |
| 104 | + * A utility class for formatting the display name of a browser scenario test. |
| 105 | + */ |
| 106 | + final class NameFormatter { |
| 107 | + |
| 108 | + /** |
| 109 | + * The constructor is private to prevent instantiation. |
| 110 | + */ |
| 111 | + private NameFormatter() {} |
| 112 | + |
| 113 | + /** |
| 114 | + * Formats the display name of a browser scenario test. |
| 115 | + * |
| 116 | + * @param namePattern the name pattern from {@link BrowserScenarioTest#name()} |
| 117 | + * @param displayName the display name from {@link ExtensionContext#getDisplayName()} |
| 118 | + * @param browser the {@link BrowsersTemplate.Browser} from the {@link TestTemplateInvocationContext } |
| 119 | + * @return the formatted display name |
| 120 | + */ |
| 121 | + public static String format(String namePattern, String displayName, BrowsersTemplate.Browser browser) { |
| 122 | + Preconditions.notNull(namePattern, "namePattern must not be null"); |
| 123 | + Preconditions.notNull(displayName, "displayName must not be null"); |
| 124 | + Preconditions.notNull(browser, "browser must not be null"); |
| 125 | + String result = namePattern.trim(); |
| 126 | + Preconditions.notBlank(result, "@BrowserScenarioTest must be declared with a non-empty name."); |
| 127 | + result = replacePlaceholders(result, DISPLAY_NAME_PLACEHOLDER, displayName); |
| 128 | + result = replacePlaceholders(result, TYPE_PLACEHOLDER, browser.getType()); |
| 129 | + result = replacePlaceholders(result, VERSION_PLACEHOLDER, browser.getVersion()); |
| 130 | + result = replacePlaceholders(result, ARGUMENTS_PLACEHOLDER, browser.getArguments(), Arrays::toString); |
| 131 | + result = replacePlaceholders(result, PREFERENCES_PLACEHOLDER, browser.getPreferences(), Arrays::toString); |
| 132 | + result = replacePlaceholders(result, CAPABILITIES_PLACEHOLDER, browser.getCapabilities(), Object::toString); |
| 133 | + return replacePlaceholders(result, REMOTE_URL_PLACEHOLDER, browser.getRemoteUrl()); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Null-safe placeholder replacement in the name pattern with the given string value. If the value is null, |
| 138 | + * replacement is skipped. |
| 139 | + * |
| 140 | + * @param namePattern the (not-nullable) pattern for string replacements |
| 141 | + * @param placeholder the (not-nullable) placeholder to be replaced |
| 142 | + * @param value the (nullable) string value to replace with which to replace the placeholder |
| 143 | + * @return the (non-null) string pattern with placeholders replaced by the given value |
| 144 | + */ |
| 145 | + private static String replacePlaceholders(String namePattern, String placeholder, String value) { |
| 146 | + return replacePlaceholders(namePattern, placeholder, value, UnaryOperator.identity()); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Null-safe placeholder replacement in the name pattern with the given value transformed with a mapping |
| 151 | + * function. If the value is null, replacement is skipped. |
| 152 | + * |
| 153 | + * @param namePattern the (not-nullable) pattern for string replacements |
| 154 | + * @param placeholder the (not-nullable) placeholder to be replaced |
| 155 | + * @param value the (nullable) string value to replace with which to replace the placeholder |
| 156 | + * @param mapper a mapping function to transform the value into a suitable string |
| 157 | + * @return the (non-null) string pattern with placeholders replaced by the given value |
| 158 | + * @param <T> the generic type of the value |
| 159 | + */ |
| 160 | + private static <T> String replacePlaceholders(String namePattern, String placeholder, T value, Function<T, String> mapper) { |
| 161 | + if (value != null) { |
| 162 | + return namePattern.replace(placeholder, mapper.apply(value)); |
| 163 | + } else { |
| 164 | + return namePattern; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments