Skip to content

Commit 8fa473a

Browse files
committed
Update 1
1 parent c36fc43 commit 8fa473a

File tree

13 files changed

+392
-271
lines changed

13 files changed

+392
-271
lines changed

docs/modules/plugins/partials/ui-element-css-property-steps.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ The context can be set by the <<_change_context,corresponding steps>>.
2828

2929
[source,gherkin]
3030
----
31-
Then context element has CSS properties:$parameters
31+
Then context element has CSS properties matching rules:$parameters
3232
----
3333

3434
* `$parameters` - The xref:ROOT:glossary.adoc#_examplestable[ExamplesTable] containing the following columns:
35-
** [subs=+quotes]`*cssName*` - A name of the CSS property.
35+
** [subs=+quotes]`*cssProperty*` - A name of the CSS property.
3636
** [subs=+quotes]`*comparisonRule*` - CSS property xref:parameters:string-comparison-rule.adoc[comparison rule].
3737
** [subs=+quotes]`*expectedValue*` - The expected value of the CSS property.
3838

3939
.Change context to element and verify several CSS properties
4040
[source,gherkin]
4141
----
4242
When I change context to element located by `id(block)`
43-
Then context element has CSS properties:
44-
|cssName |comparisonRule |expectedValue |
43+
Then context element has CSS properties matching rules:
44+
|cssProperty |comparisonRule |expectedValue |
4545
|align-items |is equal to |center |
4646
|border |contains |solid |
4747
|font-size |contains |20 |
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2019-2024 the original author or authors.
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+
* https://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+
17+
package org.vividus.steps.ui.web;
18+
19+
import org.jbehave.core.annotations.AsParameters;
20+
import org.vividus.steps.StringComparisonRule;
21+
22+
@AsParameters
23+
public class CssValidationParameters
24+
{
25+
private String cssProperty;
26+
private StringComparisonRule comparisonRule;
27+
private String expectedValue;
28+
29+
public CssValidationParameters()
30+
{
31+
}
32+
33+
public CssValidationParameters(String cssProperty, StringComparisonRule comparisonRule, String expectedValue)
34+
{
35+
this.cssProperty = cssProperty;
36+
this.comparisonRule = comparisonRule;
37+
this.expectedValue = expectedValue;
38+
}
39+
40+
public String getCssProperty()
41+
{
42+
return cssProperty;
43+
}
44+
45+
public void setCssProperty(String cssProperty)
46+
{
47+
this.cssProperty = cssProperty;
48+
}
49+
50+
public StringComparisonRule getComparisonRule()
51+
{
52+
return comparisonRule;
53+
}
54+
55+
public void setComparisonRule(StringComparisonRule comparisonRule)
56+
{
57+
this.comparisonRule = comparisonRule;
58+
}
59+
60+
public String getExpectedValue()
61+
{
62+
return expectedValue;
63+
}
64+
65+
public void setExpectedValue(String expectedValue)
66+
{
67+
this.expectedValue = expectedValue;
68+
}
69+
}

vividus-extension-web-app/src/main/java/org/vividus/steps/ui/web/CssValidationResult.java

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,27 @@
1818

1919
import org.vividus.steps.StringComparisonRule;
2020

21-
public class CssValidationResult
21+
public class CssValidationResult extends CssValidationParameters
2222
{
23-
private String cssName;
24-
private String cssActualValue;
25-
private StringComparisonRule comparisonRule;
26-
private String cssExpectedValue;
23+
private String actualValue;
2724
private boolean passed;
2825

29-
public CssValidationResult(String cssName, String cssActualValue, StringComparisonRule comparisonRule,
26+
public CssValidationResult(String cssName, String actualValue, StringComparisonRule comparisonRule,
3027
String cssExpectedValue, boolean passed)
3128
{
32-
this.cssName = cssName;
33-
this.cssActualValue = cssActualValue;
34-
this.comparisonRule = comparisonRule;
35-
this.cssExpectedValue = cssExpectedValue;
29+
super(cssName, comparisonRule, cssExpectedValue);
30+
this.actualValue = actualValue;
3631
this.passed = passed;
3732
}
3833

39-
public String getCssName()
34+
public String getActualValue()
4035
{
41-
return cssName;
36+
return actualValue;
4237
}
4338

44-
public void setCssName(String cssName)
39+
public void setActualValue(String actualValue)
4540
{
46-
this.cssName = cssName;
47-
}
48-
49-
public String getCssActualValue()
50-
{
51-
return cssActualValue;
52-
}
53-
54-
public void setCssActualValue(String cssActualValue)
55-
{
56-
this.cssActualValue = cssActualValue;
57-
}
58-
59-
public StringComparisonRule getComparisonRule()
60-
{
61-
return comparisonRule;
62-
}
63-
64-
public void setComparisonRule(StringComparisonRule comparisonRule)
65-
{
66-
this.comparisonRule = comparisonRule;
67-
}
68-
69-
public String getCssExpectedValue()
70-
{
71-
return cssExpectedValue;
72-
}
73-
74-
public void setCssExpectedValue(String cssExpectedValue)
75-
{
76-
this.cssExpectedValue = cssExpectedValue;
41+
this.actualValue = actualValue;
7742
}
7843

7944
public boolean isPassed()

vividus-extension-web-app/src/main/resources/templates/css_validation_result.ftl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta charset="utf-8">
77
<title>CSS properties validation results table</title>
88
<link rel="stylesheet" href="../../styles.css"/>
9-
<link rel="stylesheet" href="../../webjars/bootstrap/3.4.1/css/bootstrap.min.css"/>
9+
<link rel="stylesheet" href="../../webjars/bootstrap/5.3.3/css/bootstrap.min.css"/>
1010
</head>
1111
<body>
1212
<style>
@@ -59,10 +59,10 @@
5959
<tbody>
6060
<#list cssResults as cssResult>
6161
<tr class="${cssResult.passed?string('pass', 'fail')}">
62-
<td>${cssResult.cssName}</td>
63-
<td>${cssResult.cssActualValue!""}</td>
62+
<td>${cssResult.cssProperty}</td>
63+
<td>${cssResult.actualValue!""}</td>
6464
<td>${cssResult.comparisonRule}</td>
65-
<td>${cssResult.cssExpectedValue}</td>
65+
<td>${cssResult.expectedValue}</td>
6666
<td>
6767
<span class="icon ${cssResult.passed?string('pass', 'fail')}">
6868
${cssResult.passed?string('✔', '✖')}
@@ -72,6 +72,6 @@
7272
</#list>
7373
</tbody>
7474
</table>
75-
<script src="../../webjars/bootstrap/3.4.1/js/bootstrap.min.js"></script>
75+
<script src="../../webjars/bootstrap/5.3.3/js/bootstrap.min.js"></script>
7676
</body>
7777
</html>

vividus-extension-web-app/src/test/java/org/vividus/steps/ui/web/CssValidationResultTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ void testToCoverage()
3232
final String newExpected = "new_expected";
3333
CssValidationResult result = new CssValidationResult("key", "value", StringComparisonRule.CONTAINS,
3434
"expected", true);
35-
result.setCssName(newKey);
36-
result.setCssActualValue(newValue);
35+
result.setCssProperty(newKey);
36+
result.setActualValue(newValue);
3737
result.setComparisonRule(StringComparisonRule.IS_EQUAL_TO);
38-
result.setCssExpectedValue(newExpected);
38+
result.setExpectedValue(newExpected);
3939
result.setPassed(false);
40-
assertEquals(newKey, result.getCssName());
41-
assertEquals(newValue, result.getCssActualValue());
40+
assertEquals(newKey, result.getCssProperty());
41+
assertEquals(newValue, result.getActualValue());
4242
assertEquals(StringComparisonRule.IS_EQUAL_TO, result.getComparisonRule());
43-
assertEquals(newExpected, result.getCssExpectedValue());
43+
assertEquals(newExpected, result.getExpectedValue());
4444
assertFalse(result.isPassed());
4545
}
4646
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2019-2024 the original author or authors.
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+
* https://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+
17+
package org.vividus.ui.web.playwright.steps;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.Optional;
23+
24+
import com.microsoft.playwright.Locator;
25+
26+
import org.apache.commons.lang3.StringUtils;
27+
import org.apache.commons.text.CaseUtils;
28+
import org.hamcrest.Matcher;
29+
import org.jbehave.core.annotations.Then;
30+
import org.vividus.reporter.event.IAttachmentPublisher;
31+
import org.vividus.softassert.ISoftAssert;
32+
import org.vividus.steps.StringComparisonRule;
33+
import org.vividus.steps.ui.web.CssValidationParameters;
34+
import org.vividus.steps.ui.web.CssValidationResult;
35+
import org.vividus.ui.web.action.JavascriptActions;
36+
import org.vividus.ui.web.playwright.UiContext;
37+
import org.vividus.ui.web.playwright.action.ElementActions;
38+
39+
public class ElementCssSteps
40+
{
41+
private static final String ELEMENT_CSS_CONTAINING_VALUE = "Element has CSS property '%s' containing value '%s'";
42+
43+
private final UiContext uiContext;
44+
private final ISoftAssert softAssert;
45+
private final ElementActions elementActions;
46+
private final JavascriptActions javascriptActions;
47+
private final IAttachmentPublisher attachmentPublisher;
48+
49+
public ElementCssSteps(UiContext uiContext, ISoftAssert softAssert, ElementActions elementActions,
50+
JavascriptActions javascriptActions, IAttachmentPublisher attachmentPublisher)
51+
{
52+
this.uiContext = uiContext;
53+
this.softAssert = softAssert;
54+
this.elementActions = elementActions;
55+
this.javascriptActions = javascriptActions;
56+
this.attachmentPublisher = attachmentPublisher;
57+
}
58+
59+
/**
60+
* Checks that the context <b>element</b> has an expected <b>CSS property</b>
61+
* @param cssName A name of the <b>CSS property</b>
62+
* @param comparisonRule is equal to, contains, does not contain
63+
* @param cssValue An expected value of the <b>CSS property</b>
64+
*/
65+
@Then("context element has CSS property `$cssName` with value that $comparisonRule `$cssValue`")
66+
public void assertElementCssProperty(String cssName, StringComparisonRule comparisonRule, String cssValue)
67+
{
68+
String actualCssValue = elementActions.getCssValue(uiContext.getCurrentContexOrPageRoot(),
69+
cssName);
70+
Matcher<String> matcher = comparisonRule.createMatcher(cssValue);
71+
softAssert.assertThat("Element css property value is", actualCssValue, matcher);
72+
}
73+
74+
/**
75+
* Checks that the context <b>element</b> has an expected <b>CSS properties</b>
76+
* <p>The expected CSS parameters to be defined in the ExamplesTable:</p>
77+
* <ul>
78+
* <li><b>cssProperty</b> - the name of the CSS property</li>
79+
* <li><b>comparisonRule</b> - String comparison rule: "is equal to", "contains", "does not contain",
80+
* "matches".</li>
81+
* <li><b>expectedValue</b> - expected CSS property value</li>
82+
* </ul>
83+
* <p>Usage example:</p>
84+
* <code>
85+
* <br>Then context element has CSS properties matching rules:
86+
* <br>|cssProperty |comparisonRule |expectedValue |
87+
* <br>|border |contains |solid |
88+
* </code>
89+
*
90+
* @param parameters The parameters of the expected CSS properties to set as ExamplesTable
91+
*/
92+
@Then("context element has CSS properties matching rules:$parameters")
93+
public void doesElementHasCssProperties(List<CssValidationParameters> parameters)
94+
{
95+
String getAllCssScript =
96+
org.vividus.util.ResourceUtils.loadResource("org/vividus/ui/web/get-element-computed-css-func.js");
97+
String script = "([el]) => {" + getAllCssScript + "return getComputedStyleAsMap(el)}";
98+
Locator element = uiContext.getCurrentContexOrPageRoot();
99+
Map<String, String> elementCss = javascriptActions.executeScript(script, element.elementHandle());
100+
101+
List<CssValidationResult> cssResults = validateElementCss(parameters, elementCss);
102+
attachmentPublisher.publishAttachment("templates/css_validation_result.ftl",
103+
Map.of("cssResults", cssResults), "Css validation results");
104+
}
105+
106+
private List<CssValidationResult> validateElementCss(List<CssValidationParameters> parameters,
107+
Map<String, String> elementCss)
108+
{
109+
List<CssValidationResult> cssResults = new ArrayList<>();
110+
parameters.forEach(param ->
111+
{
112+
String cssName = param.getCssProperty();
113+
String expectedValue = param.getExpectedValue();
114+
StringComparisonRule comparisonRule = param.getComparisonRule();
115+
116+
String actualCssValue = getCssValue(elementCss, cssName);
117+
boolean passed = softAssert.assertThat(String.format(ELEMENT_CSS_CONTAINING_VALUE, cssName, expectedValue),
118+
actualCssValue, comparisonRule.createMatcher(expectedValue));
119+
cssResults.add(new CssValidationResult(cssName, actualCssValue, comparisonRule, expectedValue, passed));
120+
});
121+
return cssResults;
122+
}
123+
124+
private String getCssValue(Map<String, String> cssMap, String cssName)
125+
{
126+
return Optional.ofNullable(cssMap.get(cssName)).orElseGet(() -> {
127+
String cssValueAsCamelCase = CaseUtils.toCamelCase(StringUtils.removeStart(cssName, '-'), false, '-');
128+
return cssMap.get(cssValueAsCamelCase);
129+
});
130+
}
131+
}

0 commit comments

Comments
 (0)