Use the Selendroid framework to automate 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 user interactions over a mobile application (.apk files).
Table of Contents:
Using Selendroid to Automate Mobile App User Interactions
The app under test is a recipe app, which a user can access to view, rate, and perform other things. You can run this app both online as well as offline.
Expectations From the Script
- Start the server and install the apk file (if not installed previously)
- Launch the application
- Access the menu list and navigate to a particular category
- Perform advanced searches to filter out recipes within the category
- Add a review comment to the recipe
- Verify the added review comment
- View a list of all recipes created by the author of the current recipe
Application Setup
We need to create a user over the BigOven app which would be required while adding ratings. The user credentials will be useful as we log in 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
Note: 250000RecipesBigOven.apk is the application under test.
2) Make sure 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, and selenium jar files are already imported and attached to the libraries.
The first step would be creating a new Package
Mentioning the package name
The next step would be to create a TestNG test case under the above package.
Creating a new TestNG Class
1) Open the eclipse project
2) Right-click on the package name. Here is the SelendroidFirst
3) Click on TestNG >> create TestNG class
Specify the Class Name to say ‘BigOven’
Selendroid Script
Now paste the following code 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 is to Run the Script.
Select the test case >> right-click >> Run As >> TestNG Test.
Here are a few snapshots of the application
Landing page:
Category list page:
Search refine page:
Viewing the Detail recipe:
Login over BigOven:
Adding Review Comments:
Viewing the added comment:
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”.
View Console Results
You 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 a testing XML file and then execute it and also enhance the test result by using the TestNG reporting utility.
Conclusion
Selendroid is an open-source framework that allows us to create automation test cases for mobile applications.
The best thing is that it supports Hotplugging. It supports the 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 problems while performing a few actions like swap. At times we have to create our own functions as well.
This is the end of the Selendroid tutorial series. Please share your comments, questions, and thoughts below.
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
Hands-on post.