Reputation: 1086
I am new to appium.
I am learning appium with automation of shopping app (Flipkart).
I am trying to select element 5th element from "Recommended discount for You" list.
In order to do that I have to make horizontal scroll to reach out to that element. But I am having problem get that container element in which elements of "Recommended discount for You" is present so I can fetch the 5th element
.
I guess the problem is UI of flipkart always getting change.
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(3000);
driver.scrollToExact("Recommended Offers for You");
Thread.sleep(10000);
// Getting error on below line
AndroidElement ele = (AndroidElement) driver.findElement(By.xpath("//android.support.v7.widget.RecyclerView[@index='1']"));
for(int i=0;i<4;i++)
{
Thread.sleep(2000);
if (ele.findElement(By.xpath("android.widget.RelativeLayout[@index='2']")).isDisplayed())
{
ele.findElement(By.xpath("android.widget.RelativeLayout[@index='2']")).click();
break;
}
else
{
horizontalScroll(ele);
}
}
public void horizontalScroll(AndroidElement ele)
{
Dimension size=driver.manage().window().getSize();
int x_start=(int)(size.width*0.60);
int x_end=(int)(size.width*0.30);
int y=130;
ele.swipe(SwipeElementDirection.RIGHT,x_end,y,4000);
}
I am using uiautomator
to find elements
. Here is the screenshot of UI.
I have following questions:
Please Help! Thanks in advance.
Upvotes: 2
Views: 9788
Reputation: 1387
If you have multiple UI elements with same resource id you can make use of index along with resource-id.
WebElement element=driver.findElements(By.id("your_id")).get(1);
Upvotes: 1
Reputation: 659
In android if the view is any kind of list view , all the elements will have same id , so in that case u can use the name to tap on it or u can create xpaths and use it to perform ur actions
Upvotes: 0
Reputation: 381
Best(or rather the fastest) way to locate an element in the app is using the resource-id. If the resource-id is unavailable/same you have to locate it using xpath
If you want to Learn Appium you can start by following these tutorials by Mukesh Otwani here is the link
If the UI keeps updating the only way tackle this is keep updating your script with the changes.
Hope it helps!
Upvotes: 2
Reputation: 1881
In here, I see the element has a resource-id. Why not use that ?
What is best way to find elements if UI of app is as displayed. Always, finding element by resource id is a preferred method. Because, it finds the element faster than xpath. Also, if there is an element added in between, the xpath varies, because the element in the hierarchy changes.
Which app should I automate in order to learn appium. Any native app, to begin with use appium provided UICatalog for iOS, and apps from playstore for android.
How to automate app if UI of app is always getting change Scalable solution to automate UI app involves creating page objects, maintaining locators in one place and not spread out through out the test cases etc. It depends on the magnitude of change, is it the navigation that has changed, the element hierarchy that has changed etc. The resource id based selection is always preferred, which sort of reduces the change in automation script, if there is a layout change in the app. But I am afraid there is no single method to tackle this issue.
Upvotes: 1