@@ -35,22 +35,24 @@ public class ResponsiveUIValidator {
3535 protected static WebDriver driver ;
3636 static WebElement rootElement ;
3737 static long startTime ;
38+ private static boolean isMobileTopBar = false ;
3839 private static boolean withReport = false ;
3940 private static String scenarioName = "Default" ;
4041 private static Color rootColor = new Color (255 , 0 , 0 , 255 );
4142 private static Color highlightedElementsColor = new Color (255 , 0 , 255 , 255 );
4243 private static Color linesColor = Color .ORANGE ;
4344 private static String currentZoom = "100%" ;
45+ private static List <String > jsonFiles = new ArrayList <>();
4446 private static File screenshot ;
4547 private static BufferedImage img ;
4648 private static Graphics2D g ;
4749 private static JSONArray errorMessage ;
48- String rootElementReadableName = "Root Element" ;
49- List <WebElement > rootElements ;
5050 boolean drawLeftOffsetLine = false ;
5151 boolean drawRightOffsetLine = false ;
5252 boolean drawTopOffsetLine = false ;
5353 boolean drawBottomOffsetLine = false ;
54+ String rootElementReadableName = "Root Element" ;
55+ List <WebElement > rootElements ;
5456 ResponsiveUIValidator .Units units = PX ;
5557 int xRoot ;
5658 int yRoot ;
@@ -91,6 +93,15 @@ public void setLinesColor(Color color) {
9193 linesColor = color ;
9294 }
9395
96+ /**
97+ * Set top bar mobile offset. Applicable only for native mobile testing
98+ *
99+ * @param state
100+ */
101+ public void setTopBarMobileOffset (boolean state ) {
102+ isMobileTopBar = state ;
103+ }
104+
94105 /**
95106 * Method that defines start of new validation. Needs to be called each time before calling findElement(), findElements()
96107 *
@@ -191,11 +202,13 @@ public boolean validate() {
191202 }
192203
193204 long ms = System .currentTimeMillis ();
194- try (Writer writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (TARGET_AUTOMOTION_JSON + rootElementReadableName .replace (" " , "" ) + "-automotion" + ms + ".json" ), StandardCharsets .UTF_8 ))) {
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 ))) {
195207 writer .write (jsonResults .toJSONString ());
196208 } catch (IOException ex ) {
197209 LOG .error ("Cannot create json report: " + ex .getMessage ());
198210 }
211+ jsonFiles .add (jsonFileName );
199212 try {
200213 File file = new File (TARGET_AUTOMOTION_JSON + rootElementReadableName .replace (" " , "" ) + "-automotion" + ms + ".json" );
201214 if (file .getParentFile ().mkdirs ()) {
@@ -225,9 +238,9 @@ public boolean validate() {
225238 * Call method to generate HTML report
226239 */
227240 public void generateReport () {
228- if (withReport && isAutomotionFolderExists ()) {
241+ if (withReport && ! jsonFiles . isEmpty ()) {
229242 try {
230- new HtmlReportBuilder ().buildReport ();
243+ new HtmlReportBuilder ().buildReport (jsonFiles );
231244 } catch (IOException | ParseException | InterruptedException e ) {
232245 e .printStackTrace ();
233246 }
@@ -240,9 +253,9 @@ public void generateReport() {
240253 * @param name
241254 */
242255 public void generateReport (String name ) {
243- if (withReport && isAutomotionFolderExists ()) {
256+ if (withReport && ! jsonFiles . isEmpty ()) {
244257 try {
245- new HtmlReportBuilder ().buildReport (name );
258+ new HtmlReportBuilder ().buildReport (name , jsonFiles );
246259 } catch (IOException | ParseException | InterruptedException e ) {
247260 e .printStackTrace ();
248261 }
@@ -267,7 +280,7 @@ void drawScreenshot() {
267280
268281 g .setColor (highlightedElementsColor );
269282 g .setStroke (new BasicStroke (2 ));
270- g .drawRect (getRetinaValue ((int ) x ), getRetinaValue (( int ) y ), getRetinaValue ((int ) width ), getRetinaValue ((int ) height ));
283+ g .drawRect (retinaValue ((int ) x ), retinaValue ( mobileY (( int ) y )), retinaValue ((int ) width ), retinaValue ((int ) height ));
271284 }
272285 }
273286
@@ -624,23 +637,23 @@ void validateEqualTopBottomOffset(List<WebElement> elements) {
624637 void drawRoot (Color color ) {
625638 g .setColor (color );
626639 g .setStroke (new BasicStroke (2 ));
627- g .drawRect (getRetinaValue (xRoot ), getRetinaValue ( yRoot ), getRetinaValue (widthRoot ), getRetinaValue (heightRoot ));
628- //g.fillRect(getRetinaValue (xRoot), getRetinaValue ((yRoot), getRetinaValue (widthRoot), getRetinaValue (heightRoot));
640+ g .drawRect (retinaValue (xRoot ), retinaValue ( mobileY ( yRoot )), retinaValue (widthRoot ), retinaValue (heightRoot ));
641+ //g.fillRect(retinaValue (xRoot), retinaValue ((yRoot), retinaValue (widthRoot), retinaValue (heightRoot));
629642
630643 Stroke dashed = new BasicStroke (1 , BasicStroke .CAP_BUTT , BasicStroke .JOIN_BEVEL , 0 , new float []{9 }, 0 );
631644 g .setStroke (dashed );
632645 g .setColor (linesColor );
633646 if (drawLeftOffsetLine ) {
634- g .drawLine (getRetinaValue (xRoot ), 0 , getRetinaValue (xRoot ), getRetinaValue (img .getHeight ()));
647+ g .drawLine (retinaValue (xRoot ), 0 , retinaValue (xRoot ), retinaValue (img .getHeight ()));
635648 }
636649 if (drawRightOffsetLine ) {
637- g .drawLine (getRetinaValue (xRoot + widthRoot ), 0 , getRetinaValue (xRoot + widthRoot ), getRetinaValue (img .getHeight ()));
650+ g .drawLine (retinaValue (xRoot + widthRoot ), 0 , retinaValue (xRoot + widthRoot ), retinaValue (img .getHeight ()));
638651 }
639652 if (drawTopOffsetLine ) {
640- g .drawLine (0 , getRetinaValue ( yRoot ), getRetinaValue (img .getWidth ()), getRetinaValue (yRoot ));
653+ g .drawLine (0 , retinaValue ( mobileY ( yRoot )), retinaValue (img .getWidth ()), retinaValue (yRoot ));
641654 }
642655 if (drawBottomOffsetLine ) {
643- g .drawLine (0 , getRetinaValue ( yRoot + heightRoot ), getRetinaValue (img .getWidth ()), getRetinaValue (yRoot + heightRoot ));
656+ g .drawLine (0 , retinaValue ( mobileY ( yRoot + heightRoot )), retinaValue (img .getWidth ()), retinaValue (yRoot + heightRoot ));
644657 }
645658 }
646659
@@ -695,9 +708,9 @@ String getFormattedMessage(WebElement element) {
695708 String .valueOf (element .getSize ().height ));
696709 }
697710
698- int getRetinaValue (int value ) {
711+ int retinaValue (int value ) {
699712 if (!isMobile ()) {
700- int zoom = Integer .valueOf (currentZoom .replace ("%" , "" ));
713+ int zoom = Integer .parseInt (currentZoom .replace ("%" , "" ));
701714 if (zoom > 100 ) {
702715 value = (int ) (value + (value * Math .abs (zoom - 100f ) / 100f ));
703716 } else if (zoom < 100 ) {
@@ -717,6 +730,28 @@ int getRetinaValue(int value) {
717730 }
718731 }
719732
733+ int mobileY (int value ) {
734+ if (isMobile ()) {
735+ if (isIOS ()) {
736+ if (isMobileTopBar ) {
737+ return value + 20 ;
738+ } else {
739+ return value ;
740+ }
741+ } else if (isAndroid ()) {
742+ if (isMobileTopBar ) {
743+ return value + 20 ;
744+ } else {
745+ return value ;
746+ }
747+ } else {
748+ return value ;
749+ }
750+ } else {
751+ return value ;
752+ }
753+ }
754+
720755 long getPageWidth () {
721756 if (!isMobile ()) {
722757 JavascriptExecutor executor = (JavascriptExecutor ) driver ;
@@ -826,12 +861,12 @@ private boolean elementsAreOverlapped(WebElement elementOverlapWith) {
826861 return ((xRoot >= elLoc .x && yRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
827862 || (xRoot + widthRoot > elLoc .x && yRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
828863 || (xRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot + heightRoot < elLoc .y + elSize .height )
829- || (xRoot + widthRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot + widthRoot < 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 ))
830865
831866 || ((elLoc .x > xRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot )
832867 || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot )
833868 || (elLoc .x > xRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot + heightRoot )
834- || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot + widthRoot ))
869+ || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot + heightRoot ))
835870
836871 || elementsAreOverlappedOnBorder (rootElement , elementOverlapWith );
837872 }
@@ -847,12 +882,12 @@ private boolean elementsAreOverlapped(WebElement rootElement, WebElement element
847882 return ((xRoot > elLoc .x && yRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
848883 || (xRoot + widthRoot > elLoc .x && yRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot < elLoc .y + elSize .height )
849884 || (xRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot < elLoc .x + elSize .width && yRoot + heightRoot < elLoc .y + elSize .height )
850- || (xRoot + widthRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot + widthRoot < elLoc .y + elSize .height ))
885+ || (xRoot + widthRoot > elLoc .x && yRoot + heightRoot > elLoc .y && xRoot + widthRoot < elLoc .x + elSize .width && yRoot + heightRoot < elLoc .y + elSize .height ))
851886
852887 || ((elLoc .x > xRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot )
853888 || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot )
854889 || (elLoc .x > xRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot && elLoc .y + elSize .height < yRoot + heightRoot )
855- || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot + widthRoot ))
890+ || (elLoc .x > xRoot + widthRoot && elLoc .y > yRoot + heightRoot && elLoc .x + elSize .width < xRoot + widthRoot && elLoc .y + elSize .height < yRoot + heightRoot ))
856891
857892 || elementsAreOverlappedOnBorder (rootElement , elementOverlapWith );
858893 }
0 commit comments