Handling Ratingstar Using Appium


Rating 3 Star
import java.net.URL;
import java.util.List;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.offset.ElementOption;
import io.appium.java_client.touch.offset.PointOption;

public class Example14Rating3Star {
   
    public static void main(String[] args) throws Exception {
       
        DesiredCapabilities dc= new DesiredCapabilities();
       
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");

        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");//Version is number here
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.APP, "C:\\apkfiles\\ApiDemos.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);
               
        driver.findElementsById("android:id/text1").get(10).click(); //Views
               
        //scrolling down till we get the element
                MobileElement listitem = (MobileElement) driver.findElement(
                        MobileBy.AndroidUIAutomator(
                                "new UiScrollable(new UiSelector()).scrollIntoView("
                                        + "new UiSelector().description(\"Rating Bar\"));"));
               
                listitem.click(); //click on Seek Bar
   
   
        //Locate threeStarRatingbar.
        WebElement threeStarRatingbar = driver.findElementById("io.appium.android.apis:id/ratingbar1");
       
        //Get start point of threeStarRatingbar.
          int startX = threeStarRatingbar.getLocation().getX();
          System.out.println(startX);
       
         //Get end point of threeStarRatingbar.
          int endX = threeStarRatingbar.getSize().getWidth();
          System.out.println(endX);
         
          //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 * 0.9 for 3 star.
              int tapAt = (int) (endX * 0.9);   
           
              //Set threeStarRatingbar to Rating = 1.0 using TouchAction class.
                  TouchAction act=new TouchAction(driver); 
               
                 //act.press(tapAt,yAxis).release().perform();
                  act.press(PointOption.point(tapAt, yAxis)).release().perform();
                 
               
    }

}
Rating 5 Star

import java.net.URL;
import java.util.List;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.offset.ElementOption;
import io.appium.java_client.touch.offset.PointOption;

public class Example14Rating5Star {
   
    public static void main(String[] args) throws Exception {
       
        DesiredCapabilities dc= new DesiredCapabilities();
       
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");//Version is number here
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.APP, "C:\\apkfiles\\ApiDemos.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);
               
        driver.findElementsById("android:id/text1").get(10).click(); //Views
               
        //scrolling down till we get the element
                MobileElement listitem = (MobileElement) driver.findElement(
                        MobileBy.AndroidUIAutomator(
                                "new UiScrollable(new UiSelector()).scrollIntoView("
                                        + "new UiSelector().description(\"Rating Bar\"));"));
               
                listitem.click(); //click on Seek Bar
   
   
        //Locate threeStarRatingbar.
        WebElement threeStarRatingbar = driver.findElementById("io.appium.android.apis:id/ratingbar2");
       
        //Get start point of threeStarRatingbar.
          int startX = threeStarRatingbar.getLocation().getX();
          System.out.println(startX);
       
         //Get end point of threeStarRatingbar.
          int endX = threeStarRatingbar.getSize().getWidth();
          System.out.println(endX);
         
          //Get vertical location of threeStarRatingbar.
              int yAxis = threeStarRatingbar.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.6);   
           
              //Set threeStarRatingbar to Rating = 1.0 using TouchAction class.
                  TouchAction act=new TouchAction(driver); 
               
                 //act.press(tapAt,yAxis).release().perform();
                  act.press(PointOption.point(tapAt, yAxis)).release().perform();
                 
               
    }

}

Followers