#coding=utf8
from time import sleep
from appium import webdriver
import os
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)

)

class appDriver():
    def __init__(self,version=6.0):
        try:
            self.desired_caps = {}
            self.desired_caps ['platformName'] = 'Android'
            self.desired_caps ['platformVersion'] =version
            self.desired_caps ['deviceName'] ='PE-UL00'
            self.desired_caps ['app'] = PATH('../APK/xmlay.apk')
            self.appdriver= webdriver.Remote('http://localhost:4723/wd/hub',  self.desired_caps )
        except Exception,e:
            print "Driver Error:",e

    #根据“控件text属性的内容”构造出UiSelector对象
    def app_find_element_by_text(self,text):
        try:
            item='new UiSelector().text('+"\""+text+"\""+')'
            #进行费时装换,如果不进行格式这步格式转换,会出现HTTP错误
            item="%s" %(item)
            element=self.appdriver.find_element_by_android_uiautomator(item)
            return element
            self.appdriver.f
        except Exception,e:
            print "By Text Error:",e

    #根据“控件text属性包含的内容”构造出UiSelector对象
    def app_find_elements_by_textContains(self,text):
        try:
            item='new UiSelector().textContains'+"("+text+")"
            item="%s" %(item)
            element=self.appdriver.find_elements_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By textContains Error:",e

    #根据“控件text属性正则表达式的内容”构造出UiSelector对象
    def app_find_elements_by_textMatches(self,text):
        try:
            item='new UiSelector().textMatches'+"("+text+")"
            item="%s" %(item)
            element=self.appdriver.find_elements_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By textMatches Error:",e

    #根据“控件text属性开始的内容”构造出UiSelector对象
    def app_find_elements_by_textStartsWith(self,text):
        try:
            item='new UiSelector().textStartsWith'+"("+text+")"
            item="%s" %(item)
            element=self.appdriver.find_elements_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By resourceId Error:",e

    #根据“控件content-desc属性的内容”构造出UiSelector对象
    def app_find_element_by_description(self,text):
        try:
            item='new UiSelector().description'+"("+text+")"
            item="%s" %(item)
            element=self.appdriver.find_element_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By description Error:",e

    #根据“控件content-desc属性包含的内容”构造出UiSelector对象
    def app_find_elements_by_descriptionContains(self,text):
        try:
            item='new UiSelector().descriptionContains'+"("+text+")"
            item="%s" %(item)
            element=self.appdriver.find_elements_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By descriptionContains Error:",e

    #根据“控件content-desc属性正则表达式的内容”构造出UiSelector对象
    def app_find_elements_by_descriptionMatches(self,text):
        try:
            item='new UiSelector().descriptionMatches'+"("+text+")"
            item="%s" %(item)
            element=self.appdriver.find_elements_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By descriptionMatches Error:",e

    #根据“控件content-desc属性正则表达式的内容”构造出UiSelector对象
    def app_find_elements_by_descriptionStartsWith(self,text):
        try:
            item='new UiSelector().descriptionStartsWith'+"("+text+")"
            item="%s" %(item)
            element=self.appdriver.find_elements_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By descriptionStartsWith Error:",e

    #根据资源resource-id获取对象
    def app_find_element_by_resourceId(self,resourceid):
        try:
            item='new UiSelector().resourceId'+"("+resourceid+")"
            item="%s" %(item)
            element=self.appdriver.find_element_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By Text Error:",e

    #根据资源根据资源resource-id的正则表达式获取对象
    def app_find_elements_by_resourceIdMatches(self,resourceid):
        try:
            item='new UiSelector().resourceIdMatches'+"("+resourceid+")"
            item="%s" %(item)
            element=self.appdriver.find_elements_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By resourceIdMatches Error:",e

    #根据资源根据资源class的正则表达式获取对象
    def app_find_element_by_className(self,className):
        try:
            item='new UiSelector().className'+"("+className+")"
            item="%s" %(item)
            element=self.appdriver.find_element_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By resourceIdMatches Error:",e

    #根据资源根据资源index的正则表达式获取对象
    def app_find_element_by_index(self,index):
        try:
            item='new UiSelector().index'+"("+index+")"
            #
            item="%s" %(item)
            element=self.appdriver.find_element_by_android_uiautomator(item)
            return element
        except Exception,e:
            print "By index Error:",e
    def quitApp(self):
        self.appdriver.quit()



def test():
    app=appDriver()
    try:
            sleep(5)
            el =app.app_find_element_by_text('允许')
            if el:
                el.click()
    except Exception,e:
            print e
        #
    try:
            #拨打电话和管理同话
            sleep(5)
            el = app.app_find_element_by_text("禁止后不再询问")
            if el:
                el.click()
            sleep(5)
            el = app.app_find_element_by_text("始终允许")
            if el:
                el.click()
    except Exception,e:
            print e
        #访问SD卡
    try:
            sleep(5)
            el = app.app_find_element_by_text("禁止后不再询问")
            if el:
                el.click()
            sleep(5)
            el = app.app_find_element_by_text("始终允许")
            if el:
                el.click()
    except Exception,e:
            print e

        #使用此设备的位置信息
    try:
            sleep(5)
            el = app.app_find_element_by_text("禁止后不再询问")
            if el:
                el.click()
            sleep(5)
            el = app.app_find_element_by_text("始终允许")
            if el:
                el.click()
    except Exception,e:
            print e

    app.quitApp()

if __name__=="__main__":
    test()

03-14 23:24