Skip to content

Commit b6f8043

Browse files
committed
[responsive-validator] - implemented autogenerating screenshots based on json files
1 parent 9c9728d commit b6f8043

File tree

5 files changed

+105
-4
lines changed

5 files changed

+105
-4
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class ResponsiveValidator implements Validator {
2929
private static final String ERROR_KEY = "error";
3030
private static final String MESSAGE = "message";
3131
private static final String DETAILS = "details";
32+
private static final Object REASON = "reason";
33+
private static final String ELEMENT = "element";
3234
private final Logger LOG = Logger.getLogger(ResponsiveValidator.class);
3335
private WebDriver driver;
3436
private String rootElementReadableName;
@@ -276,7 +278,7 @@ public boolean validate() {
276278
jsonResults.put(DETAILS, errorMessage);
277279
}
278280

279-
if (withReport) {
281+
if (withReport && !errorMessage.isEmpty()) {
280282
try {
281283
map = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
282284
img = ImageIO.read(map);
@@ -330,6 +332,23 @@ private void drawScreenshot() {
330332

331333
drawElementRect(Color.RED, rootElement);
332334

335+
for (Object obj : errorMessage) {
336+
JSONObject det = (JSONObject) obj;
337+
JSONObject details = (JSONObject) det.get(REASON);
338+
JSONObject numE = (JSONObject) details.get(ELEMENT);
339+
340+
if (numE != null) {
341+
float x = (float) numE.get(X);
342+
float y = (float) numE.get(Y);
343+
float width = (float) numE.get(WIDTH);
344+
float height = (float) numE.get(HEIGHT);
345+
346+
g.setColor(Color.MAGENTA);
347+
g.setStroke(new BasicStroke(2));
348+
g.drawRect((int) x, (int) y, (int) width, (int) height);
349+
}
350+
}
351+
333352
try {
334353
ImageIO.write(img, "png", map);
335354
File file = new File("target/automotion/" + map.getName());
@@ -530,7 +549,7 @@ private void putJsonDetailsWithoutElement(String message) {
530549
JSONObject details = new JSONObject();
531550
JSONObject mes = new JSONObject();
532551
mes.put(MESSAGE, message);
533-
details.put(counterDetails, mes);
552+
details.put(REASON, mes);
534553
errorMessage.add(details);
535554
counterDetails++;
536555
}
@@ -549,8 +568,8 @@ private void putJsonDetailsWithElement(String message, WebElement element) {
549568
elDetails.put(HEIGHT, heightContainer);
550569
JSONObject mes = new JSONObject();
551570
mes.put(MESSAGE, message);
552-
mes.put("element", elDetails);
553-
details.put(counterDetails, mes);
571+
mes.put(ELEMENT, elDetails);
572+
details.put(REASON, mes);
554573
errorMessage.add(details);
555574
counterDetails++;
556575
}
10.3 MB
Binary file not shown.

src/test/java/MailServiceTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import http.helpers.MailService;
22
import org.junit.After;
33
import org.junit.Assert;
4+
import org.junit.Ignore;
45
import org.junit.Test;
56

67
import javax.mail.MessagingException;
78

9+
@Ignore
810
public class MailServiceTest {
911

1012
private static final String EMAIL = "[email protected]";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import org.junit.Test;
2+
import org.openqa.selenium.WebDriver;
3+
import util.driver.WebDriverFactory;
4+
import util.validator.ResponsiveValidator;
5+
6+
public class ResponsiveValidatorTest {
7+
8+
@Test
9+
public void testThatResponsiveValidatorWorks() {
10+
System.setProperty("IS_LOCAL", "TRUE");
11+
System.setProperty("BROWSER", "Chrome");
12+
13+
WebDriverFactory driverFactory = new WebDriverFactory();
14+
WebDriver driver = driverFactory.getDriver();
15+
driver.manage().window().maximize();
16+
17+
driver.get("https://www.facey.top");
18+
19+
TestPage page = new TestPage(driver);
20+
21+
new ResponsiveValidator(driver)
22+
.findElement(page.newPhotos(), "New Photos")
23+
.widthBetween(300, 400)
24+
.heightBetween(20, 50)
25+
.minOffset(10, 500, 500, 600)
26+
.maxOffset(200, 2000, 2000, 1000)
27+
.notOverlapWith(page.header(), "Header")
28+
.notOverlapWith(page.myPhotos(), "My Photos")
29+
.notOverlapWith(page.topPhotos(), "Top Photos")
30+
.sameOffsetRightAs(page.logo(), "Logo")
31+
.drawMap()
32+
.validate();
33+
34+
driver.quit();
35+
}
36+
37+
}

src/test/java/TestPage.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import org.openqa.selenium.By;
2+
import org.openqa.selenium.WebDriver;
3+
import org.openqa.selenium.WebElement;
4+
import org.openqa.selenium.support.ui.ExpectedConditions;
5+
import web.BaseWebMobileElement;
6+
7+
/**
8+
* Created by ZayCo on 24/11/16.
9+
*/
10+
public class TestPage extends BaseWebMobileElement {
11+
12+
public TestPage(WebDriver driver) {
13+
super(driver);
14+
}
15+
16+
public WebElement header() {
17+
return getWebElement(By.id("navbar"));
18+
}
19+
20+
public WebElement logo() {
21+
return getWebElement(By.id("main-logo"));
22+
}
23+
24+
public WebElement topPhotos() {
25+
return getWebElement(By.id("top-photos"));
26+
}
27+
28+
public WebElement newPhotos() {
29+
return getWebElement(By.id("new-photos"));
30+
}
31+
32+
public WebElement myPhotos() {
33+
return getWebElement(By.id("my-photos"));
34+
}
35+
36+
public WebElement footer() {
37+
return getWebElement(ExpectedConditions.elementToBeClickable(By.id("footer")));
38+
}
39+
40+
public WebElement contactForm() {
41+
return getWebElement(By.id("contact-form"));
42+
}
43+
}

0 commit comments

Comments
 (0)