Monday, September 3, 2018

Appium Android - Develop Star Rating Bar Example

| Monday, September 3, 2018
Automating star rating bar inwards appium android software bear witness is rattling easy, There volition move star rating bar inwards many android software applications. Generally star rating bar is used to charge per unit of measurement products, services inwards eCommerce android software applications. Mostly yous volition run across 3 star too 5 star rating bars inwards such android software applications. If yous wants to automate star ratings inwards appium android bear witness therefore it is picayune tricky but rattling tardily too nosotros tin produce it using TouchAction class. Let's run across how tin nosotros automate 3 star too 5 star rating bars of android software application using appium alongside example.

Aim Of Test And App To Use
We wants to automate 3 star too 5 star rating bars inwards android appium automation test. We volition move API Demos app to automate star ratings. 

As yous tin run across inwards bellow image, We wants to give i star on 3 star rating bar too iv star on 5 star rating bar.


In API Demos app, Manually yous tin navigate to to a higher house covert from Views -> Rating Bar.

Create And Run Test
Create bellow given bear witness inwards eclipse too run it using testng too appium.

SetStarRating.java
package Android;  import io.appium.java_client.TouchAction; import io.appium.java_client.android.AndroidDriver; 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;  populace bird SetStarRating {  AndroidDriver driver;   @BeforeTest  populace void setUp() throws Exception {   DesiredCapabilities capabilities = novel 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 = novel AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);   // Scroll till chemical component subdivision which contains "Views" text If It Is non visible on screen.   driver.scrollTo("Views");   // Click on Views.   driver.findElement(By.name("Views")).click();   // Scroll till chemical component subdivision which contains "Rating Bar" text If It Is non visible on screen.   driver.scrollTo("Rating Bar");   // Click on Rating Bar.   driver.findElement(By.name("Rating Bar")).click();  }   //Set 3 StarRatingbar = i star.  @Test  populace void set3StarRatingbar(){     //Locate threeStarRatingbar.   WebElement threeStarRatingbar = driver.findElement(By.id("io.appium.android.apis:id/ratingbar1"));   //Get root signal of threeStarRatingbar.   int startX = threeStarRatingbar.getLocation().getX();   System.out.println(startX);   //Get halt signal of threeStarRatingbar.   int endX = threeStarRatingbar.getSize().getWidth();   System.out.println(endX);   //Get vertical place of threeStarRatingbar.   int yAxis = threeStarRatingbar.getLocation().getY();      //Set threeStarRatingbar tap seat to laid upwardly Rating = i star.   //You tin move endX * 0.3 for i star, endX * 0.6 for ii star, endX * i for 3 star.   int tapAt = (int) (endX * 0.3);       //Set threeStarRatingbar to Rating = 1.0 using TouchAction class.   TouchAction act=new TouchAction(driver);     act.press(tapAt,yAxis).release().perform();  }    //Set 5 StarRatingbar = iv star.  @Test  populace void set5StarRatingbar(){     //Locate fiveStarRatingbar.   WebElement fiveStarRatingbar = driver.findElement(By.id("io.appium.android.apis:id/ratingbar2"));   //Get root signal of fiveStarRatingbar.   int startX = fiveStarRatingbar.getLocation().getX();   System.out.println(startX);   //Get halt signal of fiveStarRatingbar.   int endX = fiveStarRatingbar.getSize().getWidth();   System.out.println(endX);   //Get vertical place of fiveStarRatingbar.   int yAxis = fiveStarRatingbar.getLocation().getY();      //Set fiveStarRatingbar tap seat to laid upwardly Rating = iv star.   //You tin move endX * 0.2 for i star, endX * 0.4 for ii star, endX * 0.6 for 3 star, endX * 0.8 for iv star, endX * i for 5 star.   int tapAt = (int) (endX * 0.8);     //Set fiveStarRatingbar to Rating = iv star using TouchAction class.   TouchAction act=new TouchAction(driver);     act.press(tapAt,yAxis).release().perform();  }   @AfterTest  populace void End() {   driver.quit();  } }

As yous tin run across inwards to a higher house test, I convey created 2 @Test methods. 1st @Test method volition give i star rating on 3 star rating bar too 2d @Test method volition give iv star rating on 5 star rating bar. If yous wants to increase or decrease star rating therefore yous tin laid upwardly value of tapAt every bit per your requirement every bit depict inwards bear witness comment.

This is the agency to handgrip star rating bar inwards android software application automation bear witness using appium.

Related Posts