Skip to content

Commit 9c9728d

Browse files
author
Denys Zaiats
committed
[responsive-validator] - added writing json report into file
1 parent 3327d81 commit 9c9728d

File tree

4 files changed

+95
-26
lines changed

4 files changed

+95
-26
lines changed

src/main/java/util/validator/LanguageChecker.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.optimaize.langdetect.text.CommonTextObjectFactories;
1212
import com.optimaize.langdetect.text.TextObject;
1313
import com.optimaize.langdetect.text.TextObjectFactory;
14+
import org.apache.log4j.Logger;
1415
import org.openqa.selenium.JavascriptExecutor;
1516
import org.openqa.selenium.WebDriver;
1617

@@ -19,6 +20,8 @@
1920

2021
public class LanguageChecker {
2122

23+
private static final Logger LOG = Logger.getLogger(LanguageChecker.class);
24+
2225
public static Optional<LdLocale> getRecognisedLanguage(String text) throws IOException {
2326
List<LanguageProfile> languageProfiles = new LanguageProfileReader().readAllBuiltIn();
2427

@@ -55,7 +58,7 @@ public static boolean isCorrectLanguageOnThePage(WebDriver driver, String lang)
5558
int bodyTextLength = bodyText.length();
5659

5760
if (bodyTextLength == 0) {
58-
System.out.println("\n!!! - Text on the page is absent\n");
61+
LOG.info("\n!!! - Text on the page is absent\n");
5962
isCorrectLang = false;
6063
} else {
6164
for (int i = 0; i < bodyTextLength; i += textBlockLength) {
@@ -66,16 +69,16 @@ public static boolean isCorrectLanguageOnThePage(WebDriver driver, String lang)
6669
String detectedLanguage = getRecognisedLanguage(tempString).get().getLanguage();
6770

6871
if (!detectedLanguage.toLowerCase().equals(lang.toLowerCase())) {
69-
System.out.println("\n!!! - Piece of text without translation: \n" + tempString + "\nExpected language is \"" + lang + "\"\n");
70-
System.out.println("\n!!! Current URL is " + driver.getCurrentUrl() + "\n- !!!");
71-
System.out.println(String.format("\n!!! Characters are between %s and %s from %s full amount of characters \n", i, i + textBlockLength, bodyTextLength));
72+
LOG.info("\n!!! - Piece of text without translation: \n" + tempString + "\nExpected language is \"" + lang + "\"\n");
73+
LOG.info("\n!!! Current URL is " + driver.getCurrentUrl() + "\n- !!!");
74+
LOG.info(String.format("\n!!! Characters are between %s and %s from %s full amount of characters \n", i, i + textBlockLength, bodyTextLength));
7275
isCorrectLang = false;
7376
break;
7477
}
7578
} catch (Exception e) {
76-
System.out.println("\n!!! - Impossible to recognise the language of this piece of text: \n" + tempString + "\nExpected language is \"" + lang + "\"\n");
77-
System.out.println("\n!!! Current URL is " + driver.getCurrentUrl() + "\n- !!!");
78-
System.out.println(String.format("\n!!! Characters are between %s and %s from %s full amount of characters \n", i, i + textBlockLength, bodyTextLength));
79+
LOG.info("\n!!! - Impossible to recognise the language of this piece of text: \n" + tempString + "\nExpected language is \"" + lang + "\"\n");
80+
LOG.info("\n!!! Current URL is " + driver.getCurrentUrl() + "\n- !!!");
81+
LOG.info(String.format("\n!!! Characters are between %s and %s from %s full amount of characters \n", i, i + textBlockLength, bodyTextLength));
7982
}
8083
}
8184
}

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

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121

2222
public class ResponsiveValidator implements Validator {
2323

24-
public static final String X = "x";
25-
public static final String Y = "y";
26-
public static final String WIDTH = "width";
27-
public static final String HEIGHT = "height";
24+
private static final String X = "x";
25+
private static final String Y = "y";
26+
private static final String WIDTH = "width";
27+
private static final String HEIGHT = "height";
2828
private static final int MIN_OFFSET = -10000;
2929
private static final String ERROR_KEY = "error";
30-
private static final String REASON_KEY = "stacktrace";
3130
private static final String MESSAGE = "message";
3231
private static final String DETAILS = "details";
3332
private final Logger LOG = Logger.getLogger(ResponsiveValidator.class);
@@ -42,6 +41,8 @@ public class ResponsiveValidator implements Validator {
4241
private HashMap<WebElement, String> overlapElements = new HashMap<>();
4342
private HashMap<WebElement, String> offsetLeftElements = new HashMap<>();
4443
private HashMap<WebElement, String> offsetRightElements = new HashMap<>();
44+
private HashMap<WebElement, String> offsetTopElements = new HashMap<>();
45+
private HashMap<WebElement, String> offsetBottomElements = new HashMap<>();
4546
private int minWidth = MIN_OFFSET;
4647
private int maxWidth = MIN_OFFSET;
4748
private int minHeight = MIN_OFFSET;
@@ -69,6 +70,7 @@ public class ResponsiveValidator implements Validator {
6970
private BufferedImage img;
7071
private Graphics2D g;
7172
private JSONArray errorMessage;
73+
private int counterDetails = 0;
7274

7375
public ResponsiveValidator(WebDriver driver) {
7476
this.driver = driver;
@@ -138,6 +140,18 @@ public ResponsiveValidator sameOffsetRightAs(WebElement element, String readable
138140
return this;
139141
}
140142

143+
@Override
144+
public ResponsiveValidator sameOffsetTopAs(WebElement element, String readableName) {
145+
offsetTopElements.put(element, readableName);
146+
return this;
147+
}
148+
149+
@Override
150+
public ResponsiveValidator sameOffsetBottomAs(WebElement element, String readableName) {
151+
offsetBottomElements.put(element, readableName);
152+
return this;
153+
}
154+
141155
@Override
142156
public ResponsiveValidator minWidth(int width) {
143157
minWidth = width;
@@ -201,7 +215,7 @@ public ResponsiveValidator drawMap() {
201215
}
202216

203217
@Override
204-
public JSONObject validate() {
218+
public boolean validate() {
205219
jsonResults = new JSONObject();
206220
jsonResults.put(ERROR_KEY, false);
207221

@@ -250,10 +264,16 @@ public JSONObject validate() {
250264
if (!offsetRightElements.isEmpty()) {
251265
validateRightOffsetForElements();
252266
}
267+
if (!offsetTopElements.isEmpty()) {
268+
validateTopOffsetForElements();
269+
}
270+
if (!offsetBottomElements.isEmpty()) {
271+
validateBottomOffsetForElements();
272+
}
253273

254274
if (!errorMessage.isEmpty()) {
255275
jsonResults.put(ERROR_KEY, true);
256-
jsonResults.put(REASON_KEY, errorMessage);
276+
jsonResults.put(DETAILS, errorMessage);
257277
}
258278

259279
if (withReport) {
@@ -265,6 +285,13 @@ public JSONObject validate() {
265285
}
266286

267287
if (!errorMessage.isEmpty()) {
288+
JSONObject rootDetails = new JSONObject();
289+
rootDetails.put(X, xRoot);
290+
rootDetails.put(Y, yRoot);
291+
rootDetails.put(WIDTH, widthRoot);
292+
rootDetails.put(HEIGHT, heightRoot);
293+
294+
jsonResults.put("rootElement", rootDetails);
268295
jsonResults.put("screenshot", map.getName());
269296
}
270297

@@ -292,10 +319,10 @@ public JSONObject validate() {
292319
}
293320
} else {
294321
jsonResults.put(ERROR_KEY, true);
295-
jsonResults.put(REASON_KEY, "Set root web element");
322+
jsonResults.put(DETAILS, "Set root web element");
296323
}
297324

298-
return jsonResults;
325+
return !((boolean) jsonResults.get(ERROR_KEY));
299326
}
300327

301328
private void drawScreenshot() {
@@ -314,7 +341,7 @@ private void drawScreenshot() {
314341

315342
private void validateRightOffsetForElements() {
316343
for (Map.Entry<WebElement, String> entry : offsetRightElements.entrySet()) {
317-
if (!elementsHasEqualOffset(false, entry.getKey())) {
344+
if (!elementsHasEqualLeftRightOffset(false, entry.getKey())) {
318345
putJsonDetailsWithElement(String.format("Element '%s' has not the same right offset as element '%s'", rootElementReadableName, entry.getValue()), entry.getKey());
319346
}
320347
}
@@ -323,13 +350,31 @@ private void validateRightOffsetForElements() {
323350

324351
private void validateLeftOffsetForElements() {
325352
for (Map.Entry<WebElement, String> entry : offsetLeftElements.entrySet()) {
326-
if (!elementsHasEqualOffset(true, entry.getKey())) {
353+
if (!elementsHasEqualLeftRightOffset(true, entry.getKey())) {
327354
putJsonDetailsWithElement(String.format("Element '%s' has not the same left offset as element '%s'", rootElementReadableName, entry.getValue()), entry.getKey());
328355
}
329356
}
330357
offsetLeftElements.clear();
331358
}
332359

360+
private void validateTopOffsetForElements() {
361+
for (Map.Entry<WebElement, String> entry : offsetTopElements.entrySet()) {
362+
if (!elementsHasEqualTopBottomOffset(true, entry.getKey())) {
363+
putJsonDetailsWithElement(String.format("Element '%s' has not the same top offset as element '%s'", rootElementReadableName, entry.getValue()), entry.getKey());
364+
}
365+
}
366+
offsetTopElements.clear();
367+
}
368+
369+
private void validateBottomOffsetForElements() {
370+
for (Map.Entry<WebElement, String> entry : offsetBottomElements.entrySet()) {
371+
if (!elementsHasEqualTopBottomOffset(false, entry.getKey())) {
372+
putJsonDetailsWithElement(String.format("Element '%s' has not the same bottom offset as element '%s'", rootElementReadableName, entry.getValue()), entry.getKey());
373+
}
374+
}
375+
offsetBottomElements.clear();
376+
}
377+
333378
private void validateOverlappingWithElements() {
334379
for (Map.Entry<WebElement, String> entry : overlapElements.entrySet()) {
335380
if (elementsAreOverlapped(entry.getKey())) {
@@ -453,7 +498,7 @@ private boolean elementsAreOverlapped(WebElement elementOverlapWith) {
453498
|| (xRoot + widthRoot > elLoc.x && yRoot + heightRoot > elLoc.y && xRoot + widthRoot < elLoc.x + elSize.width && yRoot + widthRoot < elLoc.y + elSize.height);
454499
}
455500

456-
private boolean elementsHasEqualOffset(boolean isLeft, WebElement elementToCompare) {
501+
private boolean elementsHasEqualLeftRightOffset(boolean isLeft, WebElement elementToCompare) {
457502
Point elLoc = elementToCompare.getLocation();
458503
Dimension elSize = elementToCompare.getSize();
459504

@@ -464,6 +509,17 @@ private boolean elementsHasEqualOffset(boolean isLeft, WebElement elementToCompa
464509
}
465510
}
466511

512+
private boolean elementsHasEqualTopBottomOffset(boolean isTop, WebElement elementToCompare) {
513+
Point elLoc = elementToCompare.getLocation();
514+
Dimension elSize = elementToCompare.getSize();
515+
516+
if (isTop) {
517+
return yRoot == elLoc.getY();
518+
} else {
519+
return (pageHeight - yRoot + heightRoot) == (pageHeight - elLoc.getY() + elSize.getHeight());
520+
}
521+
}
522+
467523
private void drawElementRect(Color color, WebElement element) {
468524
g.setColor(color);
469525
g.setStroke(new BasicStroke(3));
@@ -472,12 +528,11 @@ private void drawElementRect(Color color, WebElement element) {
472528

473529
private void putJsonDetailsWithoutElement(String message) {
474530
JSONObject details = new JSONObject();
475-
JSONArray arr = new JSONArray();
476531
JSONObject mes = new JSONObject();
477532
mes.put(MESSAGE, message);
478-
arr.add(mes);
479-
details.put(DETAILS, arr);
533+
details.put(counterDetails, mes);
480534
errorMessage.add(details);
535+
counterDetails++;
481536
}
482537

483538
private void putJsonDetailsWithElement(String message, WebElement element) {
@@ -487,7 +542,6 @@ private void putJsonDetailsWithElement(String message, WebElement element) {
487542
float heightContainer = element.getSize().getHeight();
488543

489544
JSONObject details = new JSONObject();
490-
JSONArray arr = new JSONArray();
491545
JSONObject elDetails = new JSONObject();
492546
elDetails.put(X, xContainer);
493547
elDetails.put(Y, yContainer);
@@ -496,9 +550,9 @@ private void putJsonDetailsWithElement(String message, WebElement element) {
496550
JSONObject mes = new JSONObject();
497551
mes.put(MESSAGE, message);
498552
mes.put("element", elDetails);
499-
arr.add(mes);
500-
details.put(DETAILS, arr);
553+
details.put(counterDetails, mes);
501554
errorMessage.add(details);
555+
counterDetails++;
502556
}
503557

504558
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ interface Validator {
2222

2323
ResponsiveValidator sameOffsetRightAs(WebElement element, String readableName);
2424

25+
ResponsiveValidator sameOffsetTopAs(WebElement element, String readableName);
26+
27+
ResponsiveValidator sameOffsetBottomAs(WebElement element, String readableName);
28+
2529
ResponsiveValidator minWidth(int width);
2630

2731
ResponsiveValidator maxWidth(int width);
@@ -40,6 +44,6 @@ interface Validator {
4044

4145
ResponsiveValidator drawMap();
4246

43-
JSONObject validate();
47+
boolean validate();
4448

4549
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Root logger option
2+
log4j.rootLogger=INFO, stdout
3+
4+
# Direct log messages to stdout
5+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
6+
log4j.appender.stdout.Target=System.out
7+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
8+
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

0 commit comments

Comments
 (0)