Exemplo n.º 1
0
 def touch(x=None, y=None):
     """
     触摸事件
     usage: touch(e), touch(x=0.5,y=0.5)
     """
     shell_cmd("input tap %s %s" % (str(x), str(y)))
     time.sleep(0.5)
Exemplo n.º 2
0
 def adb_image(path):
     shell_cmd("rm /sdcard/screenshot.png")
     shell_cmd("/system/bin/screencap -p /sdcard/screenshot.png")
     log.info(">>>截取屏幕成功,在桌面查看文件。")
     c_time = time.strftime("%Y_%m_%d_%H-%M-%S")
     image_path = os.path.join(path, c_time + ".png")
     adb_cmd('pull /sdcard/screenshot.png %s"' % image_path)
Exemplo n.º 3
0
 def call_phone(number):
     """
     启动拨号器拨打电话
     usage: callPhone(10086)
     """
     shell_cmd("am start -a android.intent.action.CALL -d tel:%s" %
               str(number))
Exemplo n.º 4
0
 def long_press_element(e):
     """
    长按元素, Android 4.4
     """
     shell_cmd("input swipe %s %s %s %s %s" %
               (str(e[0]), str(e[1]), str(e[0]), str(e[1]), str(2000)))
     time.sleep(0.5)
Exemplo n.º 5
0
 def swipe_by_coord(start_x, start_y, end_x, end_y, duration=" "):
     """
     滑动事件,Android 4.4以上可选duration(ms)
     usage: swipe(800, 500, 200, 500)
     """
     shell_cmd("input swipe %s %s %s %s %s" %
               (str(start_x), str(start_y), str(end_x), str(end_y),
                str(duration)))
     time.sleep(0.5)
Exemplo n.º 6
0
 def press_key(event_keys):
     """
     发送一个按键事件
     args:
     - event_keys -:
     http://developer.android.com/reference/android/view/KeyEvent.html
     usage: sendKeyEvent(event_keys.HOME)
     """
     shell_cmd("input keyevent %s" % str(event_keys))
     time.sleep(0.5)
Exemplo n.º 7
0
 def touch_by_ratio(ratioWidth, ratioHigh):
     """
     通过比例发送触摸事件
     args:
     - ratioWidth -:width占比, 0<ratioWidth<1
     - ratioHigh -: high占比, 0<ratioHigh<1
     usage: touchByRatio(0.5, 0.5) 点击屏幕中心位置
     """
     shell_cmd("input tap %s %s" %
               (str(ratioWidth * Actions.get_screen_resolution()[0]),
                str(ratioHigh * Actions.get_screen_resolution()[1])))
     time.sleep(0.5)
Exemplo n.º 8
0
 def input_text(text):
     text_list = list(text)
     specific_symbol = set(['&', '@', '#', '$', '^', '*'])
     for i in range(len(text_list)):
         if text_list[i] in specific_symbol:
             if i - 1 < 0:
                 text_list.append(text_list[i])
                 text_list[0] = "\\"
             else:
                 text_list[i - 1] = text_list[i - 1] + "\\"
     seed = ''.join(text_list)
     shell_cmd('input text "%s"' % seed)
Exemplo n.º 9
0
 def swipe_by_ratio(start_ratioWidth,
                    start_ratioHigh,
                    end_ratioWidth,
                    end_ratioHigh,
                    duration=" "):
     """
     通过比例发送滑动事件,Android 4.4以上可选duration(ms)
     usage: swipeByRatio(0.9, 0.5, 0.1, 0.5) 左滑
     """
     shell_cmd("input swipe %s %s %s %s %s" % (str(start_ratioWidth * Actions.get_screen_resolution()[0]), str(start_ratioHigh * Actions.get_screen_resolution()[1]), \
                                          str(end_ratioWidth * Actions.get_screen_resolution()[0]), str(end_ratioHigh * Actions.get_screen_resolution()[0]), str(duration)))
     time.sleep(0.5)
Exemplo n.º 10
0
 def get_battery_level():
     """
     获取电池电量
     """
     level = shell_cmd("dumpsys battery |{0} level".format(
         Actions.find_type)).split(": ")[-1]
     return int(level)
Exemplo n.º 11
0
 def send_text(string):
     """
     发送一段文本,只能包含英文字符和空格,多个空格视为一个空格
     usage: sendText("i am unique")
     """
     text = str(string).split(" ")
     out = []
     for i in text:
         if i != "":
             out.append(i)
     length = len(out)
     for i in range(length):
         shell_cmd("input text %s" % out[i])
         if i != length - 1:
             Actions.press_key(62)
     time.sleep(0.5)
Exemplo n.º 12
0
 def get_battery_temperature():
     """
     获取电池温度
     """
     temp = shell_cmd("dumpsys battery | {0} temperature".format(
         Actions.find_type)).split(": ")[-1]
     return int(temp) / 10.0
Exemplo n.º 13
0
 def get_third_app_list():
     """
     获取设备中安装的第三方应用包名列表
     """
     thirdApp = []
     for packages in shell_cmd("pm list packages -3").split("\r\n")[:-1]:
         thirdApp.append(packages.split(":")[-1])
     return thirdApp
Exemplo n.º 14
0
 def get_system_app_list():
     """
     获取设备中安装的系统应用包名列表
     """
     sysApp = []
     for packages in shell_cmd("pm list packages -s").split("\r\n")[:-1]:
         sysApp.append(packages.split(":")[-1])
     return sysApp
Exemplo n.º 15
0
 def cpu_kel(self):
     get_cmd = shell_cmd("cat /proc/cpuinfo")
     find_str = "processor"
     int_cpu = 0
     for line in get_cmd.split("\r\n"):
         if line.find(find_str) >= 0:
             int_cpu += 1
     return str(int_cpu) + "核"
Exemplo n.º 16
0
 def men_total(self):
     get_cmd = shell_cmd("cat /proc/meminfo")
     men_total = 0
     men_total_str = "MemTotal"
     for line in get_cmd.split("\r\n"):
         if line.find(men_total_str) >= 0:
             men_total = line[len(men_total_str) + 1:].replace("kB", "").strip()
             break
     return int(men_total)
Exemplo n.º 17
0
 def get_focused_package_and_activity():
     '''
     获取当前应用界面的包名和Activity,返回的字符串格式为:packageName/activityName, 使用正则从如下字符串中提取
     mResumedActivity: ActivityRecord{149fa48 u0 com.dai.testsave/.MainActivity t86}
     :return:
     '''
     pattern = re.compile(r"mResumedActivity: ActivityRecord{.+\s(.+)\s.+}")
     out = shell_cmd("dumpsys activity ")
     return pattern.findall(out)[0].strip()
Exemplo n.º 18
0
 def clear_app_data(packageName):
     """
     清除应用用户数据
     usage: clearAppData("com.android.contacts")
     """
     if "Success" in shell_cmd("pm clear %s" % packageName):
         return "clear user data success "
     else:
         return "make sure package exist"
Exemplo n.º 19
0
 def get_screen_resolution():
     '''
     :return:返回 x,y 的坐标值
     '''
     result = shell_cmd("wm size")
     if result != None and result != "":
         result = result.split(":")[-1].strip()
         x = result.split("x")[0]
         y = result.split("x")[1]
         return (int(x), int(y))
     else:
         result = shell_cmd("dumpsys window displays | {0} init=".format(
             Actions.find_type))
         "    init=1080x1920 480dpi cur=1080x1920 app=1080x1920 rng=1080x1008-1920x1848"
         result = result.strip().split(" ")[0].split("=")[1]
         x = result.split("x")[0]
         y = result.split("x")[1]
         return (int(x), int(y))
Exemplo n.º 20
0
 def long_press_key(event_keys):
     """
     发送一个按键长按事件,Android 4.4以上
     usage: longPressKey(event_keys.HOME)
     POWER = 26
     BACK = 4
     HOME = 3
     MENU = 82
     VOLUME_UP = 24
     VOLUME_DOWN = 25
     SPACE = 62
     BACKSPACE = 62
     ENTER = 66
     MOVE_HOME = 122
     MOVE_END = 123
     """
     shell_cmd("input keyevent --longpress %s" % str(event_keys))
     time.sleep(0.5)
Exemplo n.º 21
0
 def get_permission_list(package_name):
     PATH = lambda p: os.path.abspath(p)
     permission_list = []
     result_list = shell_cmd(
         "dumpsys package {0} | {1} android.permission".format(
             package_name, Actions.find_type))
     for permission in result_list.split("\r\n"):
         if permission != "":
             permission_list.append(permission.strip())
     return permission_list
Exemplo n.º 22
0
 def get_matching_app_list(keyword):
     """
     模糊查询与keyword匹配的应用包名列表
     usage: getMatchingAppList("qq")
     """
     matApp = []
     for packages in shell_cmd("pm list packages %s" %
                               keyword).split("\r\n")[:-1]:
         matApp.append(packages.split(":")[-1])
     return matApp
Exemplo n.º 23
0
    def get_crash_log(path):
        # 获取app发生crash的时间列表
        time_list = []
        result_list = shell_cmd("dumpsys dropbox | {0} data_app_crash".format(
            Actions.find_type))
        for time in result_list:
            temp_list = time.split(" ")
            temp_time = []
            temp_time.append(temp_list[0])
            temp_time.append(temp_list[1])
            time_list.append(" ".join(temp_time))

        if time_list is None or len(time_list) <= 0:
            log.info(">>>No crash log to get")
            return None
        c_time = time.strftime("%Y_%m_%d_%H-%M-%S")
        log_file = os.path.join(path, c_time + ".log")
        f = open(log_file, "wb")
        for timel in time_list:
            cash_log = shell_cmd(timel)
            f.write(cash_log)
        f.close()
Exemplo n.º 24
0
 def adb_video(path, times=20):
     """
     android6 可以
     """
     PATH = lambda p: os.path.abspath(p)
     sdk = int(Actions.get_sdk_version())
     if sdk >= 19:
         shell_cmd(
             "screenrecord --time-limit --size 640 * 360 %d --verbose /data/local/tmp/screenrecord.mp4"
             % times)
         log.info(">>>Get Video file...")
         time.sleep(1.5)
         path = PATH(path)
         if not os.path.isdir(path):
             os.makedirs(path)
         c_time = time.strftime("%Y_%m_%d_%H-%M-%S")
         adb_cmd("pull /data/local/tmp/screenrecord.mp4 %s" %
                 PATH("%s/%s.mp4" % (path, c_time)))
         # shell_cmd("rm /data/local/tmp/screenrecord.mp4")
     else:
         log.error("sdk version is %d, less than 19!" % sdk)
         sys.exit(0)
Exemplo n.º 25
0
    def get_battery_status():
        """
        获取电池充电状态
        BATTERY_STATUS_UNKNOWN:未知状态
        BATTERY_STATUS_CHARGING: 充电状态
        BATTERY_STATUS_DISCHARGING: 放电状态
        BATTERY_STATUS_NOT_CHARGING:未充电
        BATTERY_STATUS_FULL: 充电已满
        """
        statusDict = {
            1: "UNKNOWN",
            2: "CHARGING",
            3: "DISCHARGING",
            4: "NOT_CHARGING",
            5: "FULL"
        }
        status = shell_cmd("dumpsys battery | {0} status".format(
            Actions.find_type)).split(": ")[-1]

        return statusDict[int(status)]
Exemplo n.º 26
0
 def get_ui_dump_xml(xml_path):
     """
     获取当前Activity的控件树
     """
     PATH = lambda a: os.path.abspath(a)
     if int(Actions.get_sdk_version()) >= 19:
         shell_cmd(
             "uiautomator dump --compressed /data/local/tmp/uidump.xml")
     else:
         shell_cmd("uiautomator dump /data/local/tmp/uidump.xml")
     path = PATH(xml_path)
     if not os.path.isdir(path):
         os.makedirs(path)
     adb_cmd("pull /data/local/tmp/uidump.xml %s" % PATH(path)).wait()
     shell_cmd("rm /data/local/tmp/uidump.xml").wait()
     if os.path.exists(os.path.join(path, "uidump.xml")):
         return True
     else:
         return False
Exemplo n.º 27
0
 def device_sn(self):
     return shell_cmd("getprop ro.serialno").strip()
Exemplo n.º 28
0
 def device_name(self):
     return shell_cmd("getprop ro.product.name").strip()
Exemplo n.º 29
0
 def brand_name(self):
     return shell_cmd("getprop ro.product.brand").strip()
Exemplo n.º 30
0
 def ip_address(self):
     return shell_cmd("ifconfig |{0} Mask".format(Actions.find_type)).strip().split("  Bcast")[0].split(":")[1]