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()