Skip to main content

Quick setup appium & launch play store app in connected android phone to automate app #java #appium

You can use eclipse or android-studio to create java project.

To do this in Linux/ubuntu follow setps:

Download Android SDK and store it in any specific dir.

To open bashrc file to set env variables:
vi ~/.bashrc

In bashrc file save below & use source command to update:
export ANDROID_HOME=/home/praveen/Android/Sdk
export PATH=$JAVA_HOME/bin:$ANDROID_HOME/build-tools/29.0.3:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH

Now save the bashrc file using "source ~/.bashrc"

if you run "which aapt" we should see full path of aapt tool,this will confirm our android sdk evt variables are set correctly.

Download eclipse, create new java project.
download selenium standalone jar, download all selenium java jars & download appium java client jar & add all these as external jars using build-path for project into "class-path" which is very important.

program:
package androidapptest;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class testapp {

public static void main(String[] args) {

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Mi A2");
caps.setCapability("udid", "4da520f"); //Give Device ID of your mobile phone
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "10");
caps.setCapability("appPackage", "com.android.vending");
caps.setCapability("appActivity", "com.google.android.finsky.activities.MainActivity");
caps.setCapability("noReset", "true");

try {
AppiumDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);

} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
}
}

Now start appium server by double clicking "Appium-linux-1.15.1.AppImage" file to see Appium popup, select Advanced, just click on Start server button to start command line logging of whats happening.

Now in command line, do "adb devices" to get device udid, which we will pass in above program for "udid" field.

Select any app which you want to launch on your connected mobile & sttart your automation, follow as is:
adb shell
<now you will be into adb shell interface>
Run "dumpsys window|grep mCurrentFocus"  to fetch clas sname of app & main activityname of app to pass in above program for "appActivity" & "appPackage" fields.

Now execute your java project & see google play store will open on your mobile screen, make sure your screen is ON, from now you can continue automating with any elements using ui automator app to fetch xpaths of each elements from your phone app.

Comments

Popular posts from this blog

javamail jdk11(11.0.10 and above) needs tls upgrade from 1.1 to 1.2

ISSUE 1: Unable to send/read mails using javamail/javaxmail after you upgraded your jdk to 11.0.10 or above?  The reason is they came up with higher security level for SSL with TLS 1.2 which is dependency for latest jdk, find the solution below: Possible Exception if you used older version TLS-1.1:  javax.net.ssl.sslhandshakeexception Solution: Add below property to your existing Properties. properties.put("mail.imaps.ssl.protocols","TLSv1.2");(IMAP used to read mail) properties.put("mail.smtp.ssl.protocols","TLSv1.2");(SMTP used to send mail) ISSUE 2: Unable to type case Multipart to String? From jdk11(latest version) onwards multipart and string cant be type casted, so we need to use Object to store message content. Solution: for (int i = 0; i < messages.length; i++) { Message message = messages[i]; Object content = message.getContent(); if (content instanceof String) { String body = (String)content; System.out.println...

Extent report for Rest API test

Below code helps in failing or passing your Rest API test case & updating result to Extent report. Maven dependency(pom.xml): <dependency>     <groupId>com.aventstack</groupId>     <artifactId>extentreports</artifactId>     <version>4.0.9</version> </dependency> code: import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; import com.aventstack.extentreports.reporter.ExtentLoggerReporter; public class A{ ExtentLoggerReporter logger = new ExtentLoggerReporter("dir path to store extent result"); static ExtentReports extent; @Test public void testname(){ //Your code to capture json from get/post/put/delete API to "JSONObject jsonobjexpected" field. //your code to read actual json content from file/anywhere to "JSONObject jsonobjactual" field. try {         JSONAssert.assertEquals(jsonobjexpected.toString(...

RestAssured rest API test framework for GET & POST API json verification with Extent report

package com.resttest; import static io.restassured.RestAssured.given; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.DriverManager; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; import org.apache.commons.collections.iterators.EntrySetMapIterator; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import org.testng.Assert; import org.testng.TestNG; import org.testng.annotations.AfterTest; import org.testn...