7

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

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74

4 Answers4

2
 @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

anuja jain
  • 1,367
  • 13
  • 19
1

I just solved my problem: instead of using press(), I try longpress() and it works like a charm! I can move seekbar where I want.

Ragnarsson
  • 1,715
  • 7
  • 41
  • 74
0

I tried like this seekbar.sendKeys("50"); and its working for me.(0(min) - 100(max))

Rithesh B
  • 198
  • 1
  • 9
0
@Test
public void seekBar() throws  Exception {
    WebElement seek_bar=driver.findElement(By.id("seek_bar"));

    int start=element.getLocation().getX();
    int end1=element.getSize().getWidth();
    int end=(int)(end1*0.4);

    int y=element.getLocation().getY();

    PointOption point = PointOption.point(start, y);
    PointOption point2 = PointOption.point(end, y);

    TouchAction action=new TouchAction(driver);
    action.press(point).moveTo(point2).release().perform();
}
Antoine
  • 1,393
  • 4
  • 20
  • 26
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 22 '22 at 11:37