Skip to content

Commit 21c170b

Browse files
committed
Add possibility to change the threshold for one ByImage locator, with border value check
1 parent 108f288 commit 21c170b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ByImage extends By {
2525
private static boolean wasLibraryLoaded = false;
2626
private final Mat template;
2727
private final String description;
28+
private float threshold = 1 - AqualityServices.getConfiguration().getVisualizationConfiguration().getDefaultThreshold();
2829

2930
private static void loadLibrary() {
3031
if (!wasLibraryLoaded) {
@@ -56,6 +57,27 @@ public ByImage(byte[] bytes) {
5657
this.template = Imgcodecs.imdecode(new MatOfByte(bytes), Imgcodecs.IMREAD_UNCHANGED);
5758
}
5859

60+
/**
61+
* Sets threshold of image similarity.
62+
* @param threshold a float between 0 and 1, where 1 means 100% match, and 0.5 means 50% match.
63+
* @return current instance of ByImage locator.
64+
*/
65+
public ByImage setThreshold(float threshold) {
66+
if (threshold < 0 || threshold > 1) {
67+
throw new IllegalArgumentException("Threshold must be a float between 0 and 1.");
68+
}
69+
this.threshold = threshold;
70+
return this;
71+
}
72+
73+
/**
74+
* Gets threshold of image similarity.
75+
* @return current value of threshold.
76+
*/
77+
public float getThreshold() {
78+
return threshold;
79+
}
80+
5981
@Override
6082
public String toString() {
6183
return String.format("ByImage: %s, size: (width:%d, height:%d)", description, template.width(), template.height());
@@ -84,7 +106,6 @@ public List<WebElement> findElements(SearchContext context) {
84106
Mat result = new Mat();
85107
Imgproc.matchTemplate(source, template, result, Imgproc.TM_CCOEFF_NORMED);
86108

87-
float threshold = 1 - AqualityServices.getConfiguration().getVisualizationConfiguration().getDefaultThreshold();
88109
Core.MinMaxLocResult minMaxLoc = Core.minMaxLoc(result);
89110

90111
int matchCounter = Math.abs((result.width() - template.width() + 1) * (result.height() - template.height() + 1));

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ public void testByImageLocator() {
4747
Assert.assertEquals(docLabels.size(), childLabels.size(), "Should be possible to find child elements by image with the same count");
4848

4949
ILabel documentByTag = AqualityServices.getElementFactory().getLabel(By.tagName("body"), "document by tag");
50-
ILabel documentByImage = AqualityServices.getElementFactory().getLabel(new ByImage(documentByTag.getElement().getScreenshotAs(OutputType.BYTES)),
50+
float fullThreshold = 1;
51+
ILabel documentByImage = AqualityServices.getElementFactory().getLabel(new ByImage(documentByTag.getElement().getScreenshotAs(OutputType.BYTES)).setThreshold(fullThreshold),
5152
"body screen");
5253
Assert.assertTrue(documentByImage.state().isDisplayed(), "Should be possible to find element by document screenshot");
54+
Assert.assertEquals(((ByImage)documentByImage.getLocator()).getThreshold(), fullThreshold, "Should be possible to get ByImage threshold");
5355
Assert.assertEquals(documentByImage.getElement().getTagName(), "body", "Correct element must be found");
5456
}
5557

0 commit comments

Comments
 (0)