Skip to content

Commit a6a2b44

Browse files
committed
[visual-testing] Simplify conversion of ExamplesTable to screenshot configuration
1 parent 4c6fde4 commit a6a2b44

File tree

21 files changed

+170
-378
lines changed

21 files changed

+170
-378
lines changed

vividus-extension-selenium/src/main/java/org/vividus/ui/converter/AbstractExamplesTableToScreenshotConfigurationConverter.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

vividus-extension-selenium/src/main/java/org/vividus/ui/screenshot/AbstractScreenshotParametersFactory.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2024 the original author or authors.
2+
* Copyright 2019-2026 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,17 +16,22 @@
1616

1717
package org.vividus.ui.screenshot;
1818

19+
import static org.apache.commons.lang3.Validate.isTrue;
20+
21+
import java.lang.reflect.ParameterizedType;
1922
import java.util.Collection;
2023
import java.util.EnumMap;
2124
import java.util.LinkedHashSet;
25+
import java.util.List;
2226
import java.util.Map;
2327
import java.util.Optional;
2428
import java.util.Set;
2529
import java.util.function.BinaryOperator;
2630
import java.util.stream.Collectors;
2731
import java.util.stream.Stream;
2832

29-
import org.apache.commons.lang3.Validate;
33+
import org.jbehave.core.model.ExamplesTable;
34+
import org.jspecify.annotations.Nullable;
3035
import org.slf4j.Logger;
3136
import org.slf4j.LoggerFactory;
3237
import org.vividus.selenium.locator.Locator;
@@ -49,22 +54,36 @@ public Optional<ScreenshotParameters> create()
4954
}
5055

5156
@Override
52-
public P create(Optional<C> screenshotConfiguration, String sourceKey, Map<IgnoreStrategy, Set<Locator>> ignores)
57+
public P create(@Nullable ExamplesTable screenshotConfiguration, String sourceKey,
58+
Map<IgnoreStrategy, Set<Locator>> ignores)
5359
{
5460
Optional<C> defaultConfiguration = getDefaultConfiguration();
5561

56-
return screenshotConfiguration.map(currentConfig ->
62+
if (screenshotConfiguration == null)
5763
{
58-
patchIgnores(sourceKey, currentConfig, ignores);
59-
60-
C configuration = defaultConfiguration
61-
.map(defaultConfig -> getConfigurationMerger().apply(currentConfig, defaultConfig))
62-
.orElse(currentConfig);
63-
return createParameters(configuration);
64-
}).orElseGet(() -> {
6564
C configuration = defaultConfiguration.orElseGet(this::createScreenshotConfiguration);
6665
return createParameters(configuration, ignores);
67-
});
66+
}
67+
List<C> screenshotConfigurations = screenshotConfiguration.getRowsAs(getScreenshotConfigurationClass());
68+
int rowNumber = screenshotConfigurations.size();
69+
isTrue(rowNumber == 1,
70+
"Exactly one row is expected in ExamplesTable in order to convert it to screenshot configuration, "
71+
+ "but found %d row(s)",
72+
rowNumber);
73+
C currentConfig = screenshotConfigurations.get(0);
74+
patchIgnores(sourceKey, currentConfig, ignores);
75+
76+
C configuration = defaultConfiguration
77+
.map(defaultConfig -> getConfigurationMerger().apply(currentConfig, defaultConfig))
78+
.orElse(currentConfig);
79+
return createParameters(configuration);
80+
}
81+
82+
public Class<C> getScreenshotConfigurationClass()
83+
{
84+
ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
85+
//noinspection unchecked
86+
return (Class<C>) parameterizedType.getActualTypeArguments()[0];
6887
}
6988

7089
protected abstract C createScreenshotConfiguration();
@@ -91,7 +110,7 @@ private Set<Locator> getIgnoresFromOneOf(Set<Locator> configIgnores, String sour
91110
{
92111
if (!source.isEmpty())
93112
{
94-
Validate.isTrue(configIgnores.isEmpty(), "The elements and areas to ignore must be passed "
113+
isTrue(configIgnores.isEmpty(), "The elements and areas to ignore must be passed "
95114
+ "either through screenshot configuration or %s", sourceKey);
96115
LOGGER.atWarn().addArgument(sourceKey).log("The passing of elements and areas to ignore through {}"
97116
+ " is deprecated, please use screenshot configuration instead");
@@ -143,7 +162,7 @@ private P createParameters(C configuration, Map<IgnoreStrategy, Set<Locator>> st
143162

144163
protected int ensureValidCutSize(int value, String key)
145164
{
146-
Validate.isTrue(value >= 0, "The %s to cut must be greater than or equal to zero", key);
165+
isTrue(value >= 0, "The %s to cut must be greater than or equal to zero", key);
147166
return value;
148167
}
149168

vividus-extension-selenium/src/main/java/org/vividus/ui/screenshot/ScreenshotParametersFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 the original author or authors.
2+
* Copyright 2019-2026 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,13 +20,15 @@
2020
import java.util.Optional;
2121
import java.util.Set;
2222

23+
import org.jbehave.core.model.ExamplesTable;
24+
import org.jspecify.annotations.Nullable;
2325
import org.vividus.selenium.locator.Locator;
2426
import org.vividus.selenium.screenshot.IgnoreStrategy;
2527

2628
public interface ScreenshotParametersFactory<C extends ScreenshotConfiguration>
2729
{
2830
Optional<ScreenshotParameters> create();
2931

30-
ScreenshotParameters create(Optional<C> screenshotConfiguration, String sourceKey,
32+
ScreenshotParameters create(@Nullable ExamplesTable screenshotConfiguration, String sourceKey,
3133
Map<IgnoreStrategy, Set<Locator>> ignores);
3234
}

vividus-extension-selenium/src/test/java/org/vividus/ui/converter/AbstractExamplesTableToScreenshotConfigurationConverterTests.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

vividus-extension-selenium/src/test/java/org/vividus/ui/screenshot/AbstractScreenshotParametersFactoryTests.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 the original author or authors.
2+
* Copyright 2019-2026 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import static org.junit.jupiter.api.Assertions.assertThrows;
2424
import static org.junit.jupiter.api.Assertions.assertTrue;
2525
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.when;
2627

2728
import java.util.HashMap;
2829
import java.util.List;
@@ -36,6 +37,7 @@
3637
import com.github.valfirst.slf4jtest.TestLoggerFactory;
3738
import com.github.valfirst.slf4jtest.TestLoggerFactoryExtension;
3839

40+
import org.jbehave.core.model.ExamplesTable;
3941
import org.junit.jupiter.api.Test;
4042
import org.junit.jupiter.api.extension.ExtendWith;
4143
import org.vividus.selenium.locator.Locator;
@@ -64,7 +66,7 @@ void shouldUseDefaultConfiguration()
6466
factory.setShootingStrategy(DEFAULT);
6567
factory.setIgnoreStrategies(createEmptyIgnores());
6668

67-
var parameters = factory.create(Optional.empty(), null, createEmptyIgnores());
69+
var parameters = factory.create(null, null, createEmptyIgnores());
6870
assertEquals(cutBottom, parameters.getCutBottom());
6971
assertEquals(1, parameters.getCutTop());
7072
assertEquals(2, parameters.getCutLeft());
@@ -79,22 +81,30 @@ void shouldMergeUserDefinedAndDefaultConfigurations()
7981
factory.setShootingStrategy(DEFAULT);
8082
factory.setIgnoreStrategies(createEmptyIgnores());
8183

82-
var parameters = factory.create(Optional.of(createConfiguration(10)), null, createEmptyIgnores());
84+
var screenshotConfiguration = new ExamplesTable("""
85+
| cutTop | cutBottom | cutLeft | cutRight |
86+
| 1 | 10 | 2 | 3 |);
87+
""");
88+
89+
var parameters = factory.create(screenshotConfiguration, null, createEmptyIgnores());
8390
assertEquals(15, parameters.getCutBottom());
8491
}
8592

8693
@Test
8794
void shouldReturnCustomConfigurationWhenDefaultIsMissing()
8895
{
89-
var cutBottom = 5;
96+
var screenshotConfiguration = new ExamplesTable("""
97+
| cutTop | cutBottom | cutLeft | cutRight |
98+
| 1 | 5 | 2 | 3 |);
99+
""");
90100

91101
factory.setScreenshotConfigurations(new PropertyMappedCollection<>(Map.of()));
92102
factory.setShootingStrategy(DEFAULT);
93103
factory.setIgnoreStrategies(createEmptyIgnores());
94104

95-
var parameters = factory.create(Optional.of(createConfiguration(cutBottom)), null,
105+
var parameters = factory.create(screenshotConfiguration, null,
96106
createEmptyIgnores());
97-
assertEquals(cutBottom, parameters.getCutBottom());
107+
assertEquals(5, parameters.getCutBottom());
98108
assertEquals(1, parameters.getCutTop());
99109
}
100110

@@ -117,6 +127,9 @@ void shouldCreateParametersFromUserDefinedConfiguration()
117127
configuration.setShootingStrategy(Optional.of(DEFAULT));
118128
configuration.setCutBottom(1);
119129

130+
ExamplesTable examplesTable = mock();
131+
when(examplesTable.getRowsAs(ScreenshotConfiguration.class)).thenReturn(List.of(configuration));
132+
120133
var globalElementLocator = mock(Locator.class);
121134
var globalAreaLocator = mock(Locator.class);
122135

@@ -126,7 +139,7 @@ void shouldCreateParametersFromUserDefinedConfiguration()
126139
IgnoreStrategy.AREA, Set.of(globalAreaLocator)
127140
));
128141

129-
ScreenshotParameters parameters = factory.create(Optional.of(configuration), null, createEmptyIgnores());
142+
ScreenshotParameters parameters = factory.create(examplesTable, null, createEmptyIgnores());
130143

131144
assertEquals(Optional.of(DEFAULT), parameters.getShootingStrategy());
132145
assertEquals(Map.of(
@@ -153,9 +166,10 @@ void shouldFailIfBothSourcesAreNotEmpty()
153166
IgnoreStrategy.AREA, Set.of(locator),
154167
IgnoreStrategy.ELEMENT, Set.of(locator)
155168
);
156-
var configuration = Optional.of(screenshotConfiguration);
169+
ExamplesTable examplesTable = mock();
170+
when(examplesTable.getRowsAs(ScreenshotConfiguration.class)).thenReturn(List.of(screenshotConfiguration));
157171
var thrown = assertThrows(IllegalArgumentException.class,
158-
() -> factory.create(configuration, IGNORES_TABLE, ignores));
172+
() -> factory.create(examplesTable, IGNORES_TABLE, ignores));
159173
assertEquals("The elements and areas to ignore must be passed either through screenshot configuration"
160174
+ " or ignores table", thrown.getMessage());
161175
}
@@ -165,6 +179,8 @@ void shouldPatchIgnores()
165179
{
166180
var locator = mock(Locator.class);
167181
var screenshotConfiguration = new ScreenshotConfiguration();
182+
ExamplesTable examplesTable = mock();
183+
when(examplesTable.getRowsAs(ScreenshotConfiguration.class)).thenReturn(List.of(screenshotConfiguration));
168184

169185
factory.setScreenshotConfigurations(new PropertyMappedCollection<>(new HashMap<>()));
170186
factory.setIgnoreStrategies(createEmptyIgnores());
@@ -174,7 +190,7 @@ void shouldPatchIgnores()
174190
IgnoreStrategy.ELEMENT, Set.of(locator)
175191
);
176192

177-
ScreenshotParameters parameters = factory.create(Optional.of(screenshotConfiguration), IGNORES_TABLE, ignores);
193+
ScreenshotParameters parameters = factory.create(examplesTable, IGNORES_TABLE, ignores);
178194

179195
assertEquals(ignores, parameters.getIgnoreStrategies());
180196
assertThat(testLogger.getLoggingEvents(), equalTo(List.of(WARNING_MESSAGE, WARNING_MESSAGE)));

vividus-plugin-applitools/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ project.description = 'VIVIDUS plugin for Applitools'
33
dependencies {
44
implementation project(':vividus-engine')
55
implementation project(':vividus-extension-visual-testing')
6+
implementation project(':vividus-extension-selenium')
67
implementation project(':vividus-http-client')
7-
implementation project(':vividus-plugin-web-app')
88
implementation project(':vividus-reporter')
99
implementation project(':vividus-soft-assert')
1010
implementation project(':vividus-util')
@@ -20,4 +20,5 @@ dependencies {
2020
testImplementation platform(libs.mockito.bom)
2121
testImplementation(libs.mockito.junitJupiter)
2222
testImplementation(libs.slf4jTest)
23+
testImplementation project(':vividus-plugin-web-app')
2324
}

0 commit comments

Comments
 (0)