Element location (Mac2Driver) #2378
-
|
I'm trying to crop a screenshot to include only the application window's bounding box: WebElement el = driver.findElement(AppiumBy.xpath("//XCUIElementTypeWindow"));
byte[] img = driver.getScreenshotAs(OutputType.BYTES);
BufferedImage fullImg = ImageIO.read(new ByteArrayInputStream(img));
Point eleLocation = el.getLocation();
Dimension eleSize = el.getSize();
BufferedImage croppedImg = fullImg.getSubimage(eleLocation.getX(), eleLocation.getY(), eleSize.getWidth(),
eleSize.getHeight());
ImageIO.write(croppedImg, "png", new File(screenshotPath));While the eleSize seems plausible, the eleLocation does not seem to relate to the actual position of the window on the screen. Top of the XCUI tree showing the Window element I'm trying to use to get position: <?xml version="1.0" encoding="UTF-8"?>
<XCUIElementTypeApplication elementType="2" identifier="" label="" title="MyApp" enabled="false" selected="false" x="0" y="0" width="0" height="0">
<XCUIElementTypeWindow elementType="4" identifier="" label="" title="MyApp" enabled="false" selected="false" x="1185" y="457" width="989" height="927">
<XCUIElementTypeGroup elementType="3" identifier="" label="" title="" enabled="true" selected="false" x="1185" y="484" width="990" height="900">
...
Apple docs (https://developer.apple.com/documentation/xcuiautomation/xcuielementattributes/frame) say the location is "The frame of the element in the screen’s coordinate space." which implies that what I'm doing "should work"? Am I using getLocation()/getSize() correctly? If not the screen, what "bounding box" are these values relative to? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
I believe it would be more reliable to get element screenshot instead of trying to cut it off from the display screenshot. Example code: |
Beta Was this translation helpful? Give feedback.
-
|
You are, of course, correct! It works as expected. THANK YOU! |
Beta Was this translation helpful? Give feedback.
I believe it would be more reliable to get element screenshot instead of trying to cut it off from the display screenshot. Example code: