I am using appium javascript client with uiautomator2 as the automation engine for android. In one of screen having multiple buttons like Button 1, Button 2, Button 3 ...etc stacked horizantal with scroll. However, only first two buttons will be visible on page load [ Refer Screen shot].
I want to click on button 4. Have implemented below code to handle scroll and click button but I am unable to break loop since attribute is always false.
var element = await driver.elementByXPath("//android.widget.Button[contains(@text,'Button 4')]");
var scrollFromElement = await driver.elementByXPath("//android.widget.Button[contains(@text,'Button 2')]");
var isEleFocused = await element.getAttribute("focused");
console.log("concierge element is Focused - " + isEleFocused);
if (isEleFocused == "false")
{
var i = 0;
do{
await swipeElementAndroid(scrollFromElement, "RIGHT")
var d1 = await element.getAttribute("focused");
if(d1 == "true"){
i=10
break;
}
else{
await swipeElementAndroid(scrollFromElement, "RIGHT")
i=i+1
console.log("i="+i)
}
}while(i<5)
}
await sleepwait(500)
console.log("after scroll- if condition")
e1 = await driver.elementByXPath("//android.widget.Button[contains(@text,'Button 4')]")
await e1.click()
Kindly suggest alternative way to handle the same.
Note: Attributes like displayed, clickable are always true even though button is not visible.