Using Selendroid to Automate the User Interactions Over a Mobile App (Part 2)

How to use Selendroid framework to automate the user interactions over a mobile application (Part-2). Also, read our ‘Selendroid Introduction Part 1‘ tutorial.

Selendroid is an amazing testing tool, which has several important features as well.

It is a very powerful automation test framework tool which can interact with multiple devices simultaneously. An application can be tested without any modification or change using Selendroid.

In this tutorial, let us see, how to use the Selendroid framework to automate the user interactions over a mobile application (.apk files).

How to Use Selendroid

The app under test is a recipe app, which a user can access to view, rate and perform other things. We can run this app both online as well as offline.

Expectation from the script

  • Start the server, install the apk file (if not installed previously)
  • Launch the application
  • Access the menu list and navigate to a particular category
  • Perform the advanced search to filter out the recipe within the category
  • Add review comment to the recipe
  • Verify the added review comment
  • View list of all recipe created by the author of the current recipe

Application Setup

We need to create a user over BigOven app as which would be required while adding ratings. The user credentials will be useful while we Login to the step of automating the script.

To start with:

1) Make sure that the Selendroid server is started using the following command.
E.g. java –jar selendroid-standalone-0.15.0-with-dependencies.jar –aut 250000RecipesBigOven.apk
Note250000RecipesBigOven.apk is the application under test.

2) Make sure whether the physical device is connected to the PC and any test environment with USB debugging mode is enabled.

3) Make sure that the required Selendroid, selenium jar files are already imported and attached to the libraries.

FileLocation

The first step would be creating new  Package

Package

Mentioning the package name

CreateTestNG

Next step would be creating a TestNG test case under the above package.

Creating new TestNG Class

1) Open the eclipse project
2) Right-click on the package name. Here it is SelendroidFirst
3) Click on TestNG >> create TestNG class

Specifying the Class Name say ‘BigOven’

CreateTestNG

Selendroid Script

Now paste the following codes over the newly created class ‘BigOven’:

package SelendroidFirst;

import io.selendroid.client.SelendroidDriver;
import io.selendroid.client.TouchAction;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

import io.selendroid.common.SelendroidCapabilities;
import io.selendroid.common.device.DeviceTargetPlatform;
import io.selendroid.standalone.SelendroidConfiguration;
import io.selendroid.standalone.SelendroidLauncher;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.List;

public class BigOven{
	
	public SelendroidLauncher selendroidServer = null;
	public static WebDriver driver = null;
		
	@Test(groups= {"Example"})
	public  void BigOven() throws Exception
	{

//**Configurations*//
SelendroidConfiguration config = new SelendroidConfiguration();config.addSupportedApp
("D:\\AppiumAutomation\\SelendroidFirst\\250000RecipesBigOven.apk");
SelendroidLauncher selendroidServer = new SelendroidLauncher(config);
selendroidServer.launchSelendroid();
SelendroidCapabilities caps = new SelendroidCapabilities();
caps.setAut("com.bigoven.android:3.3.1");
WebDriver driver = new SelendroidDriver(caps) ;
caps.setEmulator(false);	

//**Access Recipe list of particular category**//
driver.findElement(By.id("left_image")).click();
List<WebElement> kg1=driver.findElement(By.id("kitchen_grid")).findElements(By.id("grid_item_layout"));
for(WebElement kg2:kg1)
{
if(kg2.findElement(By.id("bottom_text")).getText().equals("Browse By Category"))
{
kg2.click();
break;
}
}
Thread.sleep(2000);
List<WebElement> kg4 = driver.findElement(By.id("select_dialog_listview")).findElements(By.id("text1"));
for(WebElement kg3:kg4)
{
if(kg3.getText().equals("Breakfast"))
{
kg3.click();
break;
}
}

//**Viewing Recipe Details page**//
driver.findElement(By.id("menu_refine_search")).click();
List<WebElement> bs = driver.findElement(By.id("layout_search_by_ingredients")).findElement
(By.id("buttons_layout")).findElements(By.id("search_ingredients_button"));
Thread.sleep(2000);
System.out.println(bs.get(0).getText());
bs.get(0).click();
Thread.sleep(2000);

//**Adding Rating Review to a particular recipe**//
List<WebElement> bl = driver.findElement(By.className("android.widget.ListView")).findElements
(By.id("recipe_title"));
for(WebElement bi : bl)
{
System.out.println(bi.getText());
}
bl.get(0).click();
Thread.sleep(2000);
driver.findElement(By.id("recipe_rate_text")).click();
Thread.sleep(2000);
driver.findElement(By.id("username_edit")).sendKeys("xxxxxx@xxxx.xxx");
driver.findElement(By.id("password_edit")).sendKeys("xxxxxxx");
driver.findElement(By.id("login_button")).click();
Thread.sleep(2000);
driver.findElement(By.id("review_comment")).sendKeys("Review Comment added by testuser");
driver.findElement(By.id("add_item")).click();
Thread.sleep(2000);
driver.findElement(By.id("vp_reviews_layout")).findElement(By.id("vp_reviews_img")).click();
Thread.sleep(2000);
List<WebElement> rl = driver.findElements(By.id("review_info"));
List<WebElement> rc = driver.findElements(By.id("reviewer_comment"));
WebElement rle = rl.get(0);
WebElement rce = rc.get(0);
if(rle.getText().contains("Testuser12345")&& rce.getText().equals("Review Comment added by testuser"))
{
System.out.println("Review added successfully");
}

//**Viewing all the recipes added by the user**//
driver.findElement(By.id("vp_recipe_layout")).findElement(By.id("vp_recipe_img")).click();
Thread.sleep(2000);
String AuthorName = driver.findElement(By.id("recipe_author_name")).getText();
System.out.println("List of recipe created by Author: " + AuthorName);
driver.findElement(By.id("recipe_author_image")).click();
Thread.sleep(2000);
		
}
@AfterTest
public void afterClass() {
if(driver!=null)
{
driver.quit();
}
}
}

Once we have written the test script, the next step to Run the Script.


Select the test case >> right-click >> Run As >> TestNG Test.

Run

Here are few snapshots of the application

Landing page:

HomePage

Category list page:

Search

Search refine page:

Menu

Viewing the Detail recipe:

Details

Login over BigOven:

Login

Adding Review Comments:

Rating

Viewing the added comment:

Review

Now once the test script is executed successfully, the next step would be viewing the execution result. In TestNG, we can do the same from the console “Results of running class BigOven”.

Viewing Console Result

Console

Selendroid Test Result

We can see the test results from the following default location, In our case it is SelendroidFirst >> test-output >> index.html

This is how we can execute the test cases.

We can also create testing XML file and then execute it and also enhance the test result by using TestNG reporting utility.

Conclusion

Selendroid is an open-source framework which allows us to create automation test cases for mobile applications.

The best thing is, it supports Hotplugging. It supports automation of several user interactions over mobile devices.

But the development work and online support are almost stopped, so we might not get all the features instantly. Hence we may face some problem while performing few actions like swap. At times we have to create our own function as well.

This is the end of the Selendroid tutorial series. Please share your comments, questions, and thoughts below.




Recommended reading

2 comments ↓

#1 Taran

Hands-on post.

#2 Aswathylal

Thank you for a wonderful article. I loved it, how simple the code was to follow and understand. And the screen shots were an add-on. Thanks again

Leave a Comment