Skip to content

Commit 02571b2

Browse files
committed
Include headers and format code
1 parent aff89ec commit 02571b2

File tree

2 files changed

+148
-93
lines changed

2 files changed

+148
-93
lines changed

src/main/java/io/github/bonigarcia/seljup/BrowserScenarioTest.java

Lines changed: 94 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* (C) Copyright 2025 Boni Garcia (https://bonigarcia.github.io/)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
117
package io.github.bonigarcia.seljup;
218

319
import org.junit.jupiter.api.TestInfo;
@@ -15,9 +31,10 @@
1531
import java.util.function.UnaryOperator;
1632

1733
/**
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}.
34+
* The annotated method is a <em>test template</em> that should be repeated for
35+
* each
36+
* <a href="https://bonigarcia.dev/selenium-jupiter/#template-tests">browser
37+
* scenario</a> with a configurable {@linkplain #name display name}.
2138
*
2239
* @see org.junit.jupiter.api.TestTemplate
2340
*
@@ -35,38 +52,44 @@
3552
String DISPLAY_NAME_PLACEHOLDER = "{displayName}";
3653

3754
/**
38-
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getType() browser type} of
39-
* a {@code @BrowserScenarioTest} method: <code>{type}</code>
55+
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getType()
56+
* browser type} of a {@code @BrowserScenarioTest} method:
57+
* <code>{type}</code>
4058
*/
4159
String TYPE_PLACEHOLDER = "{type}";
4260

4361
/**
44-
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getVersion() browser version} of
45-
* a {@code @BrowserScenarioTest} method: <code>{version}</code>
62+
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getVersion()
63+
* browser version} of a {@code @BrowserScenarioTest} method:
64+
* <code>{version}</code>
4665
*/
4766
String VERSION_PLACEHOLDER = "{version}";
4867

4968
/**
50-
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getArguments() browser arguments} of
51-
* a {@code @BrowserScenarioTest} method: <code>{arguments}</code>
69+
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getArguments()
70+
* browser arguments} of a {@code @BrowserScenarioTest} method:
71+
* <code>{arguments}</code>
5272
*/
5373
String ARGUMENTS_PLACEHOLDER = "{arguments}";
5474

5575
/**
56-
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getPreferences() browser preferences} of
57-
* a {@code @BrowserScenarioTest} method: <code>{preferences}</code>
76+
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getPreferences()
77+
* browser preferences} of a {@code @BrowserScenarioTest} method:
78+
* <code>{preferences}</code>
5879
*/
5980
String PREFERENCES_PLACEHOLDER = "{preferences}";
6081

6182
/**
62-
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getCapabilities() capabilities} of
63-
* a {@code @BrowserScenarioTest} method: <code>{capabilities}</code>
83+
* Placeholder for the
84+
* {@linkplain BrowsersTemplate.Browser#getCapabilities() capabilities} of a
85+
* {@code @BrowserScenarioTest} method: <code>{capabilities}</code>
6486
*/
6587
String CAPABILITIES_PLACEHOLDER = "{capabilities}";
6688

6789
/**
68-
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getRemoteUrl() remoteUrl} of
69-
* a {@code @BrowserScenarioTest} method: <code>{remoteUrl}</code>
90+
* Placeholder for the {@linkplain BrowsersTemplate.Browser#getRemoteUrl()
91+
* remoteUrl} of a {@code @BrowserScenarioTest} method:
92+
* <code>{remoteUrl}</code>
7093
*/
7194
String REMOTE_URL_PLACEHOLDER = "{remoteUrl}";
7295

@@ -89,75 +112,100 @@
89112
* <li>{@link #REMOTE_URL_PLACEHOLDER}</li>
90113
* </ul>
91114
*
92-
* <p>Defaults to {@link #DEFAULT_NAME}, resulting in
93-
* names such as {@code "chat button - chrome latest [--window-size=1280,720]"}
115+
* <p>
116+
* Defaults to {@link #DEFAULT_NAME}, resulting in names such as
117+
* {@code "chat button - chrome latest [--window-size=1280,720]"}
94118
*
95-
* <p>Alternatively, you can provide a custom display name, optionally
96-
* using the aforementioned placeholders.
119+
* <p>
120+
* Alternatively, you can provide a custom display name, optionally using
121+
* the aforementioned placeholders.
97122
*
98123
* @return a custom display name; never blank or consisting solely of
99-
* whitespace
124+
* whitespace
100125
*/
101126
String name() default DEFAULT_NAME;
102127

103128
/**
104-
* A utility class for formatting the display name of a browser scenario test.
129+
* A utility class for formatting the display name of a browser scenario
130+
* test.
105131
*/
106132
final class NameFormatter {
107133

108134
/**
109135
* The constructor is private to prevent instantiation.
110136
*/
111-
private NameFormatter() {}
137+
private NameFormatter() {
138+
}
112139

113140
/**
114141
* Formats the display name of a browser scenario test.
115142
*
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 }
143+
* @param namePattern the name pattern from
144+
* {@link BrowserScenarioTest#name()}
145+
* @param displayName the display name from
146+
* {@link ExtensionContext#getDisplayName()}
147+
* @param browser the {@link BrowsersTemplate.Browser} from the
148+
* {@link TestTemplateInvocationContext }
119149
* @return the formatted display name
120150
*/
121-
public static String format(String namePattern, String displayName, BrowsersTemplate.Browser browser) {
151+
public static String format(String namePattern, String displayName,
152+
BrowsersTemplate.Browser browser) {
122153
Preconditions.notNull(namePattern, "namePattern must not be null");
123154
Preconditions.notNull(displayName, "displayName must not be null");
124155
Preconditions.notNull(browser, "browser must not be null");
125156
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());
157+
Preconditions.notBlank(result,
158+
"@BrowserScenarioTest must be declared with a non-empty name.");
159+
result = replacePlaceholders(result, DISPLAY_NAME_PLACEHOLDER,
160+
displayName);
161+
result = replacePlaceholders(result, TYPE_PLACEHOLDER,
162+
browser.getType());
163+
result = replacePlaceholders(result, VERSION_PLACEHOLDER,
164+
browser.getVersion());
165+
result = replacePlaceholders(result, ARGUMENTS_PLACEHOLDER,
166+
browser.getArguments(), Arrays::toString);
167+
result = replacePlaceholders(result, PREFERENCES_PLACEHOLDER,
168+
browser.getPreferences(), Arrays::toString);
169+
result = replacePlaceholders(result, CAPABILITIES_PLACEHOLDER,
170+
browser.getCapabilities(), Object::toString);
171+
return replacePlaceholders(result, REMOTE_URL_PLACEHOLDER,
172+
browser.getRemoteUrl());
134173
}
135174

136175
/**
137-
* Null-safe placeholder replacement in the name pattern with the given string value. If the value is null,
138-
* replacement is skipped.
176+
* Null-safe placeholder replacement in the name pattern with the given
177+
* string value. If the value is null, replacement is skipped.
139178
*
140179
* @param namePattern the (not-nullable) pattern for string replacements
141180
* @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
181+
* @param value the (nullable) string value to replace with which
182+
* to replace the placeholder
183+
* @return the (non-null) string pattern with placeholders replaced by
184+
* the given value
144185
*/
145-
private static String replacePlaceholders(String namePattern, String placeholder, String value) {
146-
return replacePlaceholders(namePattern, placeholder, value, UnaryOperator.identity());
186+
private static String replacePlaceholders(String namePattern,
187+
String placeholder, String value) {
188+
return replacePlaceholders(namePattern, placeholder, value,
189+
UnaryOperator.identity());
147190
}
148191

149192
/**
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.
193+
* Null-safe placeholder replacement in the name pattern with the given
194+
* value transformed with a mapping function. If the value is null,
195+
* replacement is skipped.
152196
*
153197
* @param namePattern the (not-nullable) pattern for string replacements
154198
* @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
199+
* @param value the (nullable) string value to replace with which
200+
* to replace the placeholder
201+
* @param mapper a mapping function to transform the value into a
202+
* suitable string
203+
* @return the (non-null) string pattern with placeholders replaced by
204+
* the given value
158205
* @param <T> the generic type of the value
159206
*/
160-
private static <T> String replacePlaceholders(String namePattern, String placeholder, T value, Function<T, String> mapper) {
207+
private static <T> String replacePlaceholders(String namePattern,
208+
String placeholder, T value, Function<T, String> mapper) {
161209
if (value != null) {
162210
return namePattern.replace(placeholder, mapper.apply(value));
163211
} else {
Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1+
/*
2+
* (C) Copyright 2025 Boni Garcia (https://bonigarcia.github.io/)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
117
package io.github.bonigarcia.seljup.test.template;
218

19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.platform.commons.PreconditionViolationException;
24+
325
import com.google.gson.internal.LinkedTreeMap;
26+
427
import io.github.bonigarcia.seljup.BrowserBuilder;
528
import io.github.bonigarcia.seljup.BrowserScenarioTest;
629
import io.github.bonigarcia.seljup.BrowsersTemplate.Browser;
7-
import org.junit.jupiter.api.Test;
8-
import org.junit.platform.commons.PreconditionViolationException;
9-
10-
import static org.junit.jupiter.api.Assertions.assertEquals;
11-
import static org.junit.jupiter.api.Assertions.assertThrows;
1230

1331
class BrowserScenarioFormattingTest {
1432

@@ -20,76 +38,65 @@ void replaceAllPlaceholders() {
2038
capabilities.put("custom:cap-3", "true");
2139
Browser browser = new BrowserBuilder("chrome-in-docker")
2240
.version("latest")
23-
.arguments(new String[]{"--disable-gpu", "--no-sandbox"})
24-
.preferences(new String[]{"media.navigator.permission.disabled=true", "media.navigator.streams.fake=true"})
25-
.capabilities(capabilities)
26-
.remoteUrl("http://localhost:4444/")
41+
.arguments(new String[] { "--disable-gpu", "--no-sandbox" })
42+
.preferences(new String[] {
43+
"media.navigator.permission.disabled=true",
44+
"media.navigator.streams.fake=true" })
45+
.capabilities(capabilities).remoteUrl("http://localhost:4444/")
2746
.build();
2847
String result = BrowserScenarioTest.NameFormatter.format(
2948
"{displayName}, {type}, {version}, {arguments}, {preferences}, {capabilities}, {remoteUrl}",
30-
"Sample Test",
31-
browser
32-
);
33-
assertEquals("""
34-
Sample Test, chrome-in-docker, latest, [--disable-gpu, --no-sandbox], \
35-
[media.navigator.permission.disabled=true, media.navigator.streams.fake=true], \
36-
{custom:cap-1=value-1, custom:cap-2=custom-value-2, custom:cap-3=true}, http://localhost:4444/""", result);
49+
"Sample Test", browser);
50+
assertEquals(
51+
"""
52+
Sample Test, chrome-in-docker, latest, [--disable-gpu, --no-sandbox], \
53+
[media.navigator.permission.disabled=true, media.navigator.streams.fake=true], \
54+
{custom:cap-1=value-1, custom:cap-2=custom-value-2, custom:cap-3=true}, http://localhost:4444/""",
55+
result);
3756
}
3857

3958
@Test
4059
void skipNullBrowserAttributes() {
41-
Browser browser = new BrowserBuilder(null)
42-
.version(null)
43-
.arguments(null)
44-
.preferences(null)
45-
.capabilities(null)
46-
.remoteUrl(null)
47-
.build();
60+
Browser browser = new BrowserBuilder(null).version(null).arguments(null)
61+
.preferences(null).capabilities(null).remoteUrl(null).build();
4862
String result = BrowserScenarioTest.NameFormatter.format(
4963
"{displayName}, {type}, {version}, {arguments}, {preferences}, {capabilities}, {remoteUrl}",
50-
"Sample Test",
51-
browser
52-
);
53-
assertEquals("Sample Test, {type}, {version}, {arguments}, {preferences}, {capabilities}, {remoteUrl}", result);
64+
"Sample Test", browser);
65+
assertEquals(
66+
"Sample Test, {type}, {version}, {arguments}, {preferences}, {capabilities}, {remoteUrl}",
67+
result);
5468
}
5569

5670
@Test
5771
void throwExceptionWhenNamePatternIsNull() {
5872
Browser browser = new BrowserBuilder("chrome").build();
59-
assertThrows(PreconditionViolationException.class, () -> BrowserScenarioTest.NameFormatter.format(
60-
null,
61-
"Sample Test",
62-
browser
63-
));
73+
assertThrows(PreconditionViolationException.class,
74+
() -> BrowserScenarioTest.NameFormatter.format(null,
75+
"Sample Test", browser));
6476
}
6577

6678
@Test
6779
void throwExceptionWhenDisplayNameIsNull() {
6880
Browser browser = new BrowserBuilder("chrome").build();
69-
assertThrows(PreconditionViolationException.class, () -> BrowserScenarioTest.NameFormatter.format(
70-
"{displayName} - {type} {version}",
71-
null,
72-
browser
73-
));
81+
assertThrows(PreconditionViolationException.class,
82+
() -> BrowserScenarioTest.NameFormatter.format(
83+
"{displayName} - {type} {version}", null, browser));
7484
}
7585

7686
@Test
7787
void throwExceptionWhenBrowserIsNull() {
78-
assertThrows(PreconditionViolationException.class, () -> BrowserScenarioTest.NameFormatter.format(
79-
"{displayName} - {type} {version}",
80-
"Sample Test",
81-
null
82-
));
88+
assertThrows(PreconditionViolationException.class,
89+
() -> BrowserScenarioTest.NameFormatter.format(
90+
"{displayName} - {type} {version}", "Sample Test",
91+
null));
8392
}
8493

8594
@Test
8695
void throwExceptionWhenNamePatternIsEmpty() {
8796
Browser browser = new BrowserBuilder("chrome").build();
88-
assertThrows(PreconditionViolationException.class, () -> BrowserScenarioTest.NameFormatter.format(
89-
" ",
90-
"Sample Test",
91-
browser
92-
));
97+
assertThrows(PreconditionViolationException.class,
98+
() -> BrowserScenarioTest.NameFormatter.format(" ",
99+
"Sample Test", browser));
93100
}
94101

95102
}

0 commit comments

Comments
 (0)