Appium Android - Set Star Rating Bar Example

Automating star rating bar in appium android software test is very easy, There will be star rating bar in many android software applications. Generally star rating bar is used to rate products, services in eCommerce android software applications. Mostly you will see 3 star and 5 star rating bars in such android software applications. If you wants to automate star ratings in appium android test then it is little tricky but very easy and we can do it using TouchAction class. Let's see how can we automate 3 star and 5 star rating bars of android software application using appium with example.

Aim Of Test And App To Use
We wants to automate 3 star and 5 star rating bars in android appium automation test. We will use API Demos app to automate star ratings. 

As you can see in bellow image, We wants to give 1 star on 3 star rating bar and 4 star on 5 star rating bar.

In API Demos app, Manually you can navigate to above screen from Views -> Rating Bar.

Create And Run Test
Create bellow given test in eclipse and run it using testng and appium.
package Android;

import io.appium.java_client.TouchAction;
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 SetStarRating {
 AndroidDriver driver;

 public void setUp() throws Exception {
  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", "");
  driver = new AndroidDriver(new URL(""), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  // Scroll till element which contains "Views" text If It Is not visible on screen.
  // Click on Views.
  // Scroll till element which contains "Rating Bar" text If It Is not visible on screen.
  driver.scrollTo("Rating Bar");
  // Click on Rating Bar.
  driver.findElement("Rating Bar")).click();

 //Set 3 StarRatingbar = 1 star.
 public void set3StarRatingbar(){  
  //Locate threeStarRatingbar.
  WebElement threeStarRatingbar = driver.findElement(""));
  //Get start point of threeStarRatingbar.
  int startX = threeStarRatingbar.getLocation().getX();
  //Get end point of threeStarRatingbar.
  int endX = threeStarRatingbar.getSize().getWidth();
  //Get vertical location of threeStarRatingbar.
  int yAxis = threeStarRatingbar.getLocation().getY();
  //Set threeStarRatingbar tap position to set Rating = 1 star.
  //You can use endX * 0.3 for 1 star, endX * 0.6 for 2 star, endX * 1 for 3 star.
  int tapAt = (int) (endX * 0.3);    
  //Set threeStarRatingbar to Rating = 1.0 using TouchAction class.
  TouchAction act=new TouchAction(driver);,yAxis).release().perform();
 //Set 5 StarRatingbar = 4 star.
 public void set5StarRatingbar(){  
  //Locate fiveStarRatingbar.
  WebElement fiveStarRatingbar = driver.findElement(""));
  //Get start point of fiveStarRatingbar.
  int startX = fiveStarRatingbar.getLocation().getX();
  //Get end point of fiveStarRatingbar.
  int endX = fiveStarRatingbar.getSize().getWidth();
  //Get vertical location of fiveStarRatingbar.
  int yAxis = fiveStarRatingbar.getLocation().getY();
  //Set fiveStarRatingbar tap position to set Rating = 4 star.
  //You can use endX * 0.2 for 1 star, endX * 0.4 for 2 star, endX * 0.6 for 3 star, endX * 0.8 for 4 star, endX * 1 for 5 star.
  int tapAt = (int) (endX * 0.8);  
  //Set fiveStarRatingbar to Rating = 4 star using TouchAction class.
  TouchAction act=new TouchAction(driver);,yAxis).release().perform();

 public void End() {

As you can see in above test, I have created 2 @Test methods. 1st @Test method will give 1 star rating on 3 star rating bar and 2nd @Test method will give 4 star rating on 5 star rating bar. If you wants to increase or decrease star rating then you can set value of tapAt as per your requirement as describe in test comment.

This is the way to handle star rating bar in android software application automation test using appium.

1 comment:

  1. int endX = fiveStarRatingbar.getSize().getWidth();
    should be
    int endX = startX + fiveStarRatingbar.getSize().getWidth();