Friday, 10 March 2017

Appium for iOS with XCUI Instruments with Webdriver agent application

Appium iOS Automation setup with XCUI TEST WebDriver Agent

STEP BY STEP PROCEDURE::

Brew install and Update:

Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple’s OS X operation system.

To install brew:
To update the Homebrew: brew update

Install npm and Update:

Installing Node.js and NPM is pretty straightforward using Homebrew. Homebrew handles downloading,unpacking and installing Node and NPM on your system.

Open the Terminal app and type brew install node

To see if Node is installed, type node -v  in Terminal. This should print the version number of Node
To see if NPM is installed, type npm -v in Terminal. This should print the npm version number.

For Updating Node and NPM:

New versions of Node and NPM come out frequently. You can use Homebrew to update the software it installs.
  1. Make sure Homebrew has the latest version of the Node package. In Terminal type 
brew update
  1. Upgrade Node: brew upgrade node 

To Uninstall Node and NPM:

 

Use Homebrew to uninstall packages that it installed: brew uninstall node.
Use Homebrew to uninstall npm: brew uninstall npm.

Install Xcode:

Apple’s XCode development software is used to build Mac and iOS apps, but it also includes the tools you need to compile software for use on your Mac.
Download XCode from: https://developer.apple.com/xcode/download/  and install.


Install Appium, XcuitestDriver and Appium Doctor:

Appium:

Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS and Android platforms.

Download latest Appium version fromhttps://bitbucket.org/appium/appium.app/downloads/  and install.

Note: use “Sudo”, if you want to install as ADMIN
1.       Command to install Appium: npm install –g Appium@1.6.3
2.       To check Appium version : Appium –v
3.       To uninstall Appium: Sudo npm uninstall –g Appium

XCUITEST:

1.       Command to install Xcuitest Driver: npm install appiun-xcuitest-driver
2.       To uninstall Appium: Sudo npm uninstall appiun-xcuitest-driver
3.        

Appium Doctor:

1.       Command to install AppiumDoctor: npm install appiun-doctor –g
2.       To check iOS configuration: Appium-doctor --ios
3.       To uninstall Appium: Sudo npm uninstall appiun-doctor
4.       Usage:
--ios
To Check iOS setup
--android
To check Android Setup
--dev
To check dev setup
--debug
To show Debug message
--yes
Always respond Yes
--no
Always Respond No
--demo
To run Appium-doctor demo for dev
-h or –help
To show help



Appium Inspector:

No official Appium Inspector is released for Appium 1.6 or above. So there are few workarounds to identify iOS native elements.

Workaround-1: Accessibily Inspector

a.       Open XcodeàOpen Developer Tool à Click on Accessibility Inspector.
b.      Connect  an iOS device to MAC machine  and open that device in Quick Time player
c.       Select  connected device as Target Device in Accessibility Inspector
d.      Inspect required elements

Workaround-2: By Using Older Version Appium GUI

a.       Open GUI of Appium (Older version, ex:1.5.3)and set all required capabilities.
b.      Start Appium 1.6.3(1.6 or above) from command prompt.
c.       Once server is started, start inspecting elements by click on Appium Inspector on GUI

Install External Dependencies:

Ideviceinstaller:

Ideviceinstaller is a tool to interact with the installation_proxy of an iOS device allowing installing, upgrading, uninstalling, archiving, restoring and enumerating installed or archived apps.
In order to install this tool, Open the terminal and run the following command:

Command: brew install --HEAD  ideviceinstaller

Carthage:

Carthage builds your dependencies and provides you with binary frameworks, but you retain full control over your project structure and setup. Carthage does not automatically modify your project files or your build settings.

Command: brew install carthage

Note: if you previously installed the binary version of Carthage, you should delete    “/Library/Frameworks/CarthageKit.framework”

Ios-deploy:

Install and debug iOS apps without using Xcode. Designed to work on un-jailbroken devices.
Ideviceinstaller  doesn't work with iOS 10 yet. So we need to install ios-deploy

Command: npm install –g ios-deploy

DeviceConsole:

Command: npm install –g deviceconsole

Xcpretty:

Xcpretty is a fast and flexible formatter for xcodebuild.
It does one thing, and it should do it well.

Command: gem install xcpretty



Libimobiledevice:

Command: brew install libimobiledevice –HEAD  #install from head to get important update

WebDriverAgent Configuration:

1.       Go to node_modules/appium-xcuitest-driver/WebDriverAgent
2.       mkdir –p Resources/WebDriverAgent.bundle
3.       sh ./Scripts/bootstrap.sh –d
4.       Open Xcode with WebDriverAgent.xcodproj
5.       Build WebDriverAgent Project with Sign.

Steps to execute XCUITEST scripts:

a.       Sign WebDriverAgent and WebDriverAgentRunner with Apple Account and Check by building it on Simulator and Real Device.
b.      Resign the Application which you want to automate and provide the path of the resigned ipa file in script.
c.       Create the script, Start Appium and Run the testcases.

Links:

Working with Robotium - Robotium Quick Start

Robotium


Powerful Test Cases can be developed, without knowing source code of the application.
Robotium is an open-source test  framework  like Selenium but only for writing automatic black-box test cases only for Android applications.
Robotium can be used both for testing applications where the source code is available and applications where only the APK file is available and the implementation details are not known.
Robotium is similar to Selenium, but for Android. It has support for Android features such as activities, toasts, menus and context menus.
APK=Android application package file (APK) is the file format used to distribute and install application software and middleware onto Google's Android operating system.

What you are not able to do with Robotium :
Cross Activities testing, Robotium is able to work only with same certificate app, otherwise you will get inject events exception (eg :you are not able to do clicks on screen keyboard)
 Robotium has no mechanism to handle expected/unexpected alerts/popus/dialogues. For example iOs javascript tests has very simple boolean flag and callback for handling alerts
 Robotium has big problem with auto scrolling methods (possibly currently it is fixed) for example if you are looking for the text, which is not shown, Robotium will stack in the end of the scroll view and make assertTrue(false) to stop scrolling.
Robotium does not support flash and webbased applications.
Robotium does not work on Canvas.
BLACK BOX  TESTING  WITH  ROBOTIUM  ON  APK  FILES

Requirements:
1) Android application apk file for Testing.Ex: MetlifeTesting.apk
2) Eclipse for building Test project
3) ADT (Android Development Tools)
4) SDK (Software Development Kit)
5) JDK(Java Development Kit)
6) robotium-solo-3.1.jar

Prerequisites for creating test project:
*Install eclipse,ADT,SDK,JDK to your system.
*After installation give proper path in environmental variable

PATH: C:\Program Files\Java\jdk1.6.0_27\bin;D:\monkeyEclipse\android-sdk\platform-tools

 STEP 1: CREATE TEST  PROJECT  IN  ECLIPSE

Create the test project by:    
 FileàNewàProjectàAndroidàAndroid Test Project

Fill all fields like below format:
*Test Project Name: MetlifeTesting

*Test Target: Click on“This Project“

*BuildTarget:
If the application was developed using SDK Version 7 then select Android 2.1–update1.
 If it was developed by SDK Version 8 Then select Android 2.2.

*Properties : Applicationname : MetlifeTesting

*Package name: com.igate. MetlifeTesting

*Min SDK version:
Default value will be there according to Build Target selection Then click on “finish”.
  
STEP 2: DO THE FOLLOWING CHANGES IN “AndroidManifest.xml”

Change:
<instrumentation
android:targetPackage=" com.igate. MetlifeTesting "
To:
<instrumentation
android:targetPackage=" com.metlifeapps.metlifeus "

If you do not know The exact package name then type this in the DOS prompt
>launch the emulator
>adb logcat – cmd1
>adb install testapplication.apk – cmd2

 Run the application once and you will get the exact package name.

STEP 3: CREATE SAMPLE TEST CASE

Select The package and right click it and select:   New àClass


  
Copy this code into the editor:

package com.igate.MetlifeTesting;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;

public class HomePage extends ActivityInstrumentationTestCase2{

private static final String TARGET_PACKAGE_ID="com.metlifeapps.metlifeus";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.metlifeapps.metlifeus.MetAndroidActivity";
               
                private static Class launcherActivityClass;
               
                static{
                                try          {
                                                launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
                                                }catch(ClassNotFoundException e){
                                                throw new RuntimeException(e);
                                                }
                                }
                               
                @SuppressWarnings("unchecked")
                public HomePage () throws ClassNotFoundException
                {
                super(TARGET_PACKAGE_ID,launcherActivityClass);
                }
               
                private Solo solo; //Robotium class used to support test cases that span over multiple activities.
                @Override
                protected void setUp()throws Exception
                {
                solo=new Solo(getInstrumentation(),getActivity());//takes in the instrumentation and the start activity.
                }
// the test methods MUST begin with test...

                public void testMenuSelect(){
                                solo.pressMenuItem(0);
                }
               
                @Override
                public void tearDown() throws Exception {
                    solo.finishOpenedActivities();
                }
}



NOTE: Change TARGET_PACKAGE_ID  and  LAUNCHER_ACTIVITY_FULL_CLASSNAME ..

private static final String TARGET_PACKAGE_ID = " com.metlifeapps.metlifeus";
Private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME =
"com.metlifeapps.metlifeus.MetAndroidActivity ";

If you do not know The exact package name then type this in the DOS prompt
>launch the emulator
>adb install MetlifeAndroid.apk
>adb logcat

Or

>aapt dump badging appname.apk

Look for

package: name='com.igate.retail.ecoupon'
launchable-activity: name='com.igate.retail.ecoupon.activity.StartActivity'

Run the application once and you will get the exact package name.


STEP 4: ADD THE LATEST VERSION OF THE robotium jar FILE TO THE PROJECT.

Right  click on “MetlifeTesting” projectàBuild PathàConfigure Build Path


 Then   Select    Add External JarsàSelect  robotium.jar àfile à Open àOK





STEP 5:

The apk file has to have the same certificate signature that your test project has Signature:

The Signature will identify the author of the android application.Signature Means it contains the information like firstname and last name of the developer, Name Of the organizational unit, organization, city, state, two--‐letter country code.
Standard Tools like Keytool And Jarsigner Are used to generate keys and sign applications.


NOTE:

* If You know the certificate signature then you need to use the same signature in your test project

*If You do not know the certificate  signature then you need to delete the certificate signature and you shoulduse the same android debug key signature in both the application and the test project

*If The application is unsigned then you need to sign the application apk with the android debug key

PROCESS FOR GENERATING DEBUG KEYSTORE:

1)      Un-Zip the apk file.
2)      Delete The META--‐INF folder(Because META-INF folder contains certificates)
3)      Re-Zip the remaining files and rename as .apk.
(It will zip file as "MetlifeAndroid.apk.zip" , rename it to: " MetlifeAndroidNew.apk")
4)      In command prompt run below command:
             >keytool.exe -genkey -v -keystore debug.keystore -alias debug.keystore –keyalg
               RSA–validity  20000
               
(Or)
                This will definitely work:
C:\Users\bm812203\.android> keytool -genkeypair -alias androiddebugkey -keypass android
-keystore debug.keystore -storepass android -dname "CN=Android Debug,O=Android,C=US"
-validity 9999

keytool -list -alias androiddebugkey -keystore (path_to_debug_keystore).keystore -storepass
android -keypass android

jarsigner.exe -verbose -keystore debug.keystore  MetlifeAndroid.apk [give aliasname]

       5)   Create new folder in Eclipse workspace and put debug.keystore file .
     
    
 6)   Set custom debug.keystore path in eclipse:
             windows-->preferences-->android-->build





7)Right click on project àexportàdebug .keystore(path of sdk—platform tools)
STEP 6:  RUN THE EXAMPLE TEST CASES

Right Click on the test project     à Run As  àAndroid JUnit Test




STEP 7:  RUNNING TEST CASES ON THE EMULATOR/DEVICE WITHOUT USING ECLIPSE
1)      Put .apk file into android-sdk folder.

2)      Install the application apk:
>adb install MetlifeAndroid.apk

3)      Install the test project apk: (.apk copied from bin folder)
>adb install MetlifeTesting.apk
             
               4)   Run the test cases: (Here com.igate.MetlifeTesting is the package_name of your test    application)

   >adb shell am instrument -w com.igate.MetlifeTesting/android.test.InstrumentationTestRunner

For more reference go  to: