I want to scroll an android mobile app page from top to bottom.
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”);
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
((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:-
List of all articles will displayed -
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:-
“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 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.
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.
@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?
(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
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).
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.