'Appium GUI testing java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds
I have been using this code successfully until I have to screenshot other smaller images within an image. what it does is to screenshot an image using Appium, and get the background color of the image. It's been working brilliantly until I begin to take screenshots of things like buttons and tried to process them to get the color, I run into errors.
public static void checkBackgroundColor(String mobileElement, String saveReadFile, String hexValue) throws IOException {
MobileElement elem = (MobileElement) getMobileDriver().findElement(By.xpath(mobileElement));
File scrFile = elem.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(saveReadFile));
BufferedImage image = ImageIO.read( new File(saveReadFile));
org.openqa.selenium.Point point = elem.getCenter();
int centerx = point.getX();
int centerY = point.getY();
// Getting pixel color by position x and y
int clr= image.getRGB(centerY,centerx);
int r = (clr & 0x00ff0000) >> 16;
int g = (clr & 0x0000ff00) >> 8;
int b = clr & 0x000000ff;
String hex = String.format("#%02x%02x%02x", r, g, b);
// softAssertion.assertEquals(hex, hexValue);
Assert.assertEquals(hex, hexValue);
if (hex.equals(hexValue)){
System.out.println("The color matches standard = "+ hex);
MyLogger.info("The color matches standard = "+ hex);
} else {
System.out.println("This is the hex value of rgb = "+ hex);
MyLogger.error("This is the hex value of rgb = "+ hex);
}
}
This is the error I'm getting
java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at java.desktop/sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:313)
at java.desktop/java.awt.image.BufferedImage.getRGB(BufferedImage.java:917)
at nl.sample.utils.Utils.checkBackgroundColor(Utils.java:335)
at nl.sample.steps.mobile.onboarding.UIOnboardingSteps.colorBBOverslaan(UIOnboardingSteps.java:141)
at nl.sample.mobile.FinanceAdditionTest.buttonFontOnboarding(FinanceAdditionTest.java:660)
at nl.sample.cucumber.steps.FinanceSteps.onboardingButtonFontCheck(FinanceSteps.java:88)
at ✽.Onboarding button font check(file:///Users/kadeoye/IdeaProjects/project/sample-test-ui/src/test/java/nl/testing/cucumber/feature/CoverPage.feature:46)
I have tried a couple of solutions on SO but none worked.
Solution 1:[1]
Out of bound exceptions means the points where the check is made for the pixels is out does not exist. So what I did is to make sure the size coordinates are resized to where it is sure it can find the pixels. This is the solution I used.
public static void checkBackgroundColor(String mobileElement, String saveReadFile, String hexValue) throws IOException {
MobileElement elem = (MobileElement) getMobileDriver().findElement(By.xpath(mobileElement));
File scrFile = elem.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(saveReadFile));
BufferedImage image = ImageIO.read( new File(saveReadFile));
org.openqa.selenium.Point point = elem.getCenter();
org.openqa.selenium.Point location = elem.getLocation();
Dimension size = elem.getSize();
MyLogger.info("Point X is " + point.getX() +" Point Y is " + point.getY());
MyLogger.info("Location X is " + location.getX() +" Location Y is " + location.getY());
MyLogger.info("Size Height is " + size.getHeight() +" Size width is " + size.getWidth());
// Getting pixel color by position x and y
int h = (int) Math.round(size.getWidth() * 0.2);
int w = (int) Math.round(size.getHeight() * 0.8);
int clr= image.getRGB(h , w);
int r = (clr & 0x00ff0000) >> 16;
int g = (clr & 0x0000ff00) >> 8;
int b = clr & 0x000000ff;
String hex = String.format("#%02x%02x%02x", r, g, b);
if (hex.equalsIgnoreCase(hexValue)){
MyLogger.info("Actual value "+ hexValue +" is equals to the expected value " +hex);
} else {
MyLogger.info("Actual value "+ hexValue +" is is not equals to the expected value " +hex);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ken4ward |
