Sunday, 19 June 2016

Appium Error: Failed to start an Appium session, err was: Error: Screen did not unlock

The below error occured in Appium during the execution

error: Failed to start an Appium session, err was: Error: Screen did not unlock


https://github.com/appium/appium/issues/5255


While we  checked. issue is with nexus 4(Android 4.4.2) and nexus 5(Android 5.0.1). It is working fine with nexus 7(Android 4.3)

This error occured while taking screenshot automatically and make unlock.

Appium Error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity

The below error occured in appium during execution

Error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity


Solution : If the we provide incorrect activity name then we are getting this error.

Android Emulator Issue: The home button, Menu button, back button, Zoom Button is not woking in Android Emulator


The home button, Menu button, back button, Zoom Button is not wotking in Android Emulator, So please use below steps for resolve this issue

-> Goto below path in machine


C:\Users\<User_ID>\.android\avd\<AVD_Name>.avd\config.ini


-> Open the config.ini file

-> change the below settings

from
hw.mainKeys=no   change to  
hw.mainKeys=yes

-> Now restart the AVD then we got home button, Menu button, back button, Zoom Button is woking in Android Emulator


hw.mainKeys=yes

Saturday, 18 June 2016

Appium - Code for Automate the Calculator App in Android Mobile


Please follow below steps to Automate the mobile application in Appium


Emulator Setup for Android Mobile -> Setup




The First Appium code for Automate the calculator App



import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL; 
import java.util.concurrent.TimeUnit; 

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By; 
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.remote.CapabilityType; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest; 
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class Mobile 



WebDriver driver;

@BeforeTest 

public void setUp() throws MalformedURLException

{

// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();

// Set android deviceName desired capability. Set your device name. 
capabilities.setCapability("deviceName", "sdk"); 
// Set BROWSER_NAME desired capability. It's Android in our case here.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android"); 
// Set android VERSION desired capability. Set your mobile device's OS version.
capabilities.setCapability(CapabilityType.VERSION, "4.4.4"); 
// Set android platformName desired capability. It's Android in our case here. 
capabilities.setCapability("platformName", "Android");
// Set android appPackage desired capability. It is 
// com.android.calculator2 for calculator application. 
// Set your application's appPackage if you are using any other app.

capabilities.setCapability("appPackage", "com.android.calculator2"); 

// Set android appActivity desired capability. It is
// com.android.calculator2.Calculator for calculator application.
// Set your application's appPackage if you are using any other app. 

capabilities.setCapability("appActivity", "com.android.calculator2.Calculator"); 

// Created object of RemoteWebDriver will all set capabilities. 
// Set appium server address and port number in URL string.
// It will launch calculator app in android device. 

driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 
}

@Test
public void Sum() throws IOException 


System.out.println("Appium");


driver.findElements(By.xpath("//android.widget.Button")).get(0).click();
// driver.findElement(By.name("CLR")).click(); 
// Click on number 2 button. 
driver.findElement(By.name("6")).click(); 
// Click on + button. 
driver.findElement(By.name("+")).click(); 
// Click on number 5 button. 
driver.findElement(By.name("5")).click();
// Click on = button. 
driver.findElement(By.name("=")).click(); 
// Get result from result text box. 
String result = driver.findElement(By.className("android.widget.EditText")).getText(); 
System.out.println("Number sum result is : " + result);

File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File(System.getProperty("user.dir")+"\\ScreenShots.png"));


}

@AfterTest public void End() { driver.quit(); } 

}

How to find app Launcher activity name in Appium



We have two options for find app activity name

1) By using Command Prompt
2) Manifest Info
3) Hierarchy Viewer


1) By using Command Prompt

Please find below steps to find app activity name using command prompt.


-> app APK package name. EX : com.android.calculator2

-> Launch the Android Emulator and wait till starts properly.

-> Goto command prompt and goto E:\android-sdks\platform-tools and verify the device connected properly (not offline). by using below command

E:\SDK\platform-tools>adb devices

List of devices attached
emulator-5554   device

-> Open the reuired app and keep on active in Emulator

-> GOTO and command prompt and use below commands

E:\android-sdks\platform-tools>adb logcat >log.txt

-> Now its generated log text file. Here you can search by app Package name (com.android.calculator2) then we will have log like

I/ActivityManager(  878): Displayed com.android.calculator2/.Calculator: +7s332ms


Here com.android.calculator2.Calculator is app activity name.



2) By using AndroidManifest.xml Info



http://software-testing-tutorials-automation.blogspot.in/2015/10/find-launcher-activity-and-package-name.html


-> Right click on .apk file of APK Info app(or any other .apk file for which you wants to know package and activity name).

-> Select Open with -> WinRAR archiver option from right click context menu.

-> It will open .apk file in WinRAR window and you can see AndroidManifest.xml.

-> Open AndroidManifest.xml file in notepad. Now find keyword "p a c k a g e" In notepad.

-> Package name for my APK Info app is : com.intelloware.apkinfo Find keyword "a c t i v i t y" In notepad. It will show you activity name of your app.


-> Activity name for my APK Info app is display: .MainActivity. Here you need to append package name ahead of activity name so full activity name is : com.intelloware.apkinfo.MainActivity



3) Hierarchy Viewer 


-> Launch the Android Emulator and wait till starts properly.

-> Launch the appium and start the server.

-> Open the Hierarchy viewer from below path. Android Emulator should be starts properly, its should not be closed.

C:\android-sdks\tools\hierarchyviewer.bat


-> Then goto page in emulator which activity needs to be record.

-> Then automatically we can see the package name and activity name in Hierarchy viewer

we can see in bold color

com.android.calculator2/com.android.calculator2.calculator

Here,

package Name = com.android.calculator2
Activity Name = com.android.calculator2.calculator

How to find APK Package using UI Automator Viewer


We can identify the Package name by using below two methods


1) UI Automator viewer
2) Hierarchy Viewer


1) UI Automator viewer

Please find below steps to find APK package using UI Automator viewer


-> Open the reuired app and keep on active in Emulator

-> Launch the uiautomatorviewer.bat file from below mentioned path

E:\android-sdks\tools\uiautomatorviewer.bat

-> Click "Device Screenshot ("Uiautomater Dump") in uiautomatorviewer then its taking screenshot and paste into uiautomatorviewer.

-> Now we can inspect the element.

-> Right hand side we can see the option like "package" for APK package name.




2) Hierarchy Viewer


-> Launch the Android Emulator and wait till starts properly.

-> Launch the appium and start the server.

-> Open the Hierarchy viewer from below path. Android Emulator should be starts properly, its should not be closed.

C:\android-sdks\tools\hierarchyviewer.bat


-> Then goto page in emulator which activity needs to be record.

-> Then automatically we can see the package name and activity name in Hierarchy viewer

we can see in bold color

com.android.calculator2/com.android.calculator2.calculator

Here,

package Name = com.android.calculator2
Activity Name = com.android.calculator2.calculator



How to inspect element in mobile app for appium mobile automation testing (By using Emulator)



We can inspect the element in mobile app by using uiautomatorviewer.bat file from Android SDK folder.


-> Open the required app and keep on active in Emulator

-> Launch the uiautomatorviewer.bat file from below mentioned path

E:\android-sdks\tools\uiautomatorviewer.bat

-> Click "Device Screenshot ("Uiautomater Dump") in uiautomatorviewer then its automatically taking screenshot and paste into uiautomatorviewer.

-> Now we can inspect the element from Uiautomationviewer.