I am getting some Images from the API and i don't know the number and now i want to test the UI in Android through Appium and i want to scroll down to the Last image. How can I do this and also I don't know the what the title from the API is coming so that I can ScrollTo("Title") and also i am not able to swipe to the last. Is there anyway?
There is no way to know for sure whether you've scrolled to the last using appium because there is no UI reporting for the edges of a scrollview.
One way to know that you've reached the end of a scroll view without relying on devs is to compare the list of all children of the scrollview every time you swipe. If all children are exactly the same, you've reached the end. An example xpath for this would look like //android.widget.View[@content-desc="Your scrollview]//*
, which would grab all children and descendants. Once you have a list to compare, check the content of all children nodes. This will only work if there's something unique in these items. If all items are completely general, there will be nothing to compare and this won't be reliable. Ask devs to add content descriptions or accessibility data to the items if possible.
Another option would be to have the devs embed a uniquely id'd invisible view at the top and bottom of the scrollview. That way, if it can be found by the driver, you know that you've reached the very edge of the view. If there are already unique elements at the edges of your scrollview, you can use those.
In the end, the devs of the application can really help out the process of scrolling, but hopefully the trick of comparing the current scrollview's children can help you.
You can use the screen dimensions to scroll down:
public void scrollDown() {
Dimension size = driver.manage().window().getSize();
int x = size.getWidth() / 2;
int starty = (int) (size.getHeight() * 0.60);
int endy = (int) (size.getHeight() * 0.10);
driver.swipe(x, starty, x, endy, 2000);
}
scrollDown
to ensure you have reached the end of the scroll view displayed? –
Anywise You can store all the images into a list using its available locator. Then use driver.scrollToExact(list.get(list.size()).getAttribute("name"));
Example:
List<mobileElement> images = driver.findElementsByClass("<locator>");
driver.scrollToExact(images.get(images.size()).getAttribute("name"));
or
driver.scrollToExact(images.get(images.size()).getText());
@Test
public void testScroll()throws Exception
{
for(int i=0;i<4;i++)
{
Thread.sleep(2000);
if (driver.findElement(By.name("end_item")).isDisplayed())
{
driver.findElement(By.name("end_item")).click();
break;
}
else
{
horizontalScroll();
}
}
}
public void verticalScroll()
{
size=driver.manage().window().getSize();
int y_start=(int)(size.height*0.60);
int y_end=(int)(size.height*0.30);
int x=size.width/2;
driver.swipe(x,y_start,x,y_end,4000);
}
The above example works with vertical scroll and it is based on the example given at this blog for horizontal scroll http://qaautomated.blogspot.in/2016/02/how-to-do-horizontal-scroll-in-appium.html I hope this works for you.
To do this you must know resource id or cont-desc of scrollable element. You also need to know className of your scrollable element.
If you have cont-desc in scrollable list
try {
String scrollableList="your con-desc of scrollable List";
String elementClassName="android.something.something";
String anyText="any text";
driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().description(\"" + scrollableList + "\")).getChildByText("
+ "new UiSelector().className(\"" + elementClassName + "\"), \"" + anytext + "\")"));
}catch (Exception e){
System.out.println("Cannot scroll further");
}
If you have resource-id in scrollable list
try {
String scrollableList="your con-desc of scrollable List";
String elementClassName="android.something.something";
String anyText="any text";
driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().resourceId(\"" + scrollableList + "\")).getChildByText("
+ "new UiSelector().className(\"" + elementClassName + "\"), \"" + anytext + "\")"));
}catch (Exception e){
System.out.println("Cannot scroll further");
}
If the screen cannot be scroll further it will throw error which will be catched by catch block.
© 2022 - 2024 — McMap. All rights reserved.