Tuesday 10 September 2019

Framework Using APPIUM with Python scripting

Why Mobile automation?

As the world is moving towards the more use of Mobile rather than using the desktop or laptop for any stuff like shopping, banking etc. For more information about mobile automation check here

How to automate Mobile app?

In today's world we need everything on mobile and the apps installed on device should be much more efficient to help us. Like you want to do shopping, so in this case the app should be responsive, with interactive UI's and should be able to handle the load of multiple users at same time. There are so many things which need to be taken care when you are automating an application. So in this we will consider functionality testing. We will consider dialer app of a smartphone, which is considered to be mostly used by user.

Basic concepts required for automation:

Before jumping directly to coding let us understand few concepts. I have considered Dialer app using page object model

So what exactly is Page Object in automation world? It is nothing but modulating our code page by page instead of writing everything in a single code. This way representation of code is easy to manage and easy to understand.

Appium basic concepts are required to be understood before moving further. You can go through here

Pre-requisite:

You need to install below setups on your system

  1. Python 2.7 (stable version)
  2. PyCharm Community Edition 
  3. Appium standalone server
  4. Selenium
  5. Appium Client

Installation documents are available here

App for Automation:

We have choose Dialer app for the automation. We will basically automate an android platform app.
To automate any app in appium we have to first find appPackage and appActivity
So the question here is how to find the appPackage and appActivity of an application. There are two methods to figure out this
1. By using adb command

adb shell
shell@LS-5016:/ $dumpsys window windows | grep -E mCurrentFocus|mFocusedApp'

2. By using aapt dump badging command

Detail about this could be found here

Inspecting element:

Once we have decided which app to test and automate, lets see how to automate same using appium. First thing is to launch the appium server which you halve already installed on your machine.There are two ways which you can inspect UI element
1. Using uiautomator
2. Using UI inspector available in appium

Inspecting element using uiautomator detail information is available here

Here we will see in detail about inspecting element using appium inspector.
First step is to launch the appium server

Then we have to start the server

 Step 2 is to start a new session. We need to click on file-> New session window
It will display a new window where we have to set the desired capabilities like as below

And click on Start session button
So here we could see different properties of UI element. For example
If if wish to click on highlighted area it will display
com.android.dialer:id/floating_action_button_container

various actions performed on the element could be found here and various ways to identify elements could be found here

Automation code goes here:

So finally how to put them all together in a code.

webdriver.py
from appium import webdriver

class Driver:

    def __init__(self):

        desired_caps = {
            'platformName': 'android',
            'deviceName': 'LYF WIND7i',
            'appPackage': 'com.android.dialer',
            'appActivity': 'com.android.dialer.DialtactsActivity',
}
self.instance = webdriver.Remote("http://localhost:4723/wd/hub",desired_caps)
 

In this .py file we are mentioning the desired capabilities required for our test.

dialer_screen.py


from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as e

class DialerScreen:

    def __init__(self, driver):
        self.driver = driver
        self.floating_action_button = WebDriverWait(self.driver.instance, 5).
                                     until(e.visibility_of_element_located
                                     ((MobileBy.ID, "com.android.dialer:id/
                                     floating_action_button")))

    def dial_number(self):
        # Dial a number        
        dial_digit_buttons = self.driver.instance.find_element(MobileBy.ID , 
                                  "com.android.dialer:id/digits")
        dial_digit_buttons.send_keys('number')

    def click_call_button(self):
        press_call_button = self.driver.instance.find_element(MobileBy.ID,
                        "com.android.dialer:id/dialpad_floating_action_button")
        press_call_button.click()

    def click_floating_action_button(self):
        floating_action_button = self.driver.instance.find_element(MobileBy.ID,
                                "com.android.dialer:id/floating_action_button")
        floating_action_button.click()

    def click_end_call_button(self):
        press_end_call_button = self.driver.instance.find_element(MobileBy.ID,
                      "com.android.dialer:id/floating_end_call_action_button")

        press_end_call_button.click()


So here we have written code for the dialer screen as per page object model.

And finally comes our test execution script. Call_from_dialer.py
import unittest
from webdriver.webdriver import Driver
from apps.dialer.dialer_screen import DialerScreen

class DialerTestCases(unittest.TestCase):

    def setUp(self):
        self.driver = Driver()

    def test_dialer_launches(self):
        dialer = DialerScreen(self.driver)
        dialer.click_floating_action_button()
        dialer.dial_number()
        dialer.click_call_button()
        from time import sleep
        sleep(10)
        dialer.click_end_call_button()

     def tearDown(self):
        self.driver.instance.quit()

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(DialerTestCases)
    unittest.TextTestRunner(verbosity=2).run(suite)

So here you could see that,

  • def setUp(self) this method invokes the desired capabilities before we could run the actual test script.
  • test_dialer_launches(self) this method actual have the test steps which need to be carried on in order to execute it.
  • Finally the tearDown(self) method is to quit from the test.

 The file structure would look like

The test result would be as below
To run test simply right click and select run unittest for Call_from_dialer.py


Tuesday 9 July 2019

Ways to identify App Package name and App Activity

app package name and App activity
Why there is need of App Package name and App Activity?

App Package name & App Activity name is required when we are doing automation for a particular app.

For example, if we want to automate dialer app using APPIUM (which I will be covering in upcoming posts) , then you need to have app package name & app activity.

Method 1: By using adb command

This is how we can have app package name and app activity

Dialer App
Fig 1. Select the Dialer app and launch


Dialer app
Fig 2. Dialer App launched
To get the package name & activity name run below command after launching the dialer app
adb shell 
shell@LS-5016:/ $dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
Below will be output on the command prompt

Fig 3. Run above command from cmd prompt
Method 2 : By Using aapt dump badging command

For this method, I have considered Amazon app. First we have to place the apk at given path C:\Users\Administrator\AppData\Local\Android\Sdk\build-tools\28.0.3

Later we can run below command at command prompt
C:\..\AppData\Local\Android\Sdk\build-tools\28.0.3>aapt dump badging "C:\Users\Administrator\AppData\Local\Android\Sdk\build-tools\28.0.3\Amazon Shopping_v18.12.0.100_apkpure.com.apk"
Fig 4. Package name using aapt

Fig 5. App Activity using aapt



Friday 14 June 2019

All about adb shell dumpsys

adb shell dumpsys
What is dumpsys?

This was the word always ticking my head whenever I use to talk to vendors. They always needed this one. So I started reading about it and how it benefited them to resolve the android related issues.
dumpsys : It is nothing but a tool which gives the status of services running on device.

Below is the command which shows the list of all services along with the package name
adb shell service list
Output will be as below

adb shell service list
Fig 1 shell service list

So next question is how to identify the services. So below commands give you list of service
adb shell dumpsys -l
Output will be as below

adb shell dumpsys -l
Fig 2 dumpsys -l
There are many filters which you can use for analyzing the dumpsys data

1. dumpsys activity

It shows the list of activities running at that moment on device
adb shell dumpsys  activity
adb shell dumpsys activity
Fig 3 dumpsys activity


2. dumpsys batterystats

This generates the statistical data for battery usage on device.
adb shell dumpsys batterystats

adb shell dumpsys batterystats
Fig 4 dumpsys batterystats

3. dumpsys cpuinfo

This adb command display the CPU information utilized by device
adb shell dumpsys cpuinfo
adb shell dumpsys cpuinfo
Fig 5 dumpsys cpuinfo

Above output you can see that gaana app have utilized 17% of CPU as it was used last.

4. dumpsys wifi

This command displays the status of wifi
adb shell dumpsys wifi
adb shell dumpsys wifi
Fig 6 dumpsys wifi


5. dumpsys meminfo

This command displays the memory information of the device
adb shell dumpsys meminfo
adb shell dumpsys meminfo
Fig 7 dumpsys meminfo

6. dumpsys procstats

This command displays the process stats
adb shell dumpsys procstats --hours 3
adb shell dumpsys procstats
Fig 8 dumpsys procstats
For more on dumpsys refer to https://developer.android.com/studio/command-line/dumpsys

Tuesday 11 June 2019

Troubleshooting - Unable to detect device using adb devices

Unable to detect device using adb devices
There could be many situations where your device connected to system is not detected. Few of the scenarios are explained below. Apart from below scenarios if you face any trouble let us know in comment section.

1. adb devices command doesn't display any result.

For this problem you have to enable the developer option available at Setting -> More settings -> About Phone -> Tap five times on the Software version. A toast message is displayed that "Its in developer mode". Please refer adb devices page for details.
Step 1. Developer Mode selected
Step 2. Developer option is available
Step 3. USB debugging enabled

Once the debugging mode is enabled, now if you fire adb devices command, it will display below as still RSA key is not mapped and device shows us unauthorized



Fig 4. Device Unauthorized



Fig 5. RSA key pop up


Fig 6. Select the option as always



Fig 7. Device detected

2. Device not detected when connected to laptop

There could one reason here that in android 7 onward devices are connected in charge only mode. So you need to select the option from usb charge only to file transfer



 So once the Files mode is selected your device get detected.

3. Device not detected even though its mass storage is detected.

So there are situations in which device internal storage is detected but adb devices command returns nothing. For such scenarios please check below solution. Here we have considered ubuntu OS.

Step 1 : adb devices
This returns nothing

Step 2: lsusb
Once you run this command below result is displayed


Step 3: Check whether adb_usb.ini file exist on your system. In my case it was not available so we will install it with below commands
a. mkdir --parent $HOME/.android
b. wget -O $HOME/.android/adb_usb.ini https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/android/adb_usb.ini
c. vi adb_usb.ini
d. add the device code here and save
e. To save press Esc : wq

Step 4 : Once adb_usb.ini file is downloaded, add the code in the file and save


Fig 1. Downloaded adb_usb.ini


Fig 2. Added the code for mediatek device

Step 5 : Now run the adb devices command. Your device will be detected.

Why this happens?

The android sdk have qualcomm based codes already in the adb_usb.ini file, but for few other chipset it is not available then we have to add manually and save.

Tuesday 4 June 2019

ADB Network Commands

1. adb shell netstat

In computing, netstat (network statistics) is a command-line network utility tool that displays network connections for the Transmission Control Protocol (both incoming and outgoing).
adb netstat command is used for network statistics . This command is usually required to analyse the network statistics for an android device. Below pic shows the actual output of the command

netstat

2. adb shell ping 

PING command returns the network response if the device is connected to INTERNET. Ping command can be used with below options

ping [-c count]
        [-i interval]
        [-I interface]
        [-m mark]
        [-M pmtudisc_option]
        [-l preload]
        [-p pattern]
        [-Q tos]
        [-s packetsize]
        [-S sndbuf]
        [-t ttl]
        [-T timestamp_option]
        [-w deadline]
        [-W timeout]

The output of the adb shell ping command will be as below

ping

Ctrl+c is used to stop the ping command. You can also give give limited lines to be displayed on screen as a result of ping command.

adb shell ping www.facebook.com -c 4
3.  adb shell netcfg

adb shell netcfg command is used to show, manipulate routing, configure and manage network connections via profiles. Below is the result of netcfg command

netcfg
4. adb shell ip

adb shell ip command show, manipulate routing, devices, policy routing and tunnels

ip [OPTIONS ] OBJECT
OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |tunnel | tuntap | maddr | mroute | mrule | monitor| xfrm |netns | l2tp }

OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |-f[amily] { inet | inet6 | ipx | dnet | link } |-l[oops] { maximum-addr-flush-attempts } |-o[neline] | -t[imestamp] | -b[atch] [filename] |-rc[vbuf] [size]}

Example: adb shell ip -f inet addr show wlan0
                (show WiFi IP Address)





Adb over Wi-Fi

ADB Network Command
Sometimes its required to do the testing without using usb cable. That means we don't have to keep the device in charging mode but we want to run our scripts. In this case we can use adb over Wi-Fi. Lets see in detail, what is adb over Wi-Fi?

Step 1:
Connect device to system using usb

Step 2:
adb device
This will give you the list of devices connected to system
Result - List of devices attached
            ######## device

Step 3:
adb tcpip 5555
Result - restarting in TCP mode port: 5555

Step 4:
Find out the IP address of the device. Settings -> About -> Status -> IP address

Step 5:
adb connect #.#.#.#
Result - connected to #.#.#.#:5555

Step 6:
Now remove the usb cable and run below command to confirm whether the device is still connected or not

adb devices
Result - List of devices attached
#.#.#.#:5555 device

Monday 6 May 2019

Mobile UI Automation Using Python uiautomator Module

Mobile UI Automation Using Python uiautomator Module

Let's see how we can automate mobile UI using python uiautomator module. It works on Android 4.1 + .
Install python 2.7 on your system.First you need to install uiautomator package


1. How to Automate call functionality using uiautomator.

This is how you identify the element from uiautomator. You have to pick the unique properties like className,packageName,Text,resourceId etc. by using uiautomator.


from uiautomator import device as d
from uiautomator import Device
from time import sleep
import os
def VoLTE_call():
  print("Inside Call Method.")
  call = d(className='android.widget.TextView',
                     packageName='com.cyanogenmod.trebuchet',
                     text ='Phone').click()
  sleep(2)
  dialer = d(className='android.widget.ImageButton',
           packageName='com.android.dialer',
           resourceId='com.android.dialer:id/
                       floating_action_button').click()
  sleep(1)
  dialling_page = d(className='android.widget.EditText',
                    packageName='com.android.dialer',
                    resourceId='com.android.dialer:id/digits')
                                    .set_text(number)
  sleep(1)
  press_call_button = d(className='android.widget.ImageButton',
                    packageName='com.android.dialer',
                    resourceId='com.android.dialer:id/
                                dialpad_floating_action_button').click()
  sleep(5)
  #hold call
  hold = d(className='android.widget.ImageButton',
                    packageName='com.android.dialer',
                    resourceId='com.android.dialer:id/holdButton')
                             .click()
  sleep(5)
  #unhold Call
  unhold = d(className='android.widget.ImageButton',
                    packageName='com.android.dialer',
                    resourceId='com.android.dialer:id/holdButton')
                             .click()
  sleep(5)
  # mute Call
  mute= d(className='android.widget.ImageButton',
             packageName='com.android.dialer',
             resourceId='com.android.dialer:id/muteButton').click()
  sleep(5)

  #unmute call  
  unmute= d(className='android.widget.ImageButton',
             packageName='com.android.dialer',
             resourceId='com.android.dialer:id/muteButton').click()
  sleep(5)
  #speaker on  
  speaker_on= d(className='android.widget.ImageButton',
           packageName='com.android.dialer',
           resourceId='com.android.dialer:id/audioButton').click()
  sleep(5)
  # speaker off  
  speaker_off = d(className='android.widget.ImageButton',
             packageName='com.android.dialer',
             resourceId='com.android.dialer:id/audioButton').click()
  sleep(5)
  press_endcall_button = d(className='android.widget.ImageButton',
                        packageName='com.android.dialer',
                        resourceId='com.android.dialer:id/
                              floating_end_call_action_button').click()
  d.press.home()
#Call The main Method
VoLTE_call()
print(""Your test is completed...")


Friday 3 May 2019

How to Capture screenshot and video using adb commands

adb command There are multiple situations where you want to capture the screenshots or video and store in a folder all programmatically. This is simple and one line adb command that can be used to capture the screenshot and video.

Capture Screenshot:

Please find the below steps to capture the screenshot of an android device

1. Connect device to laptop. To confirm whether device is connected or not, run below command
 2. Screencap is the adb command which is used for capturing the screenshots



3. Once the screenshot is captured, its time to pull out from device to laptop.



The image is pulled on your system.

Capture Video:

Please find the below steps to capture the video of an android device. This is generally required when you want to give steps for reproducing certain issues.

1. Connect device to laptop. To confirm whether device is connected or not, run below command


2. Screenrecord is the adb command which is used for capturing the screenshots


3. Once the screen activities are recorded press ctrl+C to stop the recording.

The video which was captured is stored on sdcard. To pull out the video from sdcard please run below command

Here is simple program that depicts how programatically you can use above two commands
Same way we can also write script for video recording.


Friday 26 April 2019

Locating UI Element Using uiautomator

How to use uiautomator for inspecting UI elements on mobile?

So there are multiple ways available by which you can identify the element on mobile. some of them as below. So here I am taking an example of Hotstar app for illustrating the various ways of element identification. Also I am using python as my scripting language.

1. By ID

This is the most efficient way to locate an element as IDs are unique. This is the most prefered way to identify the elements of android app. 


So for this the code will be as below.
app_opened=d(packageName='in.startv.hotstar', resourceId='in.startv.hotstar:id/iv_ad_background').wait.exists(timeout=20000)  
You can see that I have highlighted the portion where it depicts that element is identified by ID.

2. By className:

A class contains many elements. So you have to little careful while using element identification using class name because if an element has many classes then it will match against each of them. 



So for this the code will be as below.
home_page_click=d(packageName='in.startv.hotstar', className='android.support.v4.view.ViewPager',resourceId='in.startv.hotstar:id/pager').click()

3. By Name:

It is possible to locate the element by name but they should be unique.



So for this the code will be as below.
dream11=d(packageName='in.startv.hotstar',resourceId='in.startv.hotstar:id/tv_header',  text='Dream11').click()  

4. By Xpath:

The very last option to be used for identifying the elements. You end up in many situations that you don't have option but to use xpath for identifying the elements.There are two types of xpath, relative xpath and absolute xpath. It is suggested to use relative xpath because absolute xpath uses indexes and if there is any change in the code, it will result in the failure of test scripts which is trying to identify the UI element.

So for this the code will be as below.
dream11 = d(By.xpath("//*[@text = 'Dream11']"))



Different actions performed using uiautomator or Appium

Perform Actions

Once the element is identified, you want to perform different actions just like a normal user does on mobile.

So different actions which you can be performed are:

1. click():
     Clicks the center of the visible UI element

2. dragTo():
    Drags this object to arbitrary coordinates.

3.setText():
   Allows you to set a text in editable text field.

4. ClearTextField():
    This method is used to clear the text field if any text is already available.

5. swipeUp():
    Performs the swipe up action on the UI element.

6. swipeLeft():
    Perform the swipe left action on the UI element.

7. swipeRight():
    Perform the swipe right action on the UI element.

8. swipeDown():
    Perform the swipe down action on the UI element.

Thursday 25 April 2019

Ways to identify mobile app elements

How to identify the elements on mobile UI

You can identify element using either uiautomator viewer or using appium. Let me show you how you can use the uiautomator for identifying the UI elements and later on I will explain element identification using appium

Element identification using uiautomator viewer

It is convenient GUI Tool to scan and identify the UI elements of the app which is currently displayed on the device under test. uiautomator viewer is located at <android-sdk>/tools/bin




Double click on the batch file and you will be able see the uiautomatorviewer. Once you have clicked on the uiautomatorviewer, it will display a window which would be like as below


To check the elements on UI , you need to click on third element from element and then below screen will appear on uiautomatorviewer (Make sure you have connected a mobile device to laptop)

Wednesday 8 August 2018

Why Mobile Automation Required?



             To compete in this very competitive world of smartphones we have to deliver a quality product and need more revenue in small tenure then it is necessary for an organisation to go for Mobile automation testing. Mobile automation will provide the various solutions, which helps you to verify and validate the overall functionality of the Mobile apps(native as well hybrid apps). Automation can be done to achieve better coverage in less duration with better test results.



Types of Apps available for Smartphones:
There are 3 types of apps available in smartphones. Below are the types
  1. Native App
  2.  Hybrid App
  3. Web Apps

How to Automate??

There are so many tools available for automation testing. Some are paid and some are open source.Depending on the requirement of the project one should decide the automation tool for automation.

Here are some examples of tools:
  1. Appium
  2. Robotium
  3. Selendroid(for Android)
  4. UI Automator
  5. Ranorex
  6. MonkeyRunner
  7. SeeTest


Feature Posts

Python Appium - Step by step procedure for capturing screenshot

 Why To capture screenshot? It is as important as your logs. If there is any failure for any test scenario, we can provide screenshot for th...