Skip to content

Commit 9081b5c

Browse files
authored
Merge pull request #49 from AutomateThePlanet/bellatrix-opencv-actionimage-update
Bellatrix OpenCV ActionImage Update
2 parents 4371db7 + 52cbd55 commit 9081b5c

File tree

73 files changed

+1762
-80
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1762
-80
lines changed

bellatrix.android/src/main/java/solutions/bellatrix/android/components/AndroidComponent.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
import org.openqa.selenium.interactions.Actions;
2323
import solutions.bellatrix.android.components.contracts.Component;
2424
import solutions.bellatrix.android.configuration.AndroidSettings;
25-
import solutions.bellatrix.android.findstrategies.FindStrategy;
26-
import solutions.bellatrix.android.findstrategies.NameFindStrategy;
27-
import solutions.bellatrix.android.findstrategies.TagFindStrategy;
28-
import solutions.bellatrix.android.findstrategies.XPathFindStrategy;
25+
import solutions.bellatrix.android.findstrategies.*;
2926
import solutions.bellatrix.android.infrastructure.DriverService;
3027
import solutions.bellatrix.android.services.AppService;
3128
import solutions.bellatrix.android.services.ComponentCreateService;
@@ -36,6 +33,7 @@
3633
import solutions.bellatrix.core.utilities.DebugInformation;
3734
import solutions.bellatrix.core.utilities.InstanceFactory;
3835
import solutions.bellatrix.core.utilities.Log;
36+
import solutions.bellatrix.plugins.opencv.Base64Encodable;
3937

4038
import java.awt.Dimension;
4139
import java.util.ArrayList;
@@ -221,6 +219,10 @@ public <TComponent extends AndroidComponent, TFindStrategy extends FindStrategy>
221219
return createAll(componentClass, findStrategy);
222220
}
223221

222+
public <TComponent extends AndroidComponent> TComponent createByImage(Class<TComponent> componentClass, Base64Encodable encodedImage) {
223+
return create(componentClass, new ImageBase64FindStrategy(encodedImage));
224+
}
225+
224226
public <TComponent extends AndroidComponent> TComponent createByXPath(Class<TComponent> componentClass, String xpath) {
225227
return create(componentClass, new XPathFindStrategy(xpath));
226228
}
@@ -237,6 +239,11 @@ public <TComponent extends AndroidComponent> List<TComponent> createAllByName(Cl
237239
return createAll(componentClass, new NameFindStrategy(name));
238240
}
239241

242+
public <TComponent extends AndroidComponent> List<TComponent> createAllByImage(Class<TComponent> componentClass, Base64Encodable encodedImage) {
243+
return createAll(componentClass, new ImageBase64FindStrategy(encodedImage));
244+
}
245+
246+
240247
public <TComponent extends AndroidComponent> List<TComponent> createAllByXPath(Class<TComponent> componentClass, String xpath) {
241248
return createAll(componentClass, new XPathFindStrategy(xpath));
242249
}

bellatrix.android/src/main/java/solutions/bellatrix/android/configuration/AndroidSettings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ public class AndroidSettings {
4141

4242
@Getter @Setter private Boolean videosOnFailEnabled;
4343
@Getter @Setter private String videosSaveLocation;
44+
45+
@Getter @Setter private Boolean allowImageFindStrategies;
4446
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2025 Automate The Planet Ltd.
3+
* Author: Miriyam Kyoseva
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package solutions.bellatrix.android.findstrategies;
15+
16+
import io.appium.java_client.AppiumBy;
17+
import io.appium.java_client.android.AndroidDriver;
18+
import org.openqa.selenium.WebElement;
19+
import solutions.bellatrix.plugins.opencv.Base64Encodable;
20+
21+
import java.util.List;
22+
23+
public class ImageBase64FindStrategy extends FindStrategy {
24+
private final Base64Encodable encodedImage;
25+
public ImageBase64FindStrategy(Base64Encodable encodedImage) {
26+
super(encodedImage.getImageName());
27+
this.encodedImage = encodedImage;
28+
}
29+
30+
@Override
31+
public WebElement findElement(AndroidDriver driver) {
32+
return driver.findElement(AppiumBy.image(encodedImage.getBase64Image()));
33+
}
34+
35+
@Override
36+
public List<WebElement> findAllElements(AndroidDriver driver) {
37+
return driver.findElements(AppiumBy.image(encodedImage.getBase64Image()));
38+
}
39+
40+
@Override
41+
public WebElement findElement(WebElement element) {
42+
return element.findElement(AppiumBy.image(encodedImage.getBase64Image()));
43+
}
44+
45+
@Override
46+
public List<WebElement> findAllElements(WebElement element) {
47+
return element.findElements(AppiumBy.image(encodedImage.getBase64Image()));
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return String.format("image = %s", getValue());
53+
}
54+
}

bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/DriverService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ private static AndroidDriver initializeDriverGridMode(GridSettings gridSettings,
107107
options.put("name", testName);
108108
caps.setCapability(gridSettings.getOptionsName(), options);
109109

110+
if (ConfigurationService.get(AndroidSettings.class).getAllowImageFindStrategies())
111+
caps.setCapability("use-plugins", "images");
112+
110113
AndroidDriver driver = null;
111114
try {
112115
driver = new AndroidDriver(new URL(gridSettings.getUrl()), caps);
@@ -134,6 +137,9 @@ private static AndroidDriver initializeDriverRegularMode(String serviceUrl) {
134137
caps.setAppActivity(getAppConfiguration().getAppActivity());
135138
}
136139

140+
if (ConfigurationService.get(AndroidSettings.class).getAllowImageFindStrategies())
141+
caps.setCapability("use-plugins", "images");
142+
137143
addDriverConfigOptions(caps);
138144
addCustomDriverOptions(caps);
139145
var driver = new AndroidDriver(new URL(serviceUrl), caps);

bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/MobileScreenshotPlugin.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
package solutions.bellatrix.android.infrastructure;
1515

16-
import lombok.SneakyThrows;
17-
import org.apache.commons.io.FileUtils;
1816
import org.openqa.selenium.OutputType;
1917
import org.openqa.selenium.TakesScreenshot;
2018
import plugins.screenshots.ScreenshotPlugin;
@@ -35,7 +33,11 @@ public MobileScreenshotPlugin() {
3533
}
3634

3735
@Override
38-
@SneakyThrows
36+
public byte[] takeScreenshot() {
37+
return ((TakesScreenshot)DriverService.getWrappedAndroidDriver()).getScreenshotAs(OutputType.BYTES);
38+
}
39+
40+
@Override
3941
public String takeScreenshot(String name) {
4042
var screenshotSaveDir = getOutputFolder();
4143
var filename = getUniqueFileName(name);
@@ -57,7 +59,6 @@ public String takeScreenshot(String name) {
5759
}
5860

5961
@Override
60-
@SneakyThrows
6162
public String takeScreenshot(String screenshotSaveDir, String filename) {
6263
var screenshot = ((TakesScreenshot)DriverService.getWrappedAndroidDriver()).getScreenshotAs(OutputType.BASE64);
6364

bellatrix.android/src/main/java/solutions/bellatrix/android/services/ComponentCreateService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import solutions.bellatrix.android.findstrategies.*;
1818
import solutions.bellatrix.android.infrastructure.DriverService;
1919
import solutions.bellatrix.core.utilities.InstanceFactory;
20+
import solutions.bellatrix.plugins.opencv.Base64Encodable;
2021

2122
import java.util.ArrayList;
2223
import java.util.List;
@@ -32,6 +33,10 @@ public <TComponent extends AndroidComponent, TFindStrategy extends FindStrategy>
3233
return allBy(componentClass, findStrategy);
3334
}
3435

36+
public <TComponent extends AndroidComponent> TComponent byImage(Class<TComponent> componentClass, Base64Encodable encodedImage) {
37+
return by(componentClass, new ImageBase64FindStrategy(encodedImage));
38+
}
39+
3540
public <TComponent extends AndroidComponent> TComponent byId(Class<TComponent> componentClass, String id) {
3641
return by(componentClass, new IdFindStrategy(id));
3742
}
@@ -76,6 +81,10 @@ public <TComponent extends AndroidComponent> TComponent byIdContaining(Class<TCo
7681
return by(componentClass, new IdContainingFindStrategy(idContaining));
7782
}
7883

84+
public <TComponent extends AndroidComponent> List<TComponent> allByImage(Class<TComponent> componentClass, Base64Encodable encodedImage) {
85+
return allBy(componentClass, new ImageBase64FindStrategy(encodedImage));
86+
}
87+
7988
public <TComponent extends AndroidComponent> List<TComponent> allById(Class<TComponent> componentClass, String automationId) {
8089
return allBy(componentClass, new IdFindStrategy(automationId));
8190
}

bellatrix.desktop/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@
4848
<version>1.0</version>
4949
<scope>compile</scope>
5050
</dependency>
51+
<dependency>
52+
<groupId>solutions.bellatrix</groupId>
53+
<artifactId>bellatrix.plugins.opencv</artifactId>
54+
<version>1.0</version>
55+
<scope>compile</scope>
56+
</dependency>
5157
</dependencies>
5258
</project>

bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/components/DesktopComponent.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import solutions.bellatrix.desktop.services.ComponentCreateService;
3535
import solutions.bellatrix.desktop.services.ComponentWaitService;
3636
import solutions.bellatrix.desktop.waitstrategies.*;
37+
import solutions.bellatrix.plugins.opencv.Base64Encodable;
3738

3839
import java.awt.Dimension;
3940
import java.util.ArrayList;
@@ -247,6 +248,11 @@ public <TComponent extends DesktopComponent> TComponent createByAutomationId(Cla
247248
return create(componentClass, new AutomationIdFindStrategy(automationId));
248249
}
249250

251+
public <TComponent extends DesktopComponent> TComponent createByImage(Class<TComponent> componentClass, Base64Encodable encodedImage) {
252+
return create(componentClass, new ImageBase64FindStrategy(encodedImage));
253+
}
254+
255+
250256
public <TComponent extends DesktopComponent> List<TComponent> createAllByAccessibilityId(Class<TComponent> componentClass, String accessibilityId) {
251257
return createAll(componentClass, new AccessibilityIdFindStrategy(accessibilityId));
252258
}
@@ -275,6 +281,10 @@ public <TComponent extends DesktopComponent> List<TComponent> createAllByIdConta
275281
return createAll(componentClass, new IdContainingFindStrategy(idContaining));
276282
}
277283

284+
public <TComponent extends DesktopComponent> List<TComponent> createAllByImage(Class<TComponent> componentClass, Base64Encodable encodedImage) {
285+
return createAll(componentClass, new ImageBase64FindStrategy(encodedImage));
286+
}
287+
278288
protected <TComponent extends DesktopComponent, TFindStrategy extends FindStrategy> TComponent create(Class<TComponent> componentClass, TFindStrategy findStrategy) {
279289
CREATING_ELEMENT.broadcast(new ComponentActionEventArgs(this));
280290
findElement();

bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/configuration/DesktopSettings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ public class DesktopSettings {
4747

4848
@Getter @Setter private Boolean videosOnFailEnabled;
4949
@Getter @Setter private String videosSaveLocation;
50+
51+
@Getter @Setter private Boolean allowImageFindStrategies;
5052
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2025 Automate The Planet Ltd.
3+
* Author: Miriyam Kyoseva
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package solutions.bellatrix.desktop.findstrategies;
15+
16+
import io.appium.java_client.AppiumBy;
17+
import io.appium.java_client.windows.WindowsDriver;
18+
import org.openqa.selenium.WebElement;
19+
import solutions.bellatrix.plugins.opencv.Base64Encodable;
20+
21+
import java.util.List;
22+
23+
public class ImageBase64FindStrategy extends FindStrategy {
24+
private final Base64Encodable encodedImage;
25+
public ImageBase64FindStrategy(Base64Encodable encodedImage) {
26+
super(encodedImage.getImageName());
27+
this.encodedImage = encodedImage;
28+
}
29+
30+
@Override
31+
public WebElement findElement(WindowsDriver driver) {
32+
return driver.findElement(AppiumBy.image(encodedImage.getBase64Image()));
33+
}
34+
35+
@Override
36+
public List<WebElement> findAllElements(WindowsDriver driver) {
37+
return driver.findElements(AppiumBy.image(encodedImage.getBase64Image()));
38+
}
39+
40+
@Override
41+
public WebElement findElement(WebElement element) {
42+
return element.findElement(AppiumBy.image(encodedImage.getBase64Image()));
43+
}
44+
45+
@Override
46+
public List<WebElement> findAllElements(WebElement element) {
47+
return element.findElements(AppiumBy.image(encodedImage.getBase64Image()));
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return String.format("image = %s", getValue());
53+
}
54+
}

0 commit comments

Comments
 (0)