How can I scroll in android/ios from top to bottom using appium driver?

  1. I want to scroll an android mobile app page from top to bottom.

  2. I have tried with below defined coding for scroll and click for specific web element using text. It works fine.

    // method 1 driver.scrollTo(“R”);

    // method 2 driver.ScrollToExact(“Top”);

  3. But I need to swipe an full article page from top to bottom, without using above scroll() methods. I have tried with below coding, but scroll action doesn’t happens for me. // scroll to bottom of an page

  4. ((JavascriptExecutor) driver) .executeScript("window.scrollTo(0, document.body.scrollHeight)");
    How can I scroll an android app page from top to bottom using appium driver?

I am writing mobile app automation test for my app.

Steps:-

  1. List of all articles will displayed -
  2. I click on first article In article page, there is a image at center of the article(need to scroll to see full article). Image at centre will appear when we scroll to that position.

I want to assert that image is present or image. To do it, I need to scroll and find.How can i do it?I have tried the following:-

   JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, String> scrollObject = new HashMap<String, String>();
    scrollObject.put("direction", "down");
    js.executeScript("mobile: scroll", scrollObject);

Result:-But appium says as Not yet implemented…

“mobile: scroll” these gestures are deprecated with java client new versions.

driver.swipe, Touch Action class help in scrolling. Below code works for me in android. You can modify it as per your needs

public class TouchActions{

public static AndroidDriver<WebElement> _driver;

@BeforeClass
public void setUpAppium() throws InterruptedException, IOException {
	DesiredCapabilities capabilities = new DesiredCapabilities();   
	AppCapabilitiesSetUp(capabilities);
	_driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4725/wd/hub"), capabilities);
}

@Test
public void installAndLaunchSecondAppTest() throws InterruptedException{
	// Wait for launced app to load
	Thread.sleep(10000);

	// Press Home Key to minimize launched app
	_driver.sendKeyEvent(AndroidKeyCode.HOME);
	Thread.sleep(3000);
	
	// Press Menu button to view all installed app's
	_driver.findElementByAccessibilityId("Apps").click();
	
	// Using Touch Action Classes
	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();
	Thread.sleep(1000);
}

@AfterClass(alwaysRun=true)
public void teardown(){
	_driver.closeApp();
	_driver.quit();
}

public DesiredCapabilities AppCapabilitiesSetUp(DesiredCapabilities cap){
	cap.setCapability(CapabilityType.BROWSER_NAME,"");
	cap.setCapability("platformVersion","4.4.4");
	cap.setCapability("platformName","Android");
	cap.setCapability("deviceName","emulator-5554");
	cap.setCapability("appPackage","appPackage");
	cap.setCapability("appActivity","appActivity");						
	return cap;
	
}

}

I see that you are using AndroidDriver to create your driver.

Can I use the same code to create a driver that can be used to automate an iOS app?

@amitjaincoer191

I have tried the following Scenario1 for scroll a page. Scroll happens at once. Then its get stopped

Scenario 1:-
Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int scrollStart = screenHeightStart.intValue();
System.out.println(“s=”+scrollStart);
Double screenHeightEnd = dimensions.getHeight() * 0.2;
int scrollEnd = screenHeightEnd.intValue();
driver.swipe(0,scrollStart,0,scrollEnd,2000);
sleep(10000);

Scenario2:-

I have tried following for scroll. Scroll is happening till end of page and it keep on scrolling for some time and throws error.
Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int scrollStart = screenHeightStart.intValue();
System.out.println(“s=”+scrollStart);
Double screenHeightEnd = dimensions.getHeight() * 0.2;
int scrollEnd = screenHeightEnd.intValue();
for (int i = 0; i < dimensions.getHeight(); i++) {
driver.swipe(0,scrollStart,0,scrollEnd,2000);
}

I need to stop scrolling when it reach of end of page.

What I need to change here to make it work perfectly.

Is it possible to share ipa file ? I will not share with any one and if able to create working code I will delete it.
Actually I tried scroll of iOS UICatalog using coordinates it didn’t worked.
But if I pass web element to Touch Actions it worked.

@amitjaincoer191 Here is my scenario

Scenario 1:-

  1. There are 20 article in the page
  2. Only 5 articles are displayed. Other articles are displayed when you scroll
  3. How can i scroll with x and y co-ordinates and find an expected article. I want to reach 20th article and click it.
  4. This is list of articles page, Here you see 5 articles are displayed.

Scenario 2:-

  1. Article content is displayed
  2. There are more paragraphs are displayed in an article. In centre of the page , mobile ad(Image) will be displayed.
  3. Mobile Ad(Image) will be displayed only when we scroll to position of centre of an article. I want to verify that image is present or not.
  4. This is a article page, When you scroll, Mobile Ad(image) will be displayed

How can i do it above scenarios?

@amitjaincoer191
I have tried the following Scenario1 for scroll a page. Scroll happens at once. Then its get stopped

Scenario 1:-
Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int scrollStart = screenHeightStart.intValue();
System.out.println(“s=”+scrollStart);
Double screenHeightEnd = dimensions.getHeight() * 0.2;
int scrollEnd = screenHeightEnd.intValue();
driver.swipe(0,scrollStart,0,scrollEnd,2000);
sleep(10000);

Scenario2:-

I have tried following for scroll. Scroll is happening till end of page and it keep on scrolling for some time and throws error.
Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int scrollStart = screenHeightStart.intValue();
System.out.println(“s=”+scrollStart);
Double screenHeightEnd = dimensions.getHeight() * 0.2;
int scrollEnd = screenHeightEnd.intValue();
for (int i = 0; i < dimensions.getHeight(); i++) {
driver.swipe(0,scrollStart,0,scrollEnd,2000);
}

I need to stop scrolling when it reach of end of page.

What I need to change here to make it work perfectly.

You can only conclude that u reach to end of page if u r test case says to click this element

so put terminating condition like

for (int i = 0; i < dimensions.getHeight(); i++) {
driver.swipe(0,scrollStart,0,scrollEnd,2000);
if (driver.findElement(By.name(“YourText”)).size()>0)
exit;
}
driver.findElement(By.name(“YourText”)).click();

so with this for each scroll it will check that element is visible if yes it will come out of loop and click it else it keeps on scrolling.

1 Like

@amitjaincoer191 OK THANKS.

@amitjaincoer191 Consider I have reached end of page. I am trying to do scrolling, control won’t beyond page as it is reached end of page.

I am trying to do driver.swipe method in end of page. How can i say I can able to do swipe or not ?

consider last visible element and remember it. not changed = we reached end.

2 Likes

@amitjaincoer191, can you explain why you’re using startx+20 and moving to endx+20? If i’m not wrong, the +20 indicates the pixels. So, when you’re moving from point A to point B, the coordinates should be (x,y) to (x+20,y+20), is that right?

@lostinhogwarts

It depends how u want to move

(0,0) (0,10) - move 10 units in y direction [vertical up swipe]
(0,20) (0,10) - move 10 units in y direction [vertical down swipe]
(0,0) (10,0) - move 10 units in x direction [horizontal right swipe]
(20,0) (10,0) - move 10 units in x direction [horizontal left swipe]

+20 or -20 from computed coordinates using getLocation.getX() / getLocation.getY() simply means a kind of error removing way some time computed coordinates are on extreme boundary of screen so swipe goes wrong

do not use for loop for (int i = 0; i < dimensions.getHeight(); i++)

@Karunakaran_M and @amitjaincoer191
Scenario 1:- worked for me

Thanks a lot

Hi,

scenario 1 works well for me if the element I am looking for exist.
But if it doesn’t exist how can I stop the for loop when I reach the end of the page, if I don’t know the last element of the page?

More precisely,
I am trying to check if a given element exist in a page. To do that, I use the scenario 1.
It’s perfect if the element is found but if it isn’t found, the scenario 1 continue to run until the end of “for” loop is reached, even if the end of the page is reached.

I can’t know the last element of the page because it’s a list that is specific for each App user and that can be change (add / remove element).

Thanks.

@Mague

Here is the workaround for scroll in Android - https://pioneer2k9.blogspot.in/2016/08/workaround-or-solution-for-deprecated.html

Thank you Karunakaran_M.

I already used the workaround with the swipe method and loop a predetermined number of time to find a specific element.

But I wonder, instead to have a predetermined number of time:
for i=0; i <10; i++,
we can have a way something like:
for i=0; i < “end of page”; i++ OR while(! “end of age”)
where “end of page” is not fix <=> not necessary the same element at the end of the page.

So far, I swipe screen by screen and retrieve the last element text of the current screen, and compare with the previous last element text (of the previous screen).
If it’s the same => I reach the end of the page and stop the loop.

But I am not sure it’s the best way to do that :wink:

Have a nice day

Hi Guys,

driver.scroll();
driver.scrollTo();

not coming when i’m doing driver.

Even for me scenario 1 worked fine…

I am scrolling an overlay not a page !!! Super !