Wednesday, April 12, 2017

Selendroid Automation for own Created apk file

Step 1:- Download this selendroid-standalone-0.17.0-with-dependencies.jar from below given url
and use this command to start it on port 4444 (Use your desire port to start this jar)

C:\Software\Mobile_Automation\newone\NewAPK>java -jar selendroid-standalone-0.17.0-with-dependencies.jar
Step 2:- Run AVD Manager.exe kept at location
and Start Emulator i.e. execute AVD Manager.exe which we want to run
Step 3:- Once Emulator is start make sure to confirm that we are able to see our deployed apk main activity class and app id on this page on the screen along with Emulator name

Step 4 :- Create signed apk from android studio using keystore file palced at debug.keystore available at C:\Users\test\AppData\Local\Android\sdk\platform-tools\debug.keystore for windows and for linux ~/.android/debug.keystore
Keystore name: "debug.keystore"
Keystore password: "android"
Key alias: "androiddebugkey"
Key password: "android"
CN: "CN=Android Debug,O=Android,C=US"
Or you can also used your signed jar using your own *.keystore
Note:- Refer below screen shot for signing the apk from Android Studio.
Step 5:- This is optional step if not done it will be taken care in Step 6. Install our APK in Emulator.If you don't want to use install command for installing APK you can directly drag drop apk on the Emulator screen and it will deploy your APK.
Step 6:- Take app-release-unaligned.apk that is created in step4 and start your selendroid standalone server using below command and check if you are able to see your applicaition the ui. i.e. http://localhost:4444/wd/hub/status

java -jar selendroid-standalone-0.17.0-with-dependencies.jar -keystore C:\Software\Mobile_Automation\newone\NewAPK\debug.keystore -keystorePassword android -aut app-release-unaligned.apk

java -jar selendroid-standalone-0.17.0-with-dependencies.jar -app app-release-unaligned.apk

Step 7:- Make sure to inspect the tag/widget id of our apk file refer to belwo site for the same.
Use below java code to execute your test case
package com.test.siddhu;
import io.selendroid.client.SelendroidDriver;
import io.selendroid.common.SelendroidCapabilities;
import io.selendroid.common.device.DeviceTargetPlatform;
import io.selendroid.standalone.SelendroidLauncher;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class MobileWebTest {
private SelendroidLauncher selendroidServer = null;
private WebDriver driver = null;
public void shouldSearchWithEbay() {

try {

/* SelendroidConfiguration config = new SelendroidConfiguration();
selendroidServer = new SelendroidLauncher(config);
SelendroidCapabilities capabilities = new SelendroidCapabilities("");
driver = new SelendroidDriver(capabilities);*/
/* SelendroidConfiguration config = new SelendroidConfiguration();
selendroidServer = new SelendroidLauncher(config);
//SelendroidCapabilities capabilities = new SelendroidCapabilities("");
// SelendroidCapabilities capa = new SelendroidCapabilities("");
//capa.setModel("Nexus 5");
//WebDriver driver = new SelendroidDriver(capa);

//SelendroidCapabilities caps = SelendroidCapabilities.emulator("");
//SelendroidCapabilities capabilities = new SelendroidCapabilities("");
/* capabilities.setAut("");
capabilities.setCapability("appPackage", "");
capabilities.setCapability("appActivity", "");*/
URL url = new URL("http://localhost:4444/wd/hub");
// DesiredCapabilities desiredCapabilities =;
//WebDriver driver = new SelendroidDriver(url,capabilities);
SelendroidCapabilities caps = new SelendroidCapabilities("");
// explicitly state that we want to run our test on an Android API level 10 device

// explicitly state that we use an emulator (an AVD) for test execution rather than a physical device
WebDriver driver = new SelendroidDriver(url,caps);

WebElement inputField1 = driver.findElement("editText"));
Assert.assertEquals("true", inputField1.getAttribute("enabled"));

WebElement inputField2 = driver.findElement("editText2"));
Assert.assertEquals("true", inputField2.getAttribute("enabled"));

WebElement button = driver.findElement("button"));;
// Delay time to take effect

WebElement inputField3 = driver.findElement("editText3"));
Assert.assertEquals("true", inputField3.getAttribute("enabled")); 
Assert.assertEquals("25", inputField3.getText());

} catch (Exception e) {
// TODO Auto-generated catch block
Note: - If you are getting below

Selendroid server on the device didn't came up after 20sec:
WARNING: Could not free Selendroid port
SEVERE: Error executing command: C:\Users\test\AppData\Local\Android\sdk\platform-tools\adb.exe forward --remove tcp:8080
Make sure to see Logcat i.e. C:\Users\test\AppData\Local\Android\sdk\platform-tools\adb.exe logcat
and check the detail log for me my application/apk was installing in the emulator but was not able to display on the screen. It was getting closed immediately and the answer was in below exception
04-12 12:56:23.729 6520 6534 E SELENDROID: /sdcard/appcrash.log: open failed: ENOENT (No such file or directory)
To over come this add following line inside your AndroidManifest.xml
<!--uses-permission android:name="android.permission.INTERNET" />
<!--uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!--uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
For Example we had taken apk which provide addition of two value on gui.

No comments: