Wednesday, 10 June 2015

Android Battery Drain analysis via Qpython

Hello Everybody !

Hope my previous scripts helped you guys in understanding Monkey Runner Advanced Concepts !

Script description :

The below script helps to make a track on android device battery draining and charging
After every 10 degree change in battery level android device will speak and tell you what the current level

import time
import androidhelper
droid = androidhelper.Android()
droid.batteryStartMonitoring()
new = ''
neww = ''
print 'script start'
while True :
    batteryLevel = droid.batteryGetLevel()
    new = new + str(batteryLevel[1])
    time.sleep(2)
    NewLevel = droid.batteryGetLevel()
    neww = neww + str(NewLevel[1])
    if new == neww:
       continue
    else:
        print NewLevel[1],time.asctime()
        new = neww
        if NewLevel[1] % 10 == 0:
            print 'Speaking starts'
            droid.ttsSpeak('hi dheeraj.Your phone battery is' + str(NewLevel[1]))


Sunday, 1 February 2015

Python script to open any application without manually typing touch coordinates

The below script was my dream project which i am presenting in front of you all.

I was facing a huge problems to manually write touch coordinates in my scripts before execution and sometimes those coordinates go wrong and performs a different function in Android.

Script description: Those who do not want to waste time in manually typing coordinates can use this this and make Android testing fully automated

Script :

import sys
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

def call(d):
    s = 'event3'
    f_log = open('C:\cygwin\home\dheeraj.bajaj\getevent.txt')
    p = 3
    f = open('C:\cygwin\home\dheeraj.bajaj\out4.txt','w')  
    for line in f_log.read().splitlines():
        if "event3:" in line :
            if p == 2 or p == 1 :          
                a = line.split(' ')
                print a[3]
                b = a[3]
                i = int(b,16)
                i_log = str(i).split()
                #saveout = sys.stdout              
                print i_log  
                f.write(str(i))
                f.write("\n")                
            p-=1  
    f.close()     
    f_log.close()  

    f1 = open('C:\cygwin\home\dheeraj.bajaj\out4.txt')
    f2 = f1.readlines()  
    print int(f2[0])
    x = int(f2[0])
    print int(f2[1])
    y = int(f2[1])   
    d.touch(x,y,'DOWN_AND_UP')
    f1.close()       
           
def main():
    print "Start"
    device = MonkeyRunner.waitForConnection()
       
    if not device:
        print "Couldn't get connection"
        sys.exit()
   
    print "Found device"
    call(device)
   

if __name__ == '__main__':
    main()    




Thursday, 29 January 2015

Python script to print touch coordinates using getevent command

We have prepared a batch file containing two major commands described as below :

      1. adb shell getevent -c 200 > getevent.txt
       Description:
       The above command will create 200 events including the touch event given by user
       manually. The touch event given by user will be differentiated via 'event3'

How does getevent works ? 
adb shell getevent ( connect device and use this command )
Which returns something like:
C:\>adb shell getevent
add device 1: /dev/input/event0
name: “qwerty2″
could not get driver version for /dev/input/mouse0, Not a typewriter
could not get driver version for /dev/input/mice, Not a typewriter
/dev/input/event0: 0001 0001 00000000
/dev/input/event0: 0003 0000 00000073
/dev/input/event0: 0003 0001 000000a8
/dev/input/event0: 0003 0001 00000014
/dev/input/event0: 0003 0001 00000014
/dev/input/event0: 0003 0001 00000000
/dev/input/event0: 0003 0001 00000000
/dev/input/event0: 0003 0001 00000000
/dev/input/event0: 0001 014a 00000001
        Which returns everything in hexidecimal, but you can’t send these commands directly back, you         had to convert them to decimal,
The actual X axis coordinates : 00000073 
The actual Y axis coordinates : 000000a8
These coordinates needs to be changed to decimal using python and monkeyrunner

2. monkeyrunner.bat testcall3.py
Description :
This command will execute following functions :
a. Open the getevent text file
b. Search for the touch events ( eg., search for 'event3')
c. Display the Hex format coming infront of event3 lines
d. Convert hex to int and then to decimal
e. Finally display the X and Y coordinates

Continue sharing interesting scripts !!
Thanks !!!

Friday, 23 January 2015

Monkey Scripts runtime error detection via ADB Logcat

This is my first script in front of you all regarding runtime error detection in Monkey Runner scripts. Hope you will enjoy it !!

Script Description :

1. Started any activity ( eg., open dialer)
2. Recording logs at the same time
3. finding a word in logs via find() function
4. If script shows error ( throws exception or opens any other activity apart from dialer) then capturing screenshot
5. Saving logs and moving to the other test case with the use of break function in python

Script :

 

 

 

 

 

 

 

 

 

Will share some more interesting scripts soon !