Skip to content

Commit f841298

Browse files
authored
Merge pull request #26 from ITArray/issue-24
Issue 24
2 parents 693203c + 6f563f4 commit f841298

File tree

4 files changed

+67
-26
lines changed

4 files changed

+67
-26
lines changed

src/main/java/util/driver/CapabilitiesFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ private static DesiredCapabilities getPhantomJSCapabilities() {
8585
capabilities.setCapability("browserName", "PhantomJS");
8686
capabilities.setCapability("browser", "phantomjs");
8787
capabilities.setCapability("phantomjs.page.settings.userAgent", getUserAgent());
88+
capabilities.setCapability("acceptSslCerts", true);
89+
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes","--ignore-ssl-errors=true","--ssl-protocol=tlsv1"});
8890
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
8991
getPhantomJsPath()
9092
);

src/main/java/util/general/HtmlReportBuilder.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,23 @@
2121
import java.nio.charset.StandardCharsets;
2222
import java.text.SimpleDateFormat;
2323
import java.util.Date;
24+
import java.util.List;
2425

2526
import static util.validator.Constants.*;
2627

2728

2829
public class HtmlReportBuilder {
2930

3031
private final Logger LOG = Logger.getLogger(HtmlReportBuilder.class);
32+
private List<String> jsonFiles;
3133

32-
public void buildReport(String reportName) throws IOException, ParseException, InterruptedException {
34+
public void buildReport(String reportName, List<String> jsonFiles) throws IOException, ParseException, InterruptedException {
35+
this.jsonFiles = jsonFiles;
3336
writeReport(reportName);
3437
}
3538

36-
public void buildReport() throws IOException, ParseException, InterruptedException {
39+
public void buildReport(List<String> jsonFiles) throws IOException, ParseException, InterruptedException {
40+
this.jsonFiles = jsonFiles;
3741
writeReport("result");
3842
}
3943

@@ -84,7 +88,7 @@ private Html buildHtml() throws IOException, ParseException {
8488

8589
if (listOfFiles != null) {
8690
for (File file : listOfFiles) {
87-
if (file.isFile()) {
91+
if (file.isFile() && jsonFiles.contains(file.getName())) {
8892
JSONParser parser = new JSONParser();
8993
Object obj = parser.parse(new FileReader(file));
9094

@@ -124,6 +128,7 @@ private Html buildHtml() throws IOException, ParseException {
124128
new Style("width: 96%; margin-left:2%"));
125129
}};
126130

131+
jsonFiles.remove(file.getName());
127132
while (!file.delete()) ;
128133
}
129134
}

src/main/java/util/validator/ResponsiveUIValidator.java

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/test/java/ResponsiveValidatorTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ public class ResponsiveValidatorTest {
2323
@Test
2424
public void testThatResponsiveValidatorWorks() {
2525
Map<String, String> sysProp = new HashMap<>();
26-
//sysProp.put("BROWSER", "phantomjs");
27-
//sysProp.put("IS_HEADLESS", "true");
26+
//sysProp.put("BROWSER", "Chrome");
27+
//sysProp.put("IS_LOCAL", "true");
2828
sysProp.put("IS_HEADLESS", "true");
29-
sysProp.put("BROWSER", "phantomjs");
3029
sysProp.put(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/Users/ZayCo/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs");
3130
EnvironmentHelper.setEnv(sysProp);
3231
WebDriverFactory driverFactory = new WebDriverFactory();

0 commit comments

Comments
 (0)