Our app doesn't use android.widget.ListView, all elements are loaded in android.view.View, I can see all the elements in the uiautomatorviewer element tree. But I have to swipe screen to access the elements which aren't in visible range. I tried uiautomator scrollintoview method, it does't work for this case.
Hi @Guanhua_Jing,
I have used below code for scroll in my application. When a click to element does not happen, when element is not visible,an exception occurs using this I added this code. Hope this helps you.
js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0,0)");
do
{
try
{
driver.findElement(By.xpath().click();
break;
}
catch(Exception e)
{
js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0, 200)");}
} while(true);
Thanks,
Sujata
Hi Appium_Master
I have tried your code for scrolling in the android app. below is the code, But still not able to scroll :-
HashMap<String, String> scrollObject = new HashMap<String, String>();
RemoteWebElement element = (RemoteWebElement)((AndroidDriver) driver).findElementByAndroidUIAutomator("new UiSelector().className(\"android.view.View\")");
JavascriptExecutor js = (JavascriptExecutor) driver;
String webElementId = ((RemoteWebElement) element).getId();
System.out.println(webElementId);System.out.println(element);
scrollObject.put("text", "Load more");
scrollObject.put("element", webElementId);
js.executeScript("mobile: scrollTo", scrollObject);
element.click();
But i am getting the below exception
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command.
at line
js.executeScript("mobile: scrollTo", scrollObject);
Could you please help me in how to do scrolling in android app.
I am using below versions:-
Appium 1.3.4.1
Selenium 2.45
Java-Client 2.2.0
Thanks in Advance!!
I've found scrolling by co-ordinates helps. In Android you scroll from bottom up to get the scroll working. The best way i've found to work is - either to get the location of an element :
int topY = element.getLocation().getY()
int bottomY = topY + element.getSize().getHeight()
int centerX = element.getLocation().getX() + (element.getSize().getWidth()/2)
driver.swipe(centerX, bottomY, centerX, topY, duration);
Or - get the size of the screen, divide it up and scroll like that -
Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int scrollStart = screenHeighStart.toInt();
Double screenHeightEnd = dmensions.getHeight() * 0.2
int scrollEnd = screenHeightEnd.toInt();
driver.swipe(0,scrollStart,0,scrollEnd,duration); //duration is in milliseconds I believe so go for values like 1000 or 2000
The reason you need to convert the Doubles to ints is because driver.swipe does not accept Double/double values.
I hope this helps.
@Rjimms, Thanks your solution worked well for Android scroll action. I have tried same code for iOS also, but it doesn't worked for me. If you know solution for iOS also, please provide. I am searching for reliable solution for iOS scroll and swipe.
@Rjimms, K Thanks.....
Hi Selvi,
Can you try scroll on iOS app with below strategy...
driver = new IOSDriver(new URL("http://127.0.0.1:4725/wd/hub"), capabilities);
WebDriverWait wait = new WebDriverWait(driver,150);
driver.findElementByAccessibilityId("UICatalog").click();
TouchAction act = new TouchAction(driver);
act.press(driver.findElementByAccessibilityId("Sliders")).moveTo(driver.findElementByAccessibilityId("Alert Controller")).release().perform();
Thread.sleep(4000);
// Just make sure ur locator is correct, area should be scrollable and it should work....
If u have some text in content-desc attribute of Node then above code will work...
Try with className/xpath
@Rjimms, I just want to know , Is it possible to assert certain web element is present after one scroll.
Bhaskar the concept of scroll is same as manuall scroll
U select one point of screen swipe u r finger to another point and screen scrolls and new element appears right similarly
I am scrolling from element with contect-desc property "Sliders" to element with content-desc property "Alert Controller", once this action is done new elements comes into picture if u want to scroll further u need to pull a loop with terminating condition scroll and findElement, if element is found stop scroll (by terminating loop) else continue action...
Hi Rjimms,
This code works fine on both Android and iOS. To make it run on iOS, one has to club XPath with visible property.
public static void iOSSwipe(String xPath) {
do {
try {
driver.findElement(By.xpath(xPath)).click();
break;
} catch (Exception NoSuchElementException) {
Dimension dimensions = driver.manage().window().getSize();
Double screenHeightStart = dimensions.getHeight() * 0.5;
int scrollStart = screenHeightStart.intValue();
Double screenHeightEnd = dimensions.getHeight() * 0.2;
int scrollEnd = screenHeightEnd.intValue();
driver.swipe(0, scrollStart, 0, scrollEnd, 2000);
}
} while (true);
}
Example Call statement - iOSSwipe("//*[@label='Gift Cards' and @visible='true']");
Thanks
The problem I see with this though, is what happens when your object lands above, rather than below? Or more over, what if you don't know if it's above or below? What happens when you have thousands of records on that table? It seems like there should be a more effective method to 'jump' to an object, if we're able to supply the xpath or ID.
Scroll to text :
MobileElement radioGroup = (MobileElement) wd
.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"
".resourceId(\"+listview_id+\")).scrollIntoView("
"new UiSelector().text(\"+text+\"));");
radioGroup.click();
This link will help you : https://www.youtube.com/watch?v=bT3tqaLNn-Y115
@Appium_Master: Could you please share me the code what you have showed in above video.
Also there is no method inside driver.scrollTo() [See attachment screenshot] ..could you please let me know which version of libraries i need to use.
Also my current issue is i need to scroll vertically to find button and need to click on it. Here scroll should not be dependable based on its position.Please suggest!
Currently i'm using java-client latest 5.0 beta version.
Could you clarify how to do scroll down in Windows Universal app.?
Config Setup: Appium 1.6.0 using node
Remote Webdriver
Windows universal app
Tried with JavascriptExecutor, MoveToElement, scrollTo, .... nothing works.Please clarify. Thanks
This awesome Appium document resolved my issues with iOS scrolling and swiping and using both to position to a specific element: https://appium.readthedocs.io/en/stable/en/writing-running-appium/touch-actions/214