In android software app, Seekbar Is an element which allows you to set progress level by moving draggable thumb In left or right direction. It Is an extension of Progress Bar. Automating android seekbar movement in appium software automation Is easy but little tricky. We will use TouchAction class of appium to move sleek bar In right or left direction so I recommend you to view THIS EXAMPLE to get more Idea about TouchAction class and It's usage.
App To Use In This Test
Api Demos android software app will help us to learn seekbar movement in appium so download and Install It In your mobile device If It Is not Installed. You can download it from THIS page too.Aim Of This Test
I wants to set SeekBar thumb at my desired position In X(horizontal) direction. Supposing I wants to set It at 60% position of total seekbar's width. We will use appium TouchAction class to move seekbar thumb of android software app.
Create and Run Test
Create new software test ControlSeekBar.java under Android package of your project In eclipse and put bellow given test script Inside It.
ControlSeekBar.java
package Android;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ControlSeekBar {
Process p;
// Set path of your node.exe file.
// Progra~1 represents Program Files folder.
String nodePath = "C:/Progra~1/Appium/node.exe";
// Set path of your appium.js file.
String appiumJSPath = "C:/Progra~1/Appium/node_modules/appium/bin/appium.js";
String cmd = nodePath + " " + appiumJSPath;
AndroidDriver driver;
// This method Is responsible for starting appium server.
public void appiumStart() throws IOException, InterruptedException {
// Execute command string to start appium server.
p = Runtime.getRuntime().exec(cmd);
// Provide wait time of 10 mins to start appium server properly.
// If face any error(Could not start a new session...) then Increase this time to 20 or 25 mins.
Thread.sleep(10000);
if (p != null) {
System.out.println("Appium server Is started now.");
}
}
// This method Is responsible for stopping appium server.
public void appiumStop() throws IOException {
if (p != null) {
p.destroy();
}
System.out.println("Appium server Is stopped now.");
}
@BeforeTest
public void setUp() throws Exception {
// Stop appium server If It Is already running.
appiumStop();
// Start appium server.
appiumStart();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "ZX1B32FFXF");
capabilities.setCapability("browserName", "Android");
capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "io.appium.android.apis");
capabilities.setCapability("appActivity", "io.appium.android.apis.ApiDemos");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
@Test
public void ScrollToView() {
// Scroll till element which contains "Views" text If It Is not visible on screen.
driver.scrollTo("Views");
// Click on Views.
driver.findElement(By.name("Views")).click();
// Scroll till element which contains "Seek Bar" text If It Is not visible on screen.
driver.scrollTo("Seek Bar");
// Click on Seek Bar.
driver.findElement(By.name("Seek Bar")).click();
//Locate SeekBar element.
WebElement seekBar=driver.findElementById("io.appium.android.apis:id/seek");
//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 sllebar move to position.
//endX * 0.6 means at 60% of seek bar width.
int moveToXDirectionAt = (int) (endX * 0.6);
System.out.println("Moving seek bar at " + moveToXDirectionAt+" In X direction.");
//Moving seekbar using TouchAction class.
TouchAction act=new TouchAction(driver);
act.press(startX,yAxis).moveTo(moveToXDirectionAt,yAxis).release().perform();
}
@AfterTest
public void End() throws IOException {
driver.quit();
// Stop appium server when test Is ended.
appiumStop();
}
}
Note : In above android software test script, I have also Included code to start and stop appium server programatically. If you face any Issue in starting and stopping appium server programatically then remove appiumStart() and appiumStop() methods from above test and manage to start and stop It manually.
Test Description
In above software test script,
- First of all Navigated to SeekBar screen.
- Then located seekBar webelement.
- Get startX and endX of seek bar to get It's width on screen.
- Then calculated moveToXDirectionAt value. moveToXDirectionAt Is position where I wants to move seekbar thumb In x direction. Here (endX * 0.6) Means It will calculate 60% from total width of seek bar.
- TouchAction class Is used to create and perform move seekbar thumb action chain.
This Is the way to control seekbar of android app In appium automation test.
This is a really nice post and really help full to us.
ReplyDeleteI have a question if we need to apply assert check that our script move seek bar to 60% or not what would be the code for assert. Please reply me.
My email id : chirag.selenium@gmail.com
Thanks in advance!!
After investigate into elements, i came to know by getting text from below element we can assert the result.
DeleteString seekBarTextAfterMove = driver.findElementById("io.appium.android.apis:id/progress").getText();
System.out.println("seekBarTextAfterMove : "+ seekBarTextAfterMove);
Thanks
this code perfectly moves the seekbar to a desired location in one direction. how to make this is reverse order i.e. if a video was allready played getting its current position and then moving it to back so that i can start from 0
ReplyDeleteGives following error for press
ReplyDeleteThe method press(PointOption) in the type TouchAction is not applicable for the arguments (int, int)
Please help me
Thanks in advance