Skip to content

Commit 9b8b778

Browse files
author
Denys Zaiats
committed
[responsive-validator] - added more validation
1 parent f80cef5 commit 9b8b778

File tree

7 files changed

+10376
-35
lines changed

7 files changed

+10376
-35
lines changed

src/main/java/util/validator/ResponsiveValidator.java

Lines changed: 132 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ public class ResponsiveValidator implements Validator {
3131
private static final String DETAILS = "details";
3232
private static final Object REASON = "reason";
3333
private static final String ELEMENT = "element";
34+
private static final String TARGET_AUTOMOTION_JSON = "target/automotion/json/";
35+
private static final String TARGET_AUTOMOTION_IMG = "target/automotion/img/";
3436
private final Logger LOG = Logger.getLogger(ResponsiveValidator.class);
3537
private WebDriver driver;
3638
private String rootElementReadableName;
3739
private WebElement rootElement;
3840
private WebElement leftElement;
3941
private WebElement rightElement;
40-
private WebElement aboveElement;
41-
private WebElement belowElement;
42+
private WebElement topElement;
43+
private WebElement bottomElement;
4244
private WebElement containerElement;
4345
private HashMap<WebElement, String> overlapElements = new HashMap<>();
4446
private HashMap<WebElement, String> offsetLeftElements = new HashMap<>();
@@ -68,11 +70,18 @@ public class ResponsiveValidator implements Validator {
6870
private int elementBottomOffset;
6971
private String readableContainerName;
7072
private JSONObject jsonResults;
71-
private File map;
73+
private File screenshot;
7274
private BufferedImage img;
7375
private Graphics2D g;
7476
private JSONArray errorMessage;
75-
private int counterDetails = 0;
77+
private int leftMinMargin;
78+
private int leftMaxMargin;
79+
private int rightMinMargin;
80+
private int rightMaxMargin;
81+
private int topMinMargin;
82+
private int topMaxMargin;
83+
private int bottomMinMargin;
84+
private int bottomMaxMargin;
7685

7786
public ResponsiveValidator(WebDriver driver) {
7887
this.driver = driver;
@@ -99,21 +108,54 @@ public ResponsiveValidator withLeftElement(WebElement element) {
99108
return this;
100109
}
101110

111+
@Override
112+
public ResponsiveValidator withLeftElement(WebElement element, int minMargin, int maxMargin) {
113+
leftElement = element;
114+
leftMinMargin = minMargin;
115+
leftMaxMargin = maxMargin;
116+
117+
return this;
118+
}
119+
102120
@Override
103121
public ResponsiveValidator withRightElement(WebElement element) {
104122
rightElement = element;
105123
return this;
106124
}
107125

108126
@Override
109-
public ResponsiveValidator withAboveElement(WebElement element) {
110-
aboveElement = element;
127+
public ResponsiveValidator withRightElement(WebElement element, int minMargin, int maxMargin) {
128+
rightElement = element;
129+
rightMinMargin = minMargin;
130+
rightMaxMargin = maxMargin;
131+
return this;
132+
}
133+
134+
@Override
135+
public ResponsiveValidator withTopElement(WebElement element) {
136+
topElement = element;
137+
return this;
138+
}
139+
140+
@Override
141+
public ResponsiveValidator withTopElement(WebElement element, int minMargin, int maxMargin) {
142+
topElement = element;
143+
topMinMargin = minMargin;
144+
topMaxMargin = maxMargin;
111145
return this;
112146
}
113147

114148
@Override
115-
public ResponsiveValidator withBelowElement(WebElement element) {
116-
belowElement = element;
149+
public ResponsiveValidator withBottomElement(WebElement element) {
150+
bottomElement = element;
151+
return this;
152+
}
153+
154+
@Override
155+
public ResponsiveValidator withBottomElement(WebElement element, int minMargin, int maxMargin) {
156+
bottomElement = element;
157+
bottomMinMargin = minMargin;
158+
bottomMaxMargin = maxMargin;
117159
return this;
118160
}
119161

@@ -225,16 +267,32 @@ public boolean validate() {
225267
errorMessage = new JSONArray();
226268

227269
if (leftElement != null) {
228-
validateLeftElement();
270+
if (leftMinMargin > 0) {
271+
validateLeftElementWithMargin();
272+
} else {
273+
validateLeftElement();
274+
}
229275
}
230276
if (rightElement != null) {
231-
validateRightElement();
277+
if (rightMinMargin > 0) {
278+
validateRightElementWithMargin();
279+
} else {
280+
validateRightElement();
281+
}
232282
}
233-
if (aboveElement != null) {
234-
validateAboveElement();
283+
if (topElement != null) {
284+
if (topMinMargin > 0) {
285+
validateAboveElementWithMargin();
286+
} else {
287+
validateAboveElement();
288+
}
235289
}
236-
if (belowElement != null) {
237-
validateBelowElement();
290+
if (bottomElement != null) {
291+
if (bottomMinMargin > 0) {
292+
validateBelowElementWithMargin();
293+
} else {
294+
validateBelowElement();
295+
}
238296
}
239297
if (containerElement != null) {
240298
validateInsideOfContainer();
@@ -280,10 +338,10 @@ public boolean validate() {
280338

281339
if (withReport && !errorMessage.isEmpty()) {
282340
try {
283-
map = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
284-
img = ImageIO.read(map);
341+
screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
342+
img = ImageIO.read(screenshot);
285343
} catch (Exception e) {
286-
LOG.error("Failed to create map file: " + e.getMessage());
344+
LOG.error("Failed to create screenshot file: " + e.getMessage());
287345
}
288346

289347
if (!errorMessage.isEmpty()) {
@@ -294,16 +352,16 @@ public boolean validate() {
294352
rootDetails.put(HEIGHT, heightRoot);
295353

296354
jsonResults.put("rootElement", rootDetails);
297-
jsonResults.put("screenshot", map.getName());
355+
jsonResults.put("screenshot", rootElementReadableName.replace(" ", "") + "-" + screenshot.getName());
298356
}
299357

300-
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("target/automotion/automotion-" + System.currentTimeMillis() + ".json"), StandardCharsets.UTF_8))) {
358+
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(TARGET_AUTOMOTION_JSON + rootElementReadableName.replace(" ", "") + "-automotion" + System.currentTimeMillis() + ".json"), StandardCharsets.UTF_8))) {
301359
writer.write(jsonResults.toJSONString());
302360
} catch (IOException ex) {
303361
LOG.error("Cannot create json report: " + ex.getMessage());
304362
}
305363
try {
306-
File file = new File("target/automotion/automotion-" + System.currentTimeMillis() + ".json");
364+
File file = new File(TARGET_AUTOMOTION_JSON + rootElementReadableName.replace(" ", "") + "-automotion" + System.currentTimeMillis() + ".json");
307365
if (file.getParentFile().mkdirs()) {
308366
if (file.createNewFile()) {
309367
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
@@ -327,10 +385,15 @@ public boolean validate() {
327385
return !((boolean) jsonResults.get(ERROR_KEY));
328386
}
329387

388+
@Override
389+
public void generateReport() {
390+
391+
}
392+
330393
private void drawScreenshot() {
331394
g = img.createGraphics();
332395

333-
drawElementRect(Color.RED, rootElement);
396+
drawRoot(Color.RED);
334397

335398
for (Object obj : errorMessage) {
336399
JSONObject det = (JSONObject) obj;
@@ -350,9 +413,9 @@ private void drawScreenshot() {
350413
}
351414

352415
try {
353-
ImageIO.write(img, "png", map);
354-
File file = new File("target/automotion/" + map.getName());
355-
FileUtils.copyFile(map, file);
416+
ImageIO.write(img, "png", screenshot);
417+
File file = new File(TARGET_AUTOMOTION_IMG + rootElementReadableName.replace(" ", "") + "-" + screenshot.getName());
418+
FileUtils.copyFile(screenshot, file);
356419
} catch (IOException e) {
357420
e.printStackTrace();
358421
}
@@ -468,26 +531,51 @@ private void validateInsideOfContainer() {
468531
}
469532
}
470533

534+
private void validateBelowElementWithMargin() {
535+
int yBelowElement = bottomElement.getLocation().getY();
536+
int marginBetweenRoot = yBelowElement - yRoot + heightRoot;
537+
if (marginBetweenRoot < bottomMinMargin || marginBetweenRoot > bottomMaxMargin) {
538+
putJsonDetailsWithElement(String.format("Below element aligned not properly. Expected margin should be between %spx and %spx. Actual margin is %spx", bottomMinMargin, bottomMaxMargin, marginBetweenRoot), bottomElement);
539+
}
540+
}
541+
471542
private void validateBelowElement() {
472543
List<WebElement> elements = new ArrayList<>();
473544
elements.add(rootElement);
474-
elements.add(belowElement);
545+
elements.add(bottomElement);
475546

476547
if (!PageValidator.elementsAreAlignedVertically(elements)) {
477548
putJsonDetailsWithoutElement("Below element aligned not properly");
478549
}
479550
}
480551

552+
private void validateAboveElementWithMargin() {
553+
int yAboveElement = topElement.getLocation().getY();
554+
int heightAboveElement = topElement.getSize().getHeight();
555+
int marginBetweenRoot = yRoot - yAboveElement + heightAboveElement;
556+
if (marginBetweenRoot < topMinMargin || marginBetweenRoot > topMaxMargin) {
557+
putJsonDetailsWithElement(String.format("Above element aligned not properly. Expected margin should be between %spx and %spx. Actual margin is %spx", topMinMargin, topMaxMargin, marginBetweenRoot), topElement);
558+
}
559+
}
560+
481561
private void validateAboveElement() {
482562
List<WebElement> elements = new ArrayList<>();
483-
elements.add(aboveElement);
563+
elements.add(topElement);
484564
elements.add(rootElement);
485565

486566
if (!PageValidator.elementsAreAlignedVertically(elements)) {
487567
putJsonDetailsWithoutElement("Above element aligned not properly");
488568
}
489569
}
490570

571+
private void validateRightElementWithMargin() {
572+
int xRightElement = rightElement.getLocation().getX();
573+
int marginBetweenRoot = xRightElement - xRoot + widthRoot;
574+
if (marginBetweenRoot < rightMinMargin || marginBetweenRoot > rightMaxMargin) {
575+
putJsonDetailsWithElement(String.format("Right element aligned not properly. Expected margin should be between %spx and %spx. Actual margin is %spx", rightMinMargin, rightMaxMargin, marginBetweenRoot), rightElement);
576+
}
577+
}
578+
491579
private void validateRightElement() {
492580
List<WebElement> elements = new ArrayList<>();
493581
elements.add(rootElement);
@@ -498,6 +586,15 @@ private void validateRightElement() {
498586
}
499587
}
500588

589+
private void validateLeftElementWithMargin() {
590+
int xLeftElement = leftElement.getLocation().getX();
591+
int widthLeftElement = leftElement.getSize().getWidth();
592+
int marginBetweenRoot = xRoot - xLeftElement + widthLeftElement;
593+
if (marginBetweenRoot < leftMinMargin || marginBetweenRoot > leftMaxMargin) {
594+
putJsonDetailsWithElement(String.format("Left element aligned not properly. Expected margin should be between %spx and %spx. Actual margin is %spx", leftMinMargin, leftMaxMargin, marginBetweenRoot), leftElement);
595+
}
596+
}
597+
501598
private void validateLeftElement() {
502599
List<WebElement> elements = new ArrayList<>();
503600
elements.add(leftElement);
@@ -539,10 +636,16 @@ private boolean elementsHasEqualTopBottomOffset(boolean isTop, WebElement elemen
539636
}
540637
}
541638

542-
private void drawElementRect(Color color, WebElement element) {
639+
private void drawRoot(Color color) {
543640
g.setColor(color);
544-
g.setStroke(new BasicStroke(3));
545-
g.drawRect(element.getLocation().x, element.getLocation().y, element.getSize().width, element.getSize().height);
641+
g.setStroke(new BasicStroke(2));
642+
g.drawRect(xRoot, yRoot, widthRoot, heightRoot);
643+
g.setStroke(new BasicStroke(1));
644+
g.setColor(Color.ORANGE);
645+
g.drawLine(0, yRoot, pageWidth, yRoot);
646+
g.drawLine(0, yRoot + heightRoot, pageWidth, yRoot + heightRoot);
647+
g.drawLine(xRoot, 0, xRoot, pageHeight);
648+
g.drawLine(xRoot + widthRoot, 0, xRoot + widthRoot, pageHeight);
546649
}
547650

548651
private void putJsonDetailsWithoutElement(String message) {
@@ -551,7 +654,6 @@ private void putJsonDetailsWithoutElement(String message) {
551654
mes.put(MESSAGE, message);
552655
details.put(REASON, mes);
553656
errorMessage.add(details);
554-
counterDetails++;
555657
}
556658

557659
private void putJsonDetailsWithElement(String message, WebElement element) {
@@ -571,7 +673,5 @@ private void putJsonDetailsWithElement(String message, WebElement element) {
571673
mes.put(ELEMENT, elDetails);
572674
details.put(REASON, mes);
573675
errorMessage.add(details);
574-
counterDetails++;
575676
}
576-
577677
}

src/main/java/util/validator/Validator.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package util.validator;
22

3-
import org.json.simple.JSONObject;
43
import org.openqa.selenium.WebElement;
54

65
interface Validator {
76
ResponsiveValidator findElement(WebElement element, String readableNameOfElement);
87

98
ResponsiveValidator withLeftElement(WebElement element);
109

10+
ResponsiveValidator withLeftElement(WebElement element, int minMargin, int maxMargin);
11+
1112
ResponsiveValidator withRightElement(WebElement element);
1213

13-
ResponsiveValidator withAboveElement(WebElement element);
14+
ResponsiveValidator withRightElement(WebElement element, int minMargin, int maxMargin);
15+
16+
ResponsiveValidator withTopElement(WebElement element);
17+
18+
ResponsiveValidator withTopElement(WebElement element, int minMargin, int maxMargin);
1419

15-
ResponsiveValidator withBelowElement(WebElement element);
20+
ResponsiveValidator withBottomElement(WebElement element);
21+
22+
ResponsiveValidator withBottomElement(WebElement element, int minMargin, int maxMargin);
1623

1724
ResponsiveValidator insideOf(WebElement element, String readableContainerName);
1825

@@ -46,4 +53,6 @@ interface Validator {
4653

4754
boolean validate();
4855

56+
void generateReport();
57+
4958
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rootElement":{"x":493,"width":934,"y":323,"height":405},"details":[{"reason":{"message":"Expected min height of element 'Fields' is: 500px. Actual height is: 405px"}},{"reason":{"message":"Element 'Fields' has not the same left offset as element 'Panels'","element":{"x":489.0,"width":942.0,"y":731.0,"height":68.0}}},{"reason":{"message":"Element 'Fields' has not the same right offset as element 'Panels'","element":{"x":489.0,"width":942.0,"y":731.0,"height":68.0}}},{"reason":{"message":"Element 'Fields' has not the same right offset as element 'Clear All'","element":{"x":1386.0,"width":38.0,"y":263.0,"height":48.0}}}],"screenshot":"screenshot6339565532283170740.png","error":true}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rootElement":{"x":1386,"width":38,"y":263,"height":48},"details":[{"reason":{"message":"Left element aligned not properly. Expected margin should be between 50px and 100px. Actual margin is 1503px","element":{"x":493.0,"width":610.0,"y":263.0,"height":48.0}}},{"reason":{"message":"Element 'Clear All' has not the same left offset as element 'Panels'","element":{"x":489.0,"width":942.0,"y":731.0,"height":68.0}}}],"screenshot":"screenshot6115477721966526307.png","error":true}

src/main/resources/html-result-template/css/style.css

Whitespace-only changes.

0 commit comments

Comments
 (0)