Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't move Android SeekBar with Appium

I have a customized Android seekbar like this, and the positions where it can move to. And it starts from the middle:

enter image description here

I want to move the slider first and then check if it's saved. I have a method which I use TouchAction:

public void moveSeekBar(){
    WebElement seekBar = appiumDriver.findElementById("com.feverapp:id/SymptomTrackingActivity_var");
    //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 = 786;
    System.out.println("Moving seek bar at " + moveToXDirectionAt+" In X direction.");
    //Moving seekbar using TouchAction class.
    TouchAction act=new TouchAction(appiumDriver);
    act.press(startX,yAxis).moveTo(moveToXDirectionAt,yAxis).release().perform();
}

Something I notice that:

  • seekbar doesn't move at all
  • getX and getY are always the same values even I tried to set slider back to very left before I call this method

I tried with sendkeys like this:

seekbar.sendKeys("0.5");

and it worked: it moved the slider to the very left, and it only worked with 0.5, when I entered a number within the range from 0 to 1, it didn't work

Any help much appreciated. Thanks

like image 485
Ragnarsson Avatar asked Mar 04 '16 15:03

Ragnarsson


1 Answers

 @Test
    public void testSeekBar()throws  Exception
    {
           //Locating seekbar using resource id
            WebElement seek_bar=driver.findElement(By.id("seek_bar"));
            // get start co-ordinate of seekbar
            int start=seek_bar.getLocation().getX();
            //Get width of seekbar
            int end=seek_bar.getSize().getWidth();
            //get location of seekbar vertically
            int y=seek_bar.getLocation().getY();

        // Select till which position you want to move the seekbar
        TouchAction action=new TouchAction(driver);

        //Move it will the end
        action.press(start,y).moveTo(end,y).release().perform();

        //Move it 40%
        int moveTo=(int)(end*0.4);
        action.press(start,y).moveTo(moveTo,y).release().perform();

    }

This worked for me very well

like image 196
anuja jain Avatar answered Oct 05 '22 03:10

anuja jain