In my
last blog, we discussed how can we perform the actions on the app like clicking or filling the fields, like Login & Password. It was pretty interesting, right? Okay, But there are way more actions that can be performed with an app than what we did last time. In this blog, we are going to see how can we perform other actions such as Dragging, Scrolling over an app using appium.
Let’s start with Dragging, i.e handling the SeekBar. There are some scenarios, where we need to tackle the SeekBar, like to adjust the Volume, Brightness etc. To use this functionality we need to use the method provided by the appium driver called “LongPress()”. Well, that’s not enough. As we are aware of the functionality of the “long press”. It means just pressing the left click button of the mouse(on some object) and not releasing it for while. But that doesn’t drag the SeekBar to a particular position at all. To accomplish that, we definitely need to add some extra stuff. Like, providing the details(i.e., start and end points) to the driver in order to perform the drag operation.
In this example, we are going to see how to modify the range of distance in Tinder App. Say it’s 2 KM at present and I want to modify it to somewhere around 45-50 Km.
Well, to accomplish that, First we need to locate that location at the seek bar and clicking that seek bar. Which can be done by the below code, make sure you are using the correct ID of the element.
WebElement seekbar_distance = driver.findElementById("com.tinder:id/seekBar_distance");
seekbar_distance.click();
Now, we need to drag that pointer(over the seek bar). To do that, we need to make some arrangements. Like, setting the start, end and the distance that has to be cover by that pointer.
Once the pointer reached the desired position, it has to release the click and hence the longPress() is used in the association of two extra methods, “release().perform()”. There will be one more method, i.e., moveTo(int, int), It will actually move/drag that pointer on the seek bar to get the things done.
You can find the below code for the reference. I am sure it will work(on Android).
//Get width of the seekbar
int start = seekbar_distance.getLocation().getX();
int end = seekbar_distance.getSize().getWidth();
//Get height of the seekbar
int y = seekbar_distance.getLocation().getY();
//Select till which position you want to move the seekbar
TouchAction action = new TouchAction(driver);
int moveTo = (int)(end * 0.8); // 0.8 signifies that the 80% percent of the total length
action.longPress(start,y).moveTo(moveTo,y).release().perform();
System.out.println("Successfully moved !!!");
In case, you get any issue wit the code or anything else, shoot your queries or
doubts below in the comment section.
Have a Good Day !!