Skip to content

Commit 17769cb

Browse files
authored
Merge pull request #72 from ITArray/snapshots
Snapshots
2 parents d8fbc88 + bb86cd5 commit 17769cb

17 files changed

+143
-80
lines changed

src/main/java/net/itarray/automotion/internal/DrawableScreenshot.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.itarray.automotion.internal;
22

3-
import org.apache.commons.io.FileUtils;
3+
import net.itarray.automotion.tools.helpers.Helper;
44
import org.json.simple.JSONObject;
55

66
import javax.imageio.ImageIO;
@@ -13,19 +13,20 @@
1313

1414
public class DrawableScreenshot {
1515

16-
private final File screenshot;
1716
private final DrawingConfiguration drawingConfiguration;
1817
private BufferedImage img;
1918
private TransformedGraphics graphics;
20-
private File output;
19+
private File screenshotName;
2120
private File drawingsOutput;
2221
private BufferedImage drawings;
2322

24-
public DrawableScreenshot(DriverFacade driver, SimpleTransform transform, DrawingConfiguration drawingConfiguration) {
25-
screenshot = driver.takeScreenshot();
23+
public DrawableScreenshot(SimpleTransform transform, DrawingConfiguration drawingConfiguration, String rootElementReadableName, File screenshotName) {
2624
this.drawingConfiguration = drawingConfiguration;
25+
this.screenshotName = screenshotName;
26+
drawingsOutput = new File(TARGET_AUTOMOTION_IMG + rootElementReadableName.replace(" ", "") + "-draw-" + System.currentTimeMillis() + Helper.getGeneratedStringWithLength(7) + ".png");
27+
2728
try {
28-
img = ImageIO.read(screenshot);
29+
img = ImageIO.read(screenshotName);
2930

3031
drawings = new BufferedImage(img.getWidth(), img.getHeight(),
3132
BufferedImage.TYPE_INT_ARGB);
@@ -35,12 +36,18 @@ public DrawableScreenshot(DriverFacade driver, SimpleTransform transform, Drawin
3536
graphics = new TransformedGraphics(g2d, transform);
3637

3738
} catch (Exception e) {
38-
throw new RuntimeException("Failed to create screenshot file: " + screenshot, e);
39+
throw new RuntimeException("Failed to create screenshot file: " + screenshotName, e);
3940
}
4041
}
4142

42-
public File getOutput() {
43-
return output;
43+
public static File takeScreenshot(DriverFacade driver, String rootElementReadableName) {
44+
File screenshotName = new File(TARGET_AUTOMOTION_IMG + rootElementReadableName.replace(" ", "") + "-" + System.currentTimeMillis() + Helper.getGeneratedStringWithLength(7) + ".png");
45+
driver.takeScreenshot(screenshotName);
46+
return screenshotName;
47+
}
48+
49+
public File getScreenshotName() {
50+
return screenshotName;
4451
}
4552

4653
public File getDrawingsOutput() {
@@ -68,18 +75,13 @@ public void drawScreenshot(String rootElementReadableName, Errors errors) {
6875
}
6976
}
7077

71-
output = new File(TARGET_AUTOMOTION_IMG + rootElementReadableName.replace(" ", "") + "-" + screenshot.getName());
72-
try {
73-
FileUtils.moveFile(screenshot, output);
74-
} catch (IOException e) {
75-
throw new RuntimeException(e);
76-
}
7778

78-
drawingsOutput = new File(TARGET_AUTOMOTION_IMG + rootElementReadableName.replace(" ", "") + "-trans-" + screenshot.getName());
79+
80+
7981
try {
8082
ImageIO.write(drawings, "png", drawingsOutput);
8183
} catch (IOException e) {
82-
throw new RuntimeException("Writing file failed for " + screenshot , e);
84+
throw new RuntimeException("Writing file failed for " + drawingsOutput , e);
8385
}
8486

8587
drawings.getGraphics().dispose();

src/main/java/net/itarray/automotion/internal/DriverFacade.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
import org.openqa.selenium.OutputType;
77
import org.openqa.selenium.TakesScreenshot;
88
import org.openqa.selenium.WebDriver;
9+
import org.openqa.selenium.WebDriverException;
910

1011
import java.io.File;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.io.OutputStream;
1115

1216
import static net.itarray.automotion.tools.environment.EnvironmentFactory.*;
1317

@@ -22,6 +26,17 @@ public File takeScreenshot() {
2226
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
2327
}
2428

29+
public void takeScreenshot(File file) {
30+
file.getParentFile().mkdirs();
31+
byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
32+
33+
try (OutputStream stream = new FileOutputStream(file); ){
34+
stream.write(bytes);
35+
} catch (IOException e) {
36+
throw new RuntimeException(e);
37+
}
38+
}
39+
2540
public boolean isAppiumWebContext() {
2641
if (!(driver instanceof AppiumDriver)) {
2742
return false;

src/main/java/net/itarray/automotion/internal/ResponsiveUIValidatorBase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ private void compileValidationReport() {
100100
return;
101101
}
102102

103-
DrawableScreenshot screenshot = new DrawableScreenshot(getDriver(), getTransform(), getDrawingConfiguration());
103+
File screenshotName = snapshot.takeScreenshot();
104+
105+
DrawableScreenshot screenshot = new DrawableScreenshot(getTransform(), getDrawingConfiguration(), getNameOfToBeValidated(), screenshotName);
104106

105107
drawRootElement(screenshot);
106108

@@ -171,7 +173,7 @@ private void writeResults(DrawableScreenshot drawableScreenshot) {
171173
jsonResults.put(ROOT_ELEMENT, rootDetails);
172174
jsonResults.put(TIME_EXECUTION, String.valueOf(System.currentTimeMillis() - startTime) + " milliseconds");
173175
jsonResults.put(ELEMENT_NAME, getNameOfToBeValidated());
174-
jsonResults.put(SCREENSHOT, drawableScreenshot.getOutput().getName());
176+
jsonResults.put(SCREENSHOT, drawableScreenshot.getScreenshotName().getName());
175177
jsonResults.put(DRAWINGS, drawableScreenshot.getDrawingsOutput().getName());
176178

177179
long ms = System.currentTimeMillis();

src/main/java/net/itarray/automotion/internal/UIElement.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.itarray.automotion.internal.geometry.Group;
66
import net.itarray.automotion.internal.geometry.Rectangle;
77
import net.itarray.automotion.internal.geometry.Scalar;
8+
import net.itarray.automotion.internal.geometry.Vector;
89
import net.itarray.automotion.internal.properties.Context;
910
import net.itarray.automotion.validation.properties.Condition;
1011
import net.itarray.automotion.tools.general.SystemHelper;
@@ -88,7 +89,7 @@ public Scalar getEnd(Direction direction) {
8889
return direction.end(rectangle);
8990
}
9091

91-
public Scalar getExtend(Direction direction) {
92+
public <V extends Group<V>> V getExtend(ExtendGiving<V> direction) {
9293
return direction.extend(rectangle);
9394
}
9495

@@ -108,6 +109,10 @@ public int getHeight() {
108109
return DOWN.extend(rectangle).getValue();
109110
}
110111

112+
public Vector getSize() {
113+
return ORIGIN_CORNER.extend(rectangle);
114+
}
115+
111116
public int getCornerX() {
112117
return rectangle.getCorner().getX().getValue();
113118
}
@@ -360,7 +365,7 @@ public void validateSuccessor(Direction direction, UIElement toBeValidatedSucces
360365
errors.add(
361366
String.format("%s element aligned not properly. Expected margin should be %s. Actual margin is %s",
362367
direction.afterName(),
363-
condition.toStringWithUnits(PIXELS),
368+
condition.getDescription(context, direction),
364369
signedDistance.toStringWithUnits(PIXELS)),
365370
toBeValidatedSuccessor);
366371
}
@@ -408,7 +413,7 @@ public void validateOffset(Direction direction, Condition condition, UIElement p
408413
String.format("Expected %s offset of element %s to be %s. Actual %s offset is: %s",
409414
direction.endName(),
410415
getQuotedName(),
411-
condition.toStringWithUnits(PIXELS),
416+
condition.getDescription(context, direction),
412417
direction.endName(),
413418
getOffset(direction, page).toStringWithUnits(PIXELS)));
414419
}
@@ -452,7 +457,7 @@ public void validateExtend(Direction direction, Condition condition, Context con
452457
String.format("Expected %s of element %s to be %s. Actual %s is: %s",
453458
direction.extendName(),
454459
getQuotedName(),
455-
condition.toStringWithUnits(PIXELS),
460+
condition.getDescription(context, direction),
456461
direction.extendName(),
457462
direction.extend(rectangle).toStringWithUnits(PIXELS)));
458463
}

src/main/java/net/itarray/automotion/internal/UIValidatorBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import net.itarray.automotion.internal.geometry.Scalar;
55
import net.itarray.automotion.internal.properties.Context;
66
import net.itarray.automotion.validation.properties.Expression;
7-
import net.itarray.automotion.internal.properties.ScalarConstant;
7+
import net.itarray.automotion.internal.properties.PixelConstant;
88
import net.itarray.automotion.validation.UIElementValidator;
99
import net.itarray.automotion.validation.UISnapshot;
1010
import net.itarray.automotion.validation.Units;
@@ -95,7 +95,7 @@ public UIValidatorBase isLeftOf(WebElement element, Condition<Scalar> distanceCo
9595
}
9696

9797
private Expression<Scalar> scalarExpression(int width) {
98-
return isPixels() ? new ScalarConstant(new Scalar(width)) : Expression.percent(new Scalar(width), PAGE);
98+
return isPixels() ? new PixelConstant(new Scalar(width)) : Expression.percent(new Scalar(width), PAGE);
9999
}
100100

101101
private Condition<Scalar> betweenCondition(int minMargin, int maxMargin) {

src/main/java/net/itarray/automotion/internal/properties/Between.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public boolean isSatisfiedOn(Scalar value, Context context, Direction direction)
2525
}
2626

2727
@Override
28-
public String toStringWithUnits(String units) {
29-
return String.format("between %s and %s", lowerLimitExpression.toStringWithUnits(units), upperLimitExpression.toStringWithUnits(units));
28+
public String getDescription(Context context, Direction direction) {
29+
return String.format("between %s and %s", lowerLimitExpression.getDescription(context, direction), upperLimitExpression.getDescription(context, direction));
3030
}
3131

3232
@Override

src/main/java/net/itarray/automotion/internal/properties/BinaryScalarConditionWithFixedOperand.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ public boolean isSatisfiedOn(Scalar value, Context context, Direction direction)
3232
}
3333

3434
@Override
35-
public String toStringWithUnits(String units) {
36-
if (toStringFormat != null) {
37-
return String.format(toStringFormat, fixedOperand.toStringWithUnits(units));
38-
}
39-
return fixedOperand.toStringWithUnits(units);
35+
public String getDescription(Context context, Direction direction) {
36+
return String.format(toStringFormat, fixedOperand.getDescription(context, direction));
4037
}
4138

4239
@Override

src/main/java/net/itarray/automotion/internal/properties/PagePercentage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public Scalar evaluateIn(Context context, Direction direction) {
2020
}
2121

2222
@Override
23-
public String toStringWithUnits(String units) {
24-
return null;
23+
public String getDescription(Context context, Direction direction) {
24+
return String.format("%s%% of page (%spx)", percentage, evaluateIn(context, direction));
2525
}
2626

2727
@Override

src/main/java/net/itarray/automotion/internal/properties/PagePercentageOrPixels.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
public class PagePercentageOrPixels implements Expression<Scalar> {
88

9-
private final ScalarConstant constant;
9+
private final PixelConstant constant;
1010
private final PagePercentage pagePercentage;
1111

1212
public PagePercentageOrPixels(Scalar constant) {
13-
this.constant = new ScalarConstant(constant);
13+
this.constant = new PixelConstant(constant);
1414
this.pagePercentage = new PagePercentage(constant);
1515
}
1616

@@ -43,7 +43,7 @@ public Scalar evaluateIn(Context context, Direction direction) {
4343
}
4444

4545
@Override
46-
public String toStringWithUnits(String units) {
47-
return constant.toStringWithUnits(units);
46+
public String getDescription(Context context, Direction direction) {
47+
return constant.getDescription(context, direction);
4848
}
4949
}

src/main/java/net/itarray/automotion/internal/properties/ScalarConstant.java renamed to src/main/java/net/itarray/automotion/internal/properties/PixelConstant.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import net.itarray.automotion.internal.geometry.Scalar;
55
import net.itarray.automotion.validation.properties.Expression;
66

7-
public class ScalarConstant implements Expression<Scalar> {
7+
public class PixelConstant implements Expression<Scalar> {
88

99
private final Scalar value;
1010

11-
public ScalarConstant(Scalar value) {
11+
public PixelConstant(Scalar value) {
1212
this.value = value;
1313
}
1414

@@ -18,8 +18,8 @@ public Scalar evaluateIn(Context context, Direction direction) {
1818
}
1919

2020
@Override
21-
public String toStringWithUnits(String units) {
22-
return value.toStringWithUnits(units);
21+
public String getDescription(Context context, Direction direction) {
22+
return value.toString() + "px";
2323
}
2424

2525
@Override
@@ -29,10 +29,10 @@ public String toString() {
2929

3030
@Override
3131
public boolean equals(Object object) {
32-
if (!(object instanceof ScalarConstant)) {
32+
if (!(object instanceof PixelConstant)) {
3333
return false;
3434
}
35-
ScalarConstant other = (ScalarConstant) object;
35+
PixelConstant other = (PixelConstant) object;
3636
return value.equals(other.value);
3737
}
3838

0 commit comments

Comments
 (0)