11package util .validator ;
22
3+ import http .helpers .Helper ;
34import org .apache .commons .io .FileUtils ;
45import org .apache .log4j .Logger ;
56import org .json .simple .JSONArray ;
2324import java .util .concurrent .atomic .AtomicLong ;
2425
2526import static environment .EnvironmentFactory .*;
26- import static util .general .SystemHelper .isAutomotionFolderExists ;
2727import static util .general .SystemHelper .isRetinaDisplay ;
2828import static util .validator .Constants .*;
2929import static util .validator .ResponsiveUIValidator .Units .PX ;
@@ -177,17 +177,15 @@ public boolean validate() {
177177 if (!errorMessage .isEmpty ()) {
178178 jsonResults .put (ERROR_KEY , true );
179179 jsonResults .put (DETAILS , errorMessage );
180- }
181180
182- if ( withReport && ! errorMessage . isEmpty () ) {
183- try {
184- screenshot = ((TakesScreenshot ) driver ).getScreenshotAs (OutputType .FILE );
185- img = ImageIO .read (screenshot );
186- } catch (Exception e ) {
187- LOG .error ("Failed to create screenshot file: " + e .getMessage ());
188- }
181+ if ( withReport ) {
182+ try {
183+ screenshot = ((TakesScreenshot ) driver ).getScreenshotAs (OutputType .FILE );
184+ img = ImageIO .read (screenshot );
185+ } catch (Exception e ) {
186+ LOG .error ("Failed to create screenshot file: " + e .getMessage ());
187+ }
189188
190- if (!errorMessage .isEmpty ()) {
191189 JSONObject rootDetails = new JSONObject ();
192190 rootDetails .put (X , xRoot );
193191 rootDetails .put (Y , yRoot );
@@ -199,31 +197,32 @@ public boolean validate() {
199197 jsonResults .put (TIME_EXECUTION , String .valueOf (System .currentTimeMillis () - startTime ) + " milliseconds" );
200198 jsonResults .put (ELEMENT_NAME , rootElementReadableName );
201199 jsonResults .put (SCREENSHOT , rootElementReadableName .replace (" " , "" ) + "-" + screenshot .getName ());
202- }
203200
204- long ms = System .currentTimeMillis ();
205- String jsonFileName = rootElementReadableName .replace (" " , "" ) + "-automotion" + ms + ".json" ;
206- try (Writer writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (TARGET_AUTOMOTION_JSON + jsonFileName ), StandardCharsets .UTF_8 ))) {
207- writer .write (jsonResults .toJSONString ());
208- } catch (IOException ex ) {
209- LOG .error ("Cannot create json report: " + ex .getMessage ());
210- }
211- jsonFiles .add (jsonFileName );
212- try {
213- File file = new File (TARGET_AUTOMOTION_JSON + rootElementReadableName .replace (" " , "" ) + "-automotion" + ms + ".json" );
214- if (file .getParentFile ().mkdirs ()) {
215- if (file .createNewFile ()) {
216- BufferedWriter writer = new BufferedWriter (new FileWriter (file ));
217- writer .write (jsonResults .toJSONString ());
218- writer .close ();
201+ long ms = System .currentTimeMillis ();
202+ String uuid = Helper .getGeneratedStringWithLength (7 );
203+ String jsonFileName = rootElementReadableName .replace (" " , "" ) + "-automotion" + ms + uuid + ".json" ;
204+ try (Writer writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (TARGET_AUTOMOTION_JSON + jsonFileName ), StandardCharsets .UTF_8 ))) {
205+ writer .write (jsonResults .toJSONString ());
206+ } catch (IOException ex ) {
207+ LOG .error ("Cannot create json report: " + ex .getMessage ());
208+ }
209+ jsonFiles .add (jsonFileName );
210+ try {
211+ File file = new File (TARGET_AUTOMOTION_JSON + rootElementReadableName .replace (" " , "" ) + "-automotion" + ms + uuid + ".json" );
212+ if (file .getParentFile ().mkdirs ()) {
213+ if (file .createNewFile ()) {
214+ BufferedWriter writer = new BufferedWriter (new FileWriter (file ));
215+ writer .write (jsonResults .toJSONString ());
216+ writer .close ();
217+ }
219218 }
219+ } catch (IOException e ) {
220+ e .printStackTrace ();
220221 }
221- } catch (IOException e ) {
222- e .printStackTrace ();
223- }
224222
225- if ((boolean ) jsonResults .get (ERROR_KEY )) {
226- drawScreenshot ();
223+ if ((boolean ) jsonResults .get (ERROR_KEY )) {
224+ drawScreenshot ();
225+ }
227226 }
228227 }
229228 } else {
@@ -319,7 +318,7 @@ void validateGridAlignment(int columns, int rows) {
319318 int mapSize = map .size ();
320319 if (rows > 0 ) {
321320 if (mapSize != rows ) {
322- putJsonDetailsWithoutElement ("Elements in a grid are not aligned properly. Looks like grid has wrong amount of rows. Expected is " + rows + " . Actual is " + mapSize + "" );
321+ putJsonDetailsWithoutElement (String . format ( "Elements in a grid are not aligned properly. Looks like grid has wrong amount of rows. Expected is %d . Actual is %d" , rows , mapSize ) );
323322 }
324323 }
325324
@@ -329,7 +328,7 @@ void validateGridAlignment(int columns, int rows) {
329328 if (rowCount <= mapSize ) {
330329 int actualInARow = entry .getValue ().intValue ();
331330 if (actualInARow != columns ) {
332- putJsonDetailsWithoutElement ("Elements in a grid are not aligned properly in row #" + rowCount + " . Expected " + columns + " elements in a row. Actually it's " + actualInARow + "" );
331+ putJsonDetailsWithoutElement (String . format ( "Elements in a grid are not aligned properly in row #%d . Expected %d elements in a row. Actually it's %d" , rowCount , columns , actualInARow ) );
333332 }
334333 rowCount ++;
335334 }
@@ -341,78 +340,78 @@ void validateGridAlignment(int columns, int rows) {
341340 void validateRightOffsetForChunk (List <WebElement > elements ) {
342341 for (int i = 0 ; i < elements .size () - 1 ; i ++) {
343342 if (!elementsHaveEqualLeftRightOffset (false , elements .get (i ), elements .get (i + 1 ))) {
344- putJsonDetailsWithElement ("Element #" + ( i + 1 ) + " has not the same right offset as element #" + ( i + 2 ) + "" , elements .get (i + 1 ));
343+ putJsonDetailsWithElement (String . format ( "Element #%d has not the same right offset as element #%d" , i + 1 , i + 2 ), elements .get (i + 1 ));
345344 }
346345 }
347346 }
348347
349348 void validateLeftOffsetForChunk (List <WebElement > elements ) {
350349 for (int i = 0 ; i < elements .size () - 1 ; i ++) {
351350 if (!elementsHaveEqualLeftRightOffset (true , elements .get (i ), elements .get (i + 1 ))) {
352- putJsonDetailsWithElement ("Element #" + ( i + 1 ) + " has not the same left offset as element #" + ( i + 2 ) + "" , elements .get (i + 1 ));
351+ putJsonDetailsWithElement (String . format ( "Element #%d has not the same left offset as element #%d" , i + 1 , i + 2 ), elements .get (i + 1 ));
353352 }
354353 }
355354 }
356355
357356 void validateTopOffsetForChunk (List <WebElement > elements ) {
358357 for (int i = 0 ; i < elements .size () - 1 ; i ++) {
359358 if (!elementsHaveEqualTopBottomOffset (true , elements .get (i ), elements .get (i + 1 ))) {
360- putJsonDetailsWithElement ("Element #" + ( i + 1 ) + " has not the same top offset as element #" + ( i + 2 ) + "" , elements .get (i + 1 ));
359+ putJsonDetailsWithElement (String . format ( "Element #%d has not the same top offset as element #%d" , i + 1 , i + 2 ), elements .get (i + 1 ));
361360 }
362361 }
363362 }
364363
365364 void validateBottomOffsetForChunk (List <WebElement > elements ) {
366365 for (int i = 0 ; i < elements .size () - 1 ; i ++) {
367366 if (!elementsHaveEqualTopBottomOffset (false , elements .get (i ), elements .get (i + 1 ))) {
368- putJsonDetailsWithElement ("Element #" + ( i + 1 ) + " has not the same bottom offset as element #" + ( i + 2 ) + "" , elements .get (i + 1 ));
367+ putJsonDetailsWithElement (String . format ( "Element #%d has not the same bottom offset as element #%d" , i + 1 , i + 2 ), elements .get (i + 1 ));
369368 }
370369 }
371370 }
372371
373372 void validateRightOffsetForElements (WebElement element , String readableName ) {
374373 if (!element .equals (rootElement )) {
375- if (!elementsHaveEqualLeftRightOffset (false , element )) {
374+ if (!elementsHaveEqualLeftRightOffset (false , rootElement , element )) {
376375 putJsonDetailsWithElement (String .format ("Element '%s' has not the same right offset as element '%s'" , rootElementReadableName , readableName ), element );
377376 }
378377 }
379378 }
380379
381380 void validateLeftOffsetForElements (WebElement element , String readableName ) {
382381 if (!element .equals (rootElement )) {
383- if (!elementsHaveEqualLeftRightOffset (true , element )) {
382+ if (!elementsHaveEqualLeftRightOffset (true , rootElement , element )) {
384383 putJsonDetailsWithElement (String .format ("Element '%s' has not the same left offset as element '%s'" , rootElementReadableName , readableName ), element );
385384 }
386385 }
387386 }
388387
389388 void validateTopOffsetForElements (WebElement element , String readableName ) {
390389 if (!element .equals (rootElement )) {
391- if (!elementsHaveEqualTopBottomOffset (true , element )) {
390+ if (!elementsHaveEqualTopBottomOffset (true , rootElement , element )) {
392391 putJsonDetailsWithElement (String .format ("Element '%s' has not the same top offset as element '%s'" , rootElementReadableName , readableName ), element );
393392 }
394393 }
395394 }
396395
397396 void validateBottomOffsetForElements (WebElement element , String readableName ) {
398397 if (!element .equals (rootElement )) {
399- if (!elementsHaveEqualTopBottomOffset (false , element )) {
398+ if (!elementsHaveEqualTopBottomOffset (false , rootElement , element )) {
400399 putJsonDetailsWithElement (String .format ("Element '%s' has not the same bottom offset as element '%s'" , rootElementReadableName , readableName ), element );
401400 }
402401 }
403402 }
404403
405404 void validateNotOverlappingWithElements (WebElement element , String readableName ) {
406405 if (!element .equals (rootElement )) {
407- if (elementsAreOverlapped (element )) {
406+ if (elementsAreOverlapped (rootElement , element )) {
408407 putJsonDetailsWithElement (String .format ("Element '%s' is overlapped with element '%s' but should not" , rootElementReadableName , readableName ), element );
409408 }
410409 }
411410 }
412411
413412 void validateOverlappingWithElements (WebElement element , String readableName ) {
414413 if (!element .equals (rootElement )) {
415- if (!elementsAreOverlapped (element )) {
414+ if (!elementsAreOverlapped (rootElement , element )) {
416415 putJsonDetailsWithElement (String .format ("Element '%s' is not overlapped with element '%s' but should be" , rootElementReadableName , readableName ), element );
417416 }
418417 }
@@ -839,7 +838,7 @@ private boolean elementsAreOverlappedOnBorder(WebElement rootElement, WebElement
839838 int sqRootElement = widthRoot * heightRoot ;
840839 int sqElement = elSize .width * elSize .height ;
841840
842- int sqCommon = 0 ;
841+ int sqCommon ;
843842 if (xRoot < elLoc .x && yRoot == elLoc .y ) {
844843 sqCommon = (widthRoot + (elLoc .x - (xRoot + widthRoot ) + elSize .width )) * (heightRoot );
845844 } else if (yRoot < elLoc .y && xRoot == elLoc .x ) {
@@ -855,22 +854,6 @@ private boolean elementsAreOverlappedOnBorder(WebElement rootElement, WebElement
855854 return sqCommon < sqRootElement + sqElement ;
856855 }
857856
858- private boolean elementsAreOverlapped (WebElement elementOverlapWith ) {
859- Point elLoc = elementOverlapWith .getLocation ();
860- Dimension elSize = elementOverlapWith .getSize ();
861- return ((xRoot >= elLoc .x && yRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
862- || (xRoot + widthRoot > elLoc .x && yRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
863- || (xRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot + heightRoot < elLoc .y + elSize .height )
864- || (xRoot + widthRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot + heightRoot < elLoc .y + elSize .height ))
865-
866- || ((elLoc .x > xRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot )
867- || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot )
868- || (elLoc .x > xRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot + heightRoot )
869- || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot + heightRoot ))
870-
871- || elementsAreOverlappedOnBorder (rootElement , elementOverlapWith );
872- }
873-
874857 private boolean elementsAreOverlapped (WebElement rootElement , WebElement elementOverlapWith ) {
875858 Point elLoc = elementOverlapWith .getLocation ();
876859 Dimension elSize = elementOverlapWith .getSize ();
@@ -892,17 +875,6 @@ private boolean elementsAreOverlapped(WebElement rootElement, WebElement element
892875 || elementsAreOverlappedOnBorder (rootElement , elementOverlapWith );
893876 }
894877
895- private boolean elementsHaveEqualLeftRightOffset (boolean isLeft , WebElement elementToCompare ) {
896- Point elLoc = elementToCompare .getLocation ();
897- Dimension elSize = elementToCompare .getSize ();
898-
899- if (isLeft ) {
900- return xRoot == elLoc .getX ();
901- } else {
902- return (pageWidth - xRoot + widthRoot ) == (pageWidth - elLoc .getX () + elSize .getWidth ());
903- }
904- }
905-
906878 private boolean elementsHaveEqualLeftRightOffset (boolean isLeft , WebElement element , WebElement elementToCompare ) {
907879 Point elLoc = elementToCompare .getLocation ();
908880 Dimension elSize = elementToCompare .getSize ();
@@ -916,18 +888,6 @@ private boolean elementsHaveEqualLeftRightOffset(boolean isLeft, WebElement elem
916888 }
917889 }
918890
919-
920- private boolean elementsHaveEqualTopBottomOffset (boolean isTop , WebElement elementToCompare ) {
921- Point elLoc = elementToCompare .getLocation ();
922- Dimension elSize = elementToCompare .getSize ();
923-
924- if (isTop ) {
925- return yRoot == elLoc .getY ();
926- } else {
927- return (pageHeight - yRoot + heightRoot ) == (pageHeight - elLoc .getY () + elSize .getHeight ());
928- }
929- }
930-
931891 private boolean elementsHaveEqualTopBottomOffset (boolean isTop , WebElement element , WebElement elementToCompare ) {
932892 Point elLoc = elementToCompare .getLocation ();
933893 Dimension elSize = elementToCompare .getSize ();
0 commit comments