@rajan.singla I have worked this problem by creating a custom keyword of different scroll functions like scrollToObject or scrollNumberOfTimes that allows you to scroll any direction or to different types of objects. Here is an example of a scroll to object where the parameters are the direction you wish to swipe (swiping up will make the device scroll down), the path to the object in the object repository as a string, and the maximum number of times you want to scroll before returning. The function will pass if the object is within view and then you will need another command after the function is run to interact with it.
@Keyword
def scroll_to_Object(String objectPath, String swipeDirection, int maxScrolls ) {
//This function will take in the object we are looking for
// the direction of swiping (will be opposite of scroll direction)
// and number of max scrolls before failing (each scroll about one page)
int deviceHeight = Mobile.getDeviceHeight()
int deviceWidth = Mobile.getDeviceWidth()
int middleWidth = deviceWidth/2
int middleHeight = deviceHeight/2
int buttonPosition = 0
int count = 0
while (count < maxScrolls) {
try {
Mobile.verifyElementVisible(findTestObject(objectPath), 1, FailureHandling.STOP_ON_FAILURE)
KeywordUtil.logInfo('Button is found')
buttonPosition = Mobile.getElementTopPosition(findTestObject(objectPath), 2, FailureHandling.STOP_ON_FAILURE)
if (buttonPosition < deviceHeight ){
return
}
} catch (Exception ex) {
switch (swipeDirection) {
case 'up':
Mobile.swipe(10, middleHeight, 10, 10)
break
case 'down':
Mobile.swipe(10, middleHeight, 10, (deviceHeight-10))
break
case 'right' :
Mobile.swipe(10, 10, middleWidth, 10)
break
case 'left' :
Mobile.swipe((deviceWidth-10), 10, middleWidth, 10);
break
}
count ++
}
}
KeywordUtil.logInfo('Object not found in max amount of scrolls')
}
Thanks a lot, @kreno for this function! The function runs perfectly fine. For the past 2 days, I’m struggling to find the solution to this but now this function works like a charm. Thanks a lot!
I am actually unable to understand this function. As you have mentioned:
if (buttonPosition < deviceHeight ){
return
}
In my case this condition is true, If this condition is true then how the swipe function is working to scroll down to that objectPath? Because the Mobile.swipe functions are written in the catch block.
That is supposed to break out of the function early because the button is already on screen. If the button is already on screen then there shouldn’t be a need to scroll to it. The command after it should be the one to interact with it. The function should always succeed but may hit the max scrolls and return with the log line and will wait for the next command. So after the function is in your case it sounds like you want to tap on the object after scrolling so you would need a tap command:
If the object is not in view and you have hit the maximum amount of scrolls it will return with the log line and the next command that tries to interact with the object you are searching for will be how the test fails.