Search code examples
javaandroidiosappium

What is the X and Y axis format on an iPhone screen/Android screen?


I've been researching the X and Y axis set up on an iPhone screen because I'm trying to use Appium to run automated tests on an application. Part of what I'm working on is swiping but I'm running into issues with moving points, X and Y points.

I don't know what the limits of the X and Y axis are on the iPhone screen or the Android screen. Due to that I'm not sure sure where my cursor is swiping from or to.

So far I've been able to do one swipe via the code below,

TouchAction action1 = new TouchAction((PerformsTouchActions) driver);
        action1.press(PointOption.point(250,200))
                .waitAction(new WaitOptions().withDuration(Duration.ofMillis(250))) //you can change wait durations as per your requirement
                .moveTo(PointOption.point(50, 250))
                .release()
                .perform();

        Thread.sleep(5000);

This runs one successful swipe to the left but the 2nd swipe, which runs successfully, but doesn't swipe the screen to the left. As in, the swipes it, but it doesn't move it past the required threshold to move it to the next page.

Let it be known that the above code is placed within a for loop as shown below.

 for(int i = 0; i <= 3; i++){
        TouchAction action1 = new TouchAction((PerformsTouchActions) driver);
        action1.press(PointOption.point(250,200))
                .waitAction(new WaitOptions().withDuration(Duration.ofMillis(250))) //you can change wait durations as per your requirement
                .moveTo(PointOption.point(50, 250))
                .release()
                .perform();

        Thread.sleep(5000);

    }

Is there any documentation for iPhones or Androids that shows the set up of the X and Y axis, the limitations and so on etc

Edit:

The first piece of code is placed outside of the for loop. I believe that was the piece of code that swiped the screen properly. Once the for loop is entered, the screen is unable to swipe properly. I believe I'm not using the correct syntax in the for loop. If anyone has any advice on how to properly incorporate the swipe feature into the for that would be of great help.


Solution

  • X, Y coordinate depend on the device/emulator you are using for automation if device has large display X,Y value will be high.

    I will recommend never use hardcoded value of X, Y during swipe or scrolling with the help of coordinate as it can throw exception points are out of device whenever you will be shifted on small screen Device .

    Please have a look how you can get dynamic X,Y value with the help of Device’s height and width while scrolling to bottom

    public void scrollToBottom() {
    
    // we are  scrolling to bottom so X will be constant so we are taking mid point in width. 
          int  x = driver.manage().window().getSize().width / 2;
    
    // starting of Y is from 20% of height as we have one bar in all device for showing network and battery status
          int start_y = (int) (driver.manage().window().getSize().height * 0.2);
    
    // end of Y is 80% of height
          int end_y = (int) (driver.manage().window().getSize().height * 0.8);
    
    // here scrolling length is (80% -20%) 60%.
            TouchAction dragNDrop = new TouchAction(driver)
                            .press(PointOption.point(x,start_y)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
                            .moveTo(PointOption.point(x, end_y))
                            .release();
            dragNDrop.perform();
        }
    

    If you want to get X,Y coordinate of any Mobile Element you can get as mentioned below

    MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
    Point location = element.getLocation();
    

    OR

    int leftX = element.getLocation().getX();
    int rightX = leftX + element.getSize().getWidth();
    int middleX = (rightX + leftX) / 2;
    int upperY = element.getLocation().getY();
    int lowerY = upperY+element.getSize().getHeight();
    int middleY = (upperY + lowerY) / 2;