Sinisa Brzak
Sinisa Brzak

Reputation: 123

Screenshot doesn't match coordinates and size of element for iOS app

I'm using a Appium methods to take a screenshot and crop a perticular part of that screenshot according to coordinates and size of an element.

The way I do this:

Take a screenshot

This is done with getScreenShotAs() method


Crop out the part of that image

This is done

        image.getSubimage(getElementCoordinateX(element),
                          getElementCoordinateY(element),
                          getElementWidth(element),getElementHeight(element));

        public static int getElementWidth(MobileElement element) {
            return element.getSize().getWidth();
        }
        public static int getElementHeight(MobileElement element) {
            return element.getSize().getHeight();
        }
        public static int getElementCoordinateX(MobileElement element) {
            return element.getLocation().getX();
        }
        public static int getElementCoordinateY(MobileElement element) {
            return element.getLocation().getY();
        }

I tested this approach on Android and it works as intended, but on iOS it crops out totally different part of the screenshot and I'm sure that it's the right element that's being located.

Developers told me that iOS apps work with frames and that I'm probably getting the bounds coordinates and not the frame's coordinates. I didn't find a way to interact with them using Appium. Is there a way to make this work as intended?

Upvotes: 0

Views: 824

Answers (1)

Cepin
Cepin

Reputation: 26

you can get screenshot of element directly using: Ruby:

element = driver.find_element(:predicate, "type == 'XCUIElementTypeSlider'")
screenshot = driver.element_screenshot_as(element ,:base64)

This should crop the screenshot automatically and correctly...

Upvotes: 1

Related Questions