Thursday, 11 February 2016

How to Move SeekBar with Appium ?



In android SeekBar is very cool UI element which not only looks good but allows user to slide with touch. It has various use cases and test cases. In this post we will learn to to test SeekBar using Appium with a simple example. I have created a sample apk to show a demo which contains only a seekbar which you can slide till whatever position you want. Let us move seekbar with appium test.

Check app's screenshot and functionality which we will be using for testing.
Movinf Seekbar in Appium
Appium Seekbar Example

The test case which we are writing now is to test moving SeekBar till The end and moving SeekBar till 40% of its width. Check the test case carefully.

Video Tutorial -
 


@Before
    public void testCaseSetup()throws  Exception
    {

        DesiredCapabilities cap=new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
        cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "4000");
        cap.setCapability(MobileCapabilityType.APP, "c://apks//seekbarsample.apk");
        cap.setCapability("noReset", true);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

    }

    @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();

    }


    @After
    public void testCaseTearDown()
    {
        driver.quit();
    }

Test Case Explanation-
 
1. Before we start moving SeekBar we need to locate the seetbar so first line of code is to locate it using ID.
2. We need the start position which will give us horizontal start point of seekbar then we need verticle position and width of the SeekBar to perform move action. So check the test case to know how we  are getting this information.
3.  We are using TounchActions to perform the required action hence we created action object of TouchAction class.
4. We have written two testing scenarios to test moving SeekBar till the end and To test SeekBar Moving 40%

Please share this post with your friends and share your questions,feedback or suggestions in comment box below.follow QA Automated for latest post updated.

4 comments:

  1. It's not working for me....I am running this for android app.

    org.openqa.selenium.interactions.InvalidCoordinatesException: The coordinates provided to an interactions operation are invalid. (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 61 milliseconds

    ReplyDelete
  2. can i use the above code for playing Audio and video..which having having seekbar

    ReplyDelete
  3. Can not resolve method " Press" - in the above when i try to Run the Seek bar.

    ReplyDelete
  4. Getting java.lang.NoSuchMethodError: org.openqa.selenium.remote.HttpCommandExecutor.(Ljava/util/Map;Ljava/net/URL;Lorg/openqa/selenium/remote/http/HttpClient$Factory;)V

    ReplyDelete