How to perform scroll gesture for iOS mobile app in iOS simulator?

  1. I am writing automation app test case for iOS mobile app.
  2. I am using appium version 1.4.8 , iOS simulator 6 and platform version 8.3.
  3. I have seen a weird behavior, When I tried with scroll, it is working for sometime and sometime it doesn’t works for me. Now, completely scroll gesture doesn’t happening at all.
  4. I have tried with below coding for scroll gesture. But, the scroll action doesn’t happens for me.

/ / scroll gestures

driver.scrollTo(Text)
driver.scrollToExact(Text)

These methods are unreliable so it was adviced to use below methods of java_client for scroll in a page

TouchActions
Swipe

@amitjaincoer191, Ok Thanks for your reply. Can you explain, how can I correctly use touch action and swipe for scroll action. I really got confused, how can I proceed scroll gestures to click particular text or to go to down to the bottom of the page using swipe or touch action.

Watch coordinates carefully. Consider you are on app’s page of your phone. then below code will swipe screen left and right using TouchAction class

            TouchAction tAction=new TouchAction(_driver);
	int startx = _driver.findElement(By.name("Email")).getLocation().getX();
	int starty = _driver.findElement(By.name("Email")).getLocation().getY();
	int endx = _driver.findElement(By.name("Dev Settings")).getLocation().getX();
	int endy = _driver.findElement(By.name("Dev Settings")).getLocation().getY();
	System.out.println(startx + " ::::::: " + starty + " ::::::: " + endx +  " ::::::: " +	endy);
	
            //First tap on the screen and swipe it right using moveTo function
	tAction.press(startx+20,starty+20).moveTo(endx+20,endy+20).release().perform(); 
			
	Thread.sleep(1000);
	
	//Second tap on the screen and swipe it left using moveTo function
	tAction.press(endx+20,endy+20).moveTo(startx+20,starty+20).release().perform();

Hi amitjaincoer191,

I have tried the solution suggested by you and it did not work from me. How can we swipe in upward direction?

From what I understand, you can’t. I could be wrong, but I’ve tried about 20 different solutions posted here and elsewhere and nothing has worked for me.

Xcode 7 (and El Capitan) are likely out next month and contains a wholly revamped testing system. When that comes around you should be able to do just about any gesture you want. Hopefully.

@amitjaincoer191, I have tried with above solution. It doesn’t work at all for me.

Hi All,

Actually It worked for me on Android app.
But I feel from code point of view it is independent of platform.
Note : You need to make sure that are we are preforming swipe or scroll should be scrollable
Can you share your error what you are getting and also on which app you have tried ??
scrolling and swiping is just a matter of getting correct from and to coordinates and it will work provided area is scrollable

@amitjaincoer191, I have tried your code for scroll action. I don’t get any error and all, but the scroll action doesn’t happens for me. I want to scroll an page from top to bottom. I need some clarification with above code. How can I take endx and endy web element?

@amitjaincoer191, I have tried the above code for swiping a photo gallery image. But, the swipe action doesn’t happens for me. I have tried this code for iOS app in iOS simulator 8.3 platform version. I have pasted the appium locator details for that particular image. I need to swipe from right to left and vice versa. How can I use the below information to take the web element to obtain startx, starty ,endx and endy.

Details:-
name:
type: UIAImage
value:
label:
enabled: true
visible: false
valid: true
location: {0, 193}
size: {375, 281.25}
xpath: //UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAElement[1]/UIAScrollView[1]/UIAImage[2]

Actually things are based on approximations.

So if I say we have a app Page with an element on top of page and one on bottom of page

elememt.top of page.getX - startX
element.top of page.getY - start Y

element.bottom of page.getX - end X
element.bottom of page.getY - end Y

so depending on coordinates passed swipe or scroll will happen

driver.swipe(0,0,300,0) - this will scroll horizontally right
driver.swipe(300,0,100,0) - this will scroll horizontally left
driver.swipe(0,0,0,300) - this will scroll vertically down
driver.swipe(100,500,100,100) - this will scroll vertically up

same way touchactions also work.

I have seen u r other question also so if you want to use loop to scroll to bottom then u can use loop but u must have a terminating condition for loop …

I have example from android app context in case u r interested I can post it.

I worked less on iOS app but what ever worked I found that absolute xpath returned by inspector very less reliable. so depend on other strategy like classname,relative xpath etc

2 Likes

you means this is a page where u can see thumbnail of images and u want to scroll it.

You can try like this
driver.findElement(By.xpath("//UIImage[1]")) - get its x an y coordinate for start
driver.findElement(By.xpath("//UIImage[5]")) - get its x an y coordinate for end
pass to swipe method

@amitjaincoer191, Thanks for your valuable response. Can you post your android app context? I will give an try.

@amitjaincoer191, Not like that I have a single image, I need to swipe that image and go to the next image. Its like image gallery swiping.

Try like this
// Make sure right view should be present when this code is executed
// perform horizontal swipe from right to left so that next image will appear
int x = driver.manage().window().getSize().getHeight();
int y = driver.manage().window().getSize().getWidth();
driver.swipe(x/3+300, y/2-150, x/3, y/2-150, 2000);

  1. I have tried with above code. It throws below error message.
    Error Message:-
    org.openqa.selenium.WebDriverException: An error occurred while executing user supplied JavaScript. (WARNING: The server did not provide any stacktrace information)

  2. It throws error in below line
    //driver.swipe(x/3+300, y/2-150, x/3, y/2-150, 2000);

You are right scroll gesture is not working when we are passing coordinates we can raise a bug with appium for that

Here is the code on UICatalog app and it works for scrolling. Make sure u r using java client 3.1 version

@Test
public void setUp() throws Exception {
protected static IOSDriver driver;
File app = new File("/Users/Library/Developer/Xcode/DerivedData/UICatalog-axxvcppluaedjmfkuzvxoncaguwk/Build/Products/Debug-iphonesimulator/UICatalog.app");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“platformVersion”, “8.1”);
capabilities.setCapability(“platformName”, “ios”);
capabilities.setCapability(“deviceName”, “iPhone Simulator”);
capabilities.setCapability(“app”,"/Users/amit.ja/Library/Developer/Xcode/DerivedData/UICatalog-axxvcppluaedjmfkuzvxoncaguwk/Build/Products/Debug-iphonesimulator/UICatalog.app");
capabilities.setCapability(“app”, app.getAbsolutePath());

      driver = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4725/wd/hub"), capabilities);
      WebDriverWait wait = new WebDriverWait(driver,150);
      
      driver.findElementByAccessibilityId("UICatalog").click();
      //int x1 = driver.findElementByAccessibilityId("Alert Controller").getLocation().getX();
      //int y1 = driver.findElementByAccessibilityId("Alert Controller").getLocation().getY();
      //int x2 = driver.findElementByAccessibilityId("Sliders").getLocation().getX();
      //int y2 = driver.findElementByAccessibilityId("Sliders").getLocation().getY();
      //System.out.println("x1,y1 " + x1 + "," + y1 + " x2,y2 " + x2 + "," + y2);
      TouchAction act = new TouchAction(driver);
      //act.press(100,550).moveTo(100,100).release().perform();
      act.press(driver.findElementByAccessibilityId("Sliders")).moveTo(driver.findElementByAccessibilityId("Alert Controller")).release().perform();
      Thread.sleep(4000);
    }

@amitjaincoer191, Thanks for your information, I have really got struck with scroll and swipe gestures more than a week. I have tried so many solution for gestures, still its not working. I will upgrade java client version and give a try with above code.

@Selvi_Ranganathan

scrollTo() and scrollToExact() methods work on UIATableView elements which contains UIATableCell elements.
What you need to do is, first find a UIATableView element using
WebElement tableView = driver.findElement(MobileBy.xpath("//UIATableView[3]"))
then do
tableView.scrollTo(“DesiredText”) - and this will take you that element and return you WebElement having text on which you intend to scroll.

@vipuljain33 WebElement doesn’t seem to have scrollTo method in it.