Skip to content

Commit 108f288

Browse files
committed
Implement equals and toString properly, fix javadoc
1 parent 67265e7 commit 108f288

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/main/java/aquality/selenium/elements/interfaces/ByImage.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
public class ByImage extends By {
2525
private static boolean wasLibraryLoaded = false;
2626
private final Mat template;
27+
private final String description;
2728

2829
private static void loadLibrary() {
2930
if (!wasLibraryLoaded) {
@@ -40,24 +41,43 @@ private static void loadLibrary() {
4041
*/
4142
public ByImage(File file) {
4243
loadLibrary();
44+
description = file.getName();
4345
this.template = Imgcodecs.imread(file.getAbsolutePath(), Imgcodecs.IMREAD_UNCHANGED);
4446
}
4547

4648
/**
47-
* Constructor accepting image file.
49+
* Constructor accepting image bytes.
4850
*
4951
* @param bytes image bytes to locate element by.
5052
*/
5153
public ByImage(byte[] bytes) {
5254
loadLibrary();
55+
description = String.format("bytes[%d]", bytes.length);
5356
this.template = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.IMREAD_UNCHANGED);
5457
}
5558

5659
@Override
5760
public String toString() {
58-
return "ByImage: " + new Dimension(template.width(), template.height());
61+
return String.format("ByImage: %s, size: (width:%d, height:%d)", description, template.width(), template.height());
5962
}
6063

64+
@Override
65+
public boolean equals(Object o) {
66+
if (!(o instanceof ByImage)) {
67+
return false;
68+
}
69+
70+
ByImage that = (ByImage) o;
71+
72+
return this.template.equals(that.template);
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
return template.hashCode();
78+
}
79+
80+
6181
@Override
6282
public List<WebElement> findElements(SearchContext context) {
6383
Mat source = getScreenshot(context);

src/test/java/tests/integration/LocatorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void testByImageLocator() {
4848

4949
ILabel documentByTag = AqualityServices.getElementFactory().getLabel(By.tagName("body"), "document by tag");
5050
ILabel documentByImage = AqualityServices.getElementFactory().getLabel(new ByImage(documentByTag.getElement().getScreenshotAs(OutputType.BYTES)),
51-
"full screen");
51+
"body screen");
5252
Assert.assertTrue(documentByImage.state().isDisplayed(), "Should be possible to find element by document screenshot");
5353
Assert.assertEquals(documentByImage.getElement().getTagName(), "body", "Correct element must be found");
5454
}

0 commit comments

Comments
 (0)