How to swipe "android.widget.SeekBar"

The app that im testing has a android.widget.SeekBar which has to be swiped to the right until a value in a textbox reaches 5000. I am using appium in java.

I detect the element by → MobileElement element = (MobileElement) adriver.findElement(By.xpath(“//android.widget.SeekBar[1]”));

Please assist me on this.
Thanks in advance

1 Like

I had used selendroid mode, i had tried with driver.findElement(“android.widger.SeekBar”).sendKeys(“0.5”);
So that swipe till mid of seek bar.
But that did not work if i privide other values (“0.3”).

Thanks,
Priyank Shah

3 Likes

What have you tried and where are you running into issues?

First, as a general rule, I use xPath locators as a last resort, they make for “brittle” automation code that can break if the app layout changes. You’re better off using uiAutomatorViewer or the Appium app Inspector to find the element by either AccessibilityId or Id, if you possibly can.

If you’re having issues with the swiping code, I’ve written some examples that might be useful to you here:

1 Like

I’m having the same issues as Priyank.

I am using C#, for Android 4.4.2, and Appium v1.3.4, I can find the SeekBar web element, but cannot slide it. I have tried both:

seekBar.sendKeys([numbers ranging from 0 to 1]) 

and

Action dragDrop = new TouchAction();
dragDrop.DragAndDrop(seekBar.Location, new Location(){seekBar.Location.X + (seekBar.Size.Width / 2), seekBar.Location.Y + (seekBar.Size.Height / 2)});
dragDrop.Perform();

Neither method works. We do not want to use XPath as it is brittle so that’s a no-go for us.

Please help, thanks!

1 Like

Please refer this link on how to automate seek bar.

You can also use the following code to automate seek bar

WebElement seekBar = driver.findElement(By.id("com.cj.scrolling:id/seekbar"));
//Get start point of seekbar.
int startX = seekBar.getLocation().getX();
System.out.println(startX);
//Get end point of seekbar.
    int endX = seekBar.getSize().getWidth();
    System.out.println(endX);
    //Get vertical location of seekbar.
    int yAxis = seekBar.getLocation().getY();
    //Set slidebar move to position.
    // this number is calculated based on (offset + 3/4width)
    int moveToXDirectionAt = 1000 + startX;
    System.out.println("Moving seek bar at " + moveToXDirectionAt+" In X direction.");
    //Moving seekbar using TouchAction class.
    TouchAction act=new TouchAction(driver);
    act.longPress(startX,yAxis).moveTo(moveToXDirectionAt,yAxis).release().perform();