Skip to content

Commit f0b039f

Browse files
committed
[responsive-validator] - implemented HTML reporting. Adaptation for Retina display
1 parent f389b6b commit f0b039f

File tree

12 files changed

+194
-10244
lines changed

12 files changed

+194
-10244
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,10 @@
188188
</exclusion>
189189
</exclusions>
190190
</dependency>
191+
<dependency>
192+
<groupId>com.webfirmframework</groupId>
193+
<artifactId>wffweb</artifactId>
194+
<version>2.1.1</version>
195+
</dependency>
191196
</dependencies>
192197
</project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package util.general;
2+
3+
import com.webfirmframework.wffweb.tag.html.*;
4+
import com.webfirmframework.wffweb.tag.html.attribute.Alt;
5+
import com.webfirmframework.wffweb.tag.html.attribute.Src;
6+
import com.webfirmframework.wffweb.tag.html.attribute.global.Style;
7+
import com.webfirmframework.wffweb.tag.html.images.Img;
8+
import com.webfirmframework.wffweb.tag.html.lists.Li;
9+
import com.webfirmframework.wffweb.tag.html.lists.Ol;
10+
import com.webfirmframework.wffweb.tag.html.metainfo.Head;
11+
import com.webfirmframework.wffweb.tag.htmlwff.NoTag;
12+
import org.apache.log4j.Logger;
13+
import org.json.simple.JSONArray;
14+
import org.json.simple.JSONObject;
15+
import org.json.simple.parser.JSONParser;
16+
import org.json.simple.parser.ParseException;
17+
18+
import java.io.*;
19+
import java.nio.charset.StandardCharsets;
20+
21+
/**
22+
* Created by ZayCo on 25/11/16.
23+
*/
24+
public class HtmlReportBuilder {
25+
26+
private static final String TARGET_AUTOMOTION_JSON = "target/automotion/json/";
27+
private static final String TARGET_AUTOMOTION = "target/automotion/";
28+
private final Logger LOG = Logger.getLogger(HtmlReportBuilder.class);
29+
30+
public void buildReport() throws IOException, ParseException, InterruptedException {
31+
Thread.sleep(3000);
32+
Html html = new Html(null) {{
33+
new Head(this) {{
34+
new TitleTag(this) {{
35+
new NoTag(this, "Automotion report");
36+
}};
37+
}};
38+
new Body(this) {{
39+
40+
41+
File folder = new File(TARGET_AUTOMOTION_JSON);
42+
File[] listOfFiles = folder.listFiles();
43+
44+
for (File file : listOfFiles) {
45+
if (file.isFile()) {
46+
JSONParser parser = new JSONParser();
47+
Object obj = parser.parse(new FileReader(file));
48+
49+
JSONObject jsonObject = (JSONObject) obj;
50+
JSONArray details = (JSONArray) jsonObject.get("details");
51+
new H1(this,
52+
new Style("color: black")) {{
53+
new NoTag(this, "Element: " + jsonObject.get("elementName"));
54+
}};
55+
new H2(this,
56+
new Style("color: red")) {{
57+
new NoTag(this, "Failures:");
58+
}};
59+
new Ol(this) {{
60+
for (Object details : details) {
61+
JSONObject det = (JSONObject) details;
62+
JSONObject reason = (JSONObject) det.get("reason");
63+
String numE = (String) reason.get("message");
64+
65+
new Li(this) {{
66+
new NoTag(this, numE.toString());
67+
}};
68+
}
69+
}};
70+
new P(this) {{
71+
new Img(this,
72+
new Src("img/" + jsonObject.get("screenshot")),
73+
new Alt("screenshot"),
74+
new Style("width: 100%"));
75+
}};
76+
}
77+
}
78+
}};
79+
}};
80+
81+
long ms = System.currentTimeMillis();
82+
83+
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(TARGET_AUTOMOTION + "result" + ms + ".html"), StandardCharsets.UTF_8))) {
84+
writer.write(html.toHtmlString());
85+
} catch (IOException ex) {
86+
LOG.error("Cannot create html report: " + ex.getMessage());
87+
}
88+
89+
try {
90+
File file = new File(TARGET_AUTOMOTION + "result" + ms + ".html");
91+
if (file.getParentFile().mkdirs()) {
92+
if (file.createNewFile()) {
93+
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
94+
writer.write(html.toHtmlString());
95+
writer.close();
96+
}
97+
}
98+
} catch (IOException e) {
99+
e.printStackTrace();
100+
}
101+
}
102+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package util.general;
2+
3+
import java.awt.*;
4+
import java.lang.reflect.Field;
5+
6+
public class SystemHelper {
7+
public static boolean isRetinaDisplay(Graphics2D g) {
8+
boolean isRetina = false;
9+
GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
10+
11+
try {
12+
Field field = graphicsDevice.getClass().getDeclaredField("scale");
13+
if (field != null) {
14+
field.setAccessible(true);
15+
Object scale = field.get(graphicsDevice);
16+
if(scale instanceof Integer && ((Integer) scale).intValue() == 2) {
17+
isRetina = true;
18+
}
19+
}
20+
} catch (Exception e) {
21+
e.printStackTrace();
22+
}
23+
return isRetina;
24+
}
25+
}

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import org.apache.log4j.Logger;
55
import org.json.simple.JSONArray;
66
import org.json.simple.JSONObject;
7+
import org.json.simple.parser.ParseException;
78
import org.openqa.selenium.Dimension;
89
import org.openqa.selenium.*;
910
import org.openqa.selenium.Point;
1011
import util.driver.PageValidator;
12+
import util.general.HtmlReportBuilder;
13+
import util.general.SystemHelper;
1114

1215
import javax.imageio.ImageIO;
1316
import java.awt.*;
@@ -19,6 +22,8 @@
1922
import java.util.List;
2023
import java.util.Map;
2124

25+
import static environment.EnvironmentFactory.isChrome;
26+
2227
public class ResponsiveValidator implements Validator {
2328

2429
private static final String X = "x";
@@ -87,6 +92,11 @@ public ResponsiveValidator(WebDriver driver) {
8792
this.driver = driver;
8893
}
8994

95+
@Override
96+
public ResponsiveValidator init() {
97+
return new ResponsiveValidator(driver);
98+
}
99+
90100
@Override
91101
public ResponsiveValidator findElement(WebElement element, String readableNameOfElement) {
92102
rootElement = element;
@@ -352,16 +362,18 @@ public boolean validate() {
352362
rootDetails.put(HEIGHT, heightRoot);
353363

354364
jsonResults.put("rootElement", rootDetails);
365+
jsonResults.put("elementName", rootElementReadableName);
355366
jsonResults.put("screenshot", rootElementReadableName.replace(" ", "") + "-" + screenshot.getName());
356367
}
357368

358-
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(TARGET_AUTOMOTION_JSON + rootElementReadableName.replace(" ", "") + "-automotion" + System.currentTimeMillis() + ".json"), StandardCharsets.UTF_8))) {
369+
long ms = System.currentTimeMillis();
370+
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(TARGET_AUTOMOTION_JSON + rootElementReadableName.replace(" ", "") + "-automotion" + ms + ".json"), StandardCharsets.UTF_8))) {
359371
writer.write(jsonResults.toJSONString());
360372
} catch (IOException ex) {
361-
LOG.error("Cannot create json report: " + ex.getMessage());
373+
LOG.error("Cannot create html report: " + ex.getMessage());
362374
}
363375
try {
364-
File file = new File(TARGET_AUTOMOTION_JSON + rootElementReadableName.replace(" ", "") + "-automotion" + System.currentTimeMillis() + ".json");
376+
File file = new File(TARGET_AUTOMOTION_JSON + rootElementReadableName.replace(" ", "") + "-automotion" + ms + ".json");
365377
if (file.getParentFile().mkdirs()) {
366378
if (file.createNewFile()) {
367379
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
@@ -387,7 +399,11 @@ public boolean validate() {
387399

388400
@Override
389401
public void generateReport() {
390-
402+
try {
403+
new HtmlReportBuilder().buildReport();
404+
} catch (IOException | ParseException | InterruptedException e) {
405+
e.printStackTrace();
406+
}
391407
}
392408

393409
private void drawScreenshot() {
@@ -408,7 +424,11 @@ private void drawScreenshot() {
408424

409425
g.setColor(Color.MAGENTA);
410426
g.setStroke(new BasicStroke(2));
411-
g.drawRect((int) x, (int) y, (int) width, (int) height);
427+
if (SystemHelper.isRetinaDisplay(g) && isChrome()) {
428+
g.drawRect(2 * (int) x, 2 * (int) y, 2 * (int) width, 2 * (int) height);
429+
} else {
430+
g.drawRect((int) x, (int) y, (int) width, (int) height);
431+
}
412432
}
413433
}
414434

@@ -639,13 +659,24 @@ private boolean elementsHasEqualTopBottomOffset(boolean isTop, WebElement elemen
639659
private void drawRoot(Color color) {
640660
g.setColor(color);
641661
g.setStroke(new BasicStroke(2));
642-
g.drawRect(xRoot, yRoot, widthRoot, heightRoot);
662+
if (SystemHelper.isRetinaDisplay(g) && isChrome()) {
663+
g.drawRect(2 * xRoot, 2 * yRoot, 2 * widthRoot, 2 * heightRoot);
664+
} else {
665+
g.drawRect(xRoot, yRoot, widthRoot, heightRoot);
666+
}
643667
g.setStroke(new BasicStroke(1));
644668
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);
669+
if (SystemHelper.isRetinaDisplay(g) && isChrome()) {
670+
g.drawLine(0, 2 * yRoot, 2 * pageWidth, 2 * yRoot);
671+
g.drawLine(0, 2 * (yRoot + heightRoot), 2 * pageWidth, 2 * (yRoot + heightRoot));
672+
g.drawLine(2 * xRoot, 0, 2 * xRoot, 2 * pageHeight);
673+
g.drawLine(2 * (xRoot + widthRoot), 0, 2 * (xRoot + widthRoot), 2 * pageHeight);
674+
} else {
675+
g.drawLine(0, yRoot, pageWidth, yRoot);
676+
g.drawLine(0, yRoot + heightRoot, pageWidth, yRoot + heightRoot);
677+
g.drawLine(xRoot, 0, xRoot, pageHeight);
678+
g.drawLine(xRoot + widthRoot, 0, xRoot + widthRoot, pageHeight);
679+
}
649680
}
650681

651682
private void putJsonDetailsWithoutElement(String message) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.openqa.selenium.WebElement;
44

55
interface Validator {
6+
ResponsiveValidator init();
7+
68
ResponsiveValidator findElement(WebElement element, String readableNameOfElement);
79

810
ResponsiveValidator withLeftElement(WebElement element);
3.18 MB
Binary file not shown.

src/main/resources/html-result-template/automotion-1480069761155.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/resources/html-result-template/automotion-1480069762020.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Whitespace-only changes.

0 commit comments

Comments
 (0)