Appium Mobile Automation

  • Appium Setup on Laptop:
1) Install JAVA and set its path in environment variables.

2) Android SDK
-> install it from developer.android.com (sdk only, no need of android studio)
-> Set ANDROID_HOME to sdk path
-> Set PATH to tools and platform-tools

3) Node.js
-> install it from https://nodejs.org/en/download/ (installer not binary)
-> Set PATH to node folder

4) Appium
-> Run follwing commands in terminal
-> npm install --global node-gyp
-> install rimraf -g
-> rimraf node_modules
-> npm install -g npm
-> npm install -g appium

-> npm install --global --production windows-build-tools (in case if above doesn't runs)
  • Setup on mobile
1) Connect phone to PC
2) Enable debugging mode and developer mode
3) Install Android File Transfer App  (Using this for apk transfer from mobile to mac)
4) Install ES file manager and take backup of the app to create apk file of that app
    This apk needs to be in our project folder under APK folder
5) Check if the device is connected to the PC using command "adb devices"

  • Project Setup
1) Download eclipse 
2) Add ADK plugin on eclipse :
6) Add maven-android plugin in pom.xml
7) Add appium java client jar  3.2.0
9) Starting appium on terminal
   http://stackoverflow.com/questions/24813589/how-to-setup-appium-on-mac-os-to-run-automated-tests-from-java-classes-on-androi

  • Terminal Commands
-> adb devices : Checking the devices connected to laptop and getting their id.
-> android : Opening android sdk.
-> android avd : Opening android virtual device manager



Challenges Faced

1. Finding a element that might not be currently visible on screen
    A major challenge as only the part that is visible on screen loads into the dom. So, we are not sure of the views that            might or might not be present.
    For those view which might load with or without swiping once the screen, separate methods are in PageHandler that        takes a extra argument 'scrollableElement'.
    Firstly, it checks if its currently in DOM with implicit wait of LESS_IMPLICIT_WAIT. If found, it returns it, else swipes the        screen. Agains its checks for the element, and if not found results an error.  

2. Iterating a complete list :
    We get no info about the total elements in the list and the index of each element as the index keeps on updating at            each swipe. So, extra description is added about it in content-desc attribute by mobile team.
    Hence, we fetch the total elements in list and iterate over each element by finding them using their content-desc                containing their actual index.

3. Keyboard : 
    The default keyboard might sometimes lead error as hides few views that should have been loaded, but gets hidden by     keyboard. So, we use appium inbuilt keyboard by setting the capabilities resetKeyboard and unicodeKeyboard to true.

4. Handling NumberPicker : 
    Does work property while dealing with multiple number-pickers simultaneously as the control goes on switching                randomly. So, instead of using their editText better tap on the increase or decrease the number for 10 secs.

Driver Capabilities

Desired capabilities are a set of keys and values (i.e., a map or hash) sent to theAppium server to tell the server what kind of automation session we're interested in starting up.
  • browserName : Name of mobile web browser to automate. Should be an empty string if automating an app instead
  • deviceName : The kind of mobile device or emulator to use
  • platformName : Which mobile OS platform to use.
  • newCommandTimeout : How long (in seconds) Appium will wait for a new command from the client before assuming the client quit and ending the session.
  • app : The absolute local path or remote http URL to an .apk file
  • appPackage : The package name of the app
  • appActivity : The app activity name to launch on startup
  • resetKeyboard and unicodeKeyboard : Set both of them to true to use appium inbuilt keyboard.
  • udid : The device id on which tests need to be run
  • no-reset : If true, then the apk does not install again once it is installed
  • address : The ip address to run appium server on
  •  port : The port to run appium server on
Grid Setup

1) Checking all the devices connected to PC
    Run command -> adb devices
    The device id's can be fetched out from it

Case 1 :
    Run on different devices parallely connected with single PC
        No need of grid
Assign a different port to each device
Pass two parameters(device id and port number) thru testNG xml file into test cases and add the corresponding port         number while creating driver

Case 2 :
    If we don't want to give port no in xml files, then we have to use Grid setup
    Create a hub on port 4444 and allow hub to assign port according to provide config files

Steps ->
1) Download selenium standalone jar and keep it inside the project
2) Open n+1 terminals for running n tests parallely (one for creating hub and other for running appium server)
3) Point the directory of all the terminals to the location where standalone jar is kept
4) Creating hub :
        Create a HubConfig.txt that contains all the configurations of the hub
        run command -> java -jar selenium-server-standalone-3.0.0-beta3.jar -role hub -hubConfig HubConfig.txt
        It contains the standalone jar name and the hubConfig file name
        The terminal shows up message selenium grid hub is up and running
5) Creating appium server:
        Create node.JSON device configuration file 
        run command appium --nodeconfig nodeConfigJSONFile
        appium --nodeconfig node2.JSON --address 127.0.0.1 -p 4727 --udid ON4TON75T4WSCU8S --no-reset
                                                            |                                |                |                                    |
                                                  node config file         ip address     port                          device id                            


Guidelines

  • Use setValue and replaceValue instead of sendKeys.
  • Use tap instead of click.
  • Avoid using scrollTo method, instead use swipe.
  • Explicit wait doesn't work fine with appium. Use the with proper care.
PageHandler

1)  There might be two possibilities while finding the element : Either the element is visible onscreen or the element is not currently visible on screen but would be visible on screen after a swipe.
2) If we are sure the element would be visible on screen (no need to swipe), then we follow normal method of finding element.
            public static AndroidElement findElement(String testId, AndroidDriver<AndroidElement> aDriver,PageElement                    pageObject)
            public static AndroidElement findElement(String testId, AndroidDriver<AndroidElement> aDriver,
            RepositoryParser repositoryParser, String pageName, String objectName)
            * Similarly for findElements and AndroidElement instead of AndroidDriver
3) If there is a possibility that the element might be visible on screen currently or might be possible to be visible after a swipe of the parent scrollView (might be a listView or ScrollView or something else that is scrollable), then the element is first found out within SCROLL_IMPLICIT_WAIT.
If found then it returns the element, else swipes the parent Scrollable Element and then finds again.
If then found, it returns the element, else throws the exception

4) Other than this, this class provides two more functionalities
  • public static AndroidElement waitForElementToBeVisisble(AndroidDriver<AndroidElement> aDriver, By byint                    waitSeconds
  • public static boolean waitForElementToBeUnvisible(AndroidDriver<AndroidElement> aDriver, By byint waitSeconds)
  • These methods wait for element to become visible/unvisible within provided timeFrame. waitSeconds refer to the number of times it should try to find the element within LESS_IMPLICIT_WAIT.
  • These are generally used while waiting for page to be loaded, progress bar, creating itinerary. 

Running a Test

1. Connect a device : 
    Connect the device to the laptop. Fetch its id by running the command "adb devices" on terminal.
2. Start a appium server :
    Run command : appium --address <ip-address> --port <port-no> --udid <device-id> --no-reset
    Default ip-address 127.0.0.1 and port 4723
    To use different ip or port, also need to change where the driver id created.
3. Starting the test
    Run the xml file

Comments

Post a Comment

Popular posts from this blog

API Automation using Rest Assured

Security Testing

Manage Jenkins