Exemplo n.º 1
0
def is_media(path, grep_for, serial = None):
    if serial:
        adb_connection = connection_adb(serial)
    else:
        adb_connection = connection_adb()
    a = adb_connection.parse_cmd_output(cmd = 'ls ' + path, grep_for = grep_for)
    return a != ''
def is_verified_boot_state(serial=None, state=None):
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    cmd = "getprop ro.boot.verifiedbootstate"
    return state in adb_connection.parse_cmd_output(cmd=cmd, grep_for=state)
Exemplo n.º 3
0
def get_dut_ipv6_address(serial=None, static_ip=False):
    """
    Returns device ipv6 address from the ifconfig command output
    :param serial: the DUT serial
    :param static_ip: boolean, true if ip to get is static, false if it is dynamic
    :return: the ipv6 address from the output
    """
    if static_ip:
        grep_for_text = "global mngtmpaddr"
    else:
        grep_for_text = "global temporary"

    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    for i in range(5):
        ipv6_address_line = adb_connection.parse_cmd_output(
            cmd="ip -6 addr show wlan0", grep_for=grep_for_text).split()

        if not ipv6_address_line:
            time.sleep(2)
        else:
            break
    ipv6_address = ipv6_address_line[1]
    if "/" in ipv6_address:
        ipv6_address = ipv6_address.split("/")[0]
    return ipv6_address
Exemplo n.º 4
0
def get_triggered_watchers(serial=None):
    """ description:
            returns all register watchers that were triggered

        usage:
            ui_utils.get_triggered_watchers()

        tags:
            ui, android, watcher, get, triggered
    """
    if serial:
        uidevice = ui_device(serial=serial)
        adb_connection = connection_adb(serial=serial)
    else:
        uidevice = ui_device()
        adb_connection = connection_adb()

    triggered_watchers = []
    if adb_connection.check_connected():
        try:
            for watcher in uidevice.watchers:
                if uidevice.watcher(watcher).triggered:
                    triggered_watchers.append(watcher)
        except Exception as e:
            if "RPC" not in str(e.message) and "not connected" not in \
                    str(e.message) and "not attached" not in str(e.message):
                print strftime("%d/%m %X.%S0 ", time.localtime()) + \
                    "Registered watchers query exception due to lack of adb connectivity: {0}".format(e.message)
                raise
            else:
                print strftime("%d/%m %X.%S0 ", time.localtime()) + \
                    "Watchers could not be queried due to exception: {}".format(e.message)

    return triggered_watchers
Exemplo n.º 5
0
def check_download_progess(value,
                           name,
                           download_path="/storage/sdcard0/Download/",
                           polling_time=10,
                           serial=None):
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    value_units = float(value)
    # the file needs to be in the (1 - 999) megabytes interval for this to correctly work
    # also, it assumes that at least the 1st MB of the file is successfully downloaded
    # size needs to be provided in kilobytes
    if not folder_exists(download_path + name, serial=serial):
        return (False,
                "Download not started. Check server connectivity! Exiting...")
    time_passed = 0
    #while time_passed <= max_time:
    size1 = float(get_file_size(name, file_path=download_path, serial=serial))
    time.sleep(polling_time)
    #time_passed += polling_time
    size2 = float(get_file_size(name, file_path=download_path, serial=serial))
    if size2 > size1:
        return (True, "Download is progressing")
    elif size1 == size2 and size2 < value_units:
        # raise error if the download is no longer progressing
        return (False, "Download Interrupted! Exiting...")
    elif size1 == size2 >= value_units:
        # change size1 == size2 == value_units to size1 == size2 >= value_units, since at simetimes the result
        # got from target may be > the result got from the host, it depends on the du app
        # all good if the final downloaded size equals the expected size
        return (True, "Download completed!")
Exemplo n.º 6
0
def input_keyevent(serial=None, key_number=None):
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    command = "input keyevent " + str(key_number)
    return adb_connection.run_cmd(command=command)
Exemplo n.º 7
0
def get_device_orientation_type(serial=None):
    """ description:
            gets the device orientation type

        tags:
            utils, adb, android, orientation
    """

    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()

    device_type = "phone"
    height = 0
    width = 0
    cmd = "dumpsys display | grep mDefaultViewport=DisplayViewport"
    output_string = adb_connection.parse_cmd_output(cmd,
                                                    dont_split=True).strip()

    m = re.search(r"deviceHeight=(\d+)", output_string)
    if m:
        height = int(m.group(1))

    m = re.search(r"deviceWidth=(\d+)", output_string)
    if m:
        width = int(m.group(1))

    if width > height:
        device_type = "tablet"
    # else remains set to 'phone'
    # also will return 'phone' as default if no values are found in dumpsys
    return device_type
Exemplo n.º 8
0
def get_host_ip(serial=None):
    """
    Determine de IP address of the current machine that is in the same subnet with the DUT
    """
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()

    wifi_conf = get_connection_content(serial=serial)
    device_ip_mlink = get_connection_info(
        wifi_conf
    )['ip_address']  # ip address from mLink properties in dumpsys wifi output
    if device_ip_mlink:
        subnet_mask = get_connection_info(wifi_conf)['net_mask']
        host_addresses = get_ipv4_address()
        prefix = get_prefix(device_ip_mlink, subnet_mask)
        host_ip = select_ip(prefix, host_addresses)
        if host_ip != None:
            return host_ip
        else:
            print "Host IP - undetermined due to device connectivity issue: ", host_ip
            return None
    else:
        print "Host IP - undetermined due to device connectivity issue"
        net_cfg = get_netcfg_content(serial=serial)
        print "net cfg info : \n", net_cfg
        device_ip_netcfg = get_device_wlan0_ip(
            serial=serial).split('/')[0].split(':')[-1]
        print "Device ip netcfg way : ", device_ip_netcfg
        print "Device ip mlink way : ", device_ip_mlink
        return None
Exemplo n.º 9
0
def ping_ipv6(ip, trycount=2, target_percent=50, timeout=15, serial=None):
    """ Descriptions:
            Pings an ipv6 ip for a <trycount> times

        Returns True if the packet loss is less then or equal to the target_percent

        Usage:
            ping(serial = serial,
                   ip = "ipv6.google.com"
                   trycount = 30,
                   target_percent= 15)
    """
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()

    ping_command = "ping6"
    command = ping_command + " -c " + str(trycount) + " " + str(ip)

    ping_data = adb_connection.run_cmd(command=command,
                                       timeout=timeout,
                                       mode="sync")
    ping_output = ping_data.stdout.read().strip()
    matcher = re.compile(r'(\d+)% packet loss')
    match = matcher.search(ping_output)
    if not match:
        print "Ping output : ", ping_output
        return False, None, ping_output

    percent = match.groups()[0]
    verdict = float(percent) <= target_percent

    return verdict, percent, ping_output
Exemplo n.º 10
0
def stay_on(serial=None):

    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    adb_connection.parse_cmd_output(cmd="svc power stayon true")
Exemplo n.º 11
0
def get_connection_parameter(parameter, serial=None):
    """
    Returns the requested connection parameter.
    Possible parameter values:
               DHCP_server
               DNS_addresses
               Frequency
               Gateway
               Link_speed
               MAC
               SSID
               Security
               ip_address
               p2p_device_address
               state
               pairwise_cipher
               group_cipher
               net_mask
    """
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()

    wifi_conf = get_connection_content(serial=serial)
    connection_params = get_connection_info(wifi_conf)
    if parameter in connection_params.keys():
        return connection_params[parameter]
    else:
        return None
Exemplo n.º 12
0
def check_for_download_file(value_str, serial=None):
    if serial:
        adb_connection = connection_adb(serial)
    else:
        adb_connection = connection_adb()
    cmd = "ls /storage/sdcard0/Download"
    return value_str in adb_connection.parse_cmd_output(cmd, timeout=5).strip()
Exemplo n.º 13
0
def get_connection_content(serial=None, service="wifi"):
    # services supported: "wifi", "wifip2p"
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    wifi_conf = adb_connection.parse_cmd_output("dumpsys {}".format(service))
    return wifi_conf
def get_product_name(serial=None):
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    product_name = adb_connection.parse_cmd_output(cmd="cat /system/build.prop", grep_for="ro.product.name")
    if product_name:
        return product_name.split("=")[1]
    return False
Exemplo n.º 15
0
def p2p_start_ftpdd(port_number="20211",
                    root_dir="data/ftpdfiles/",
                    serial=None):
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    cmd = "/data/busybox/tcpsvd -vE 0.0.0.0" + " " + port_number + " " + "/data/busybox/ftpd" + " " + root_dir
    createDaemon()
    adb_connection.parse_cmd_output(cmd, timeout=5)
def get_UUID(block_id, serial=None):
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()

    for line in adb_connection.parse_cmd_output('blkid').split('\n'):
        if block_id in line:
            uuid = line.split(' ')[1].split('=')[1].strip('"')
            return uuid
    return "UUID Not Found"
Exemplo n.º 17
0
def is_go_device(serial=None):
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()

    if 'isGroupOwner: true' in adb_connection.parse_cmd_output(
            "dumpsys {}".format('wifip2p')):
        return True
    else:
        return False
 def dump(self, out_file=None, serial=None, compressed=False):
     if out_file:
         return_value = super(UIDevice, self).dump(filename=out_file, compressed=compressed)
     else:
         return_value = super(UIDevice, self).dump(compressed=compressed)
     if serial:
         adb_connection = connection_adb(serial=serial)
     else:
         adb_connection = connection_adb()
     adb_connection.run_cmd("rm /data/local/tmp/dump.xml", timeout=10)
     adb_connection.run_cmd("rm /data/local/tmp/local/tmp/dump.xml", timeout=10)
     return return_value
Exemplo n.º 19
0
def is_view_displayed(view_to_find, serial=None, wait_time=100, exists=True):
    """ description:
            Return True if <view_to_find> is visible on screen.

        usage:
            adb_utils.is_view_displayed(view_to_find = {"Text": "text"})

        tags:
            adb, android, view, displayed
    """

    remote_file = "/sdcard/window_dump.xml"

    if serial:
        adb_connection = connection_adb(serial=serial)
        local_file = "./window_dump_{0}.xml".format(serial)
    else:
        adb_connection = connection_adb()
        local_file = "./window_dump_{0}.xml"

    tries = 0
    pid = adb_connection.get_pid("uiautomator")
    if pid:
        while pid and tries < 5:
            p = adb_connection.adb_root()
            p = adb_connection.kill_command(pid=pid)
            time.sleep(5)
            pid = adb_connection.get_pid("uiautomator")
            tries += 1

    current_time = 0

    while current_time < wait_time:
        p = adb_connection.run_cmd(command="uiautomator dump")

        p = adb_connection.get_file(remote=remote_file, local=local_file)

        p = adb_connection.run_cmd(command="rm {0}".format(remote_file))
        view = '{0}="{1}"'.format(view_to_find.keys()[0],
                                  view_to_find[view_to_find.keys()[0]])

        with open(local_file, "r") as f:
            read_data = f.read()
            if view in read_data:
                os.remove(local_file)
                if exists:
                    return True

        os.remove(local_file)
        current_time += 1
    if exists:
        return False
    return True
def get_battery_prop(prop=None, serial=None):

    """ description:
            gets the battery properties with dumpsys
        tags:
            utils, adb, android, dumpsys, battery
    """
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    return adb_connection.parse_cmd_output(cmd="dumpsys battery", grep_for=prop, timeout=5).strip()
def is_virtual_keyboard_on(serial=None):

    """ description:
            return true if virtual keybaord is displayed

        tags:
            utils, adb, android, vrtual keyboard
    """
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    return len(adb_connection.parse_cmd_output(cmd="dumpsys input_method", grep_for="mInputShown=true")) > 0
Exemplo n.º 22
0
def is_prop_set(prop, value, serial=None):
    """
        description:
            check if the prop has the given <value>

        tags:
            utils, adb, prop, value, check
    """
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    return value == adb_connection.get_prop(prop).strip()
Exemplo n.º 23
0
def get_battery_level(serial=None, path="dollar_cove_battery"):
    """ description:
            gets the battery level with dumpsys

        tags:
            utils, adb, android, dumpsys, battery, level
    """
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    return adb_connection.parse_file(
        file_name="/sys/class/power_supply/{0}/capacity".format(path)).strip()
Exemplo n.º 24
0
def get_dut_date(serial=None, gmt_date=True):
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()

    if gmt_date:
        p = adb_connection.run_cmd(command="date -u")
    else:
        p = adb_connection.run_cmd(command="date")
    date_dut = str(p.stdout.read())
    date_dut = parse(date_dut)
    return date_dut
Exemplo n.º 25
0
def check_airplane_mode_on(serial=None):
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()

    cmd = "dumpsys wifi | grep mAirplaneModeOn"
    out = adb_connection.parse_cmd_output(cmd=cmd)
    if "true" in out.strip():
        return True
    elif "false" in out.strip():
        return False
    else:
        return None
Exemplo n.º 26
0
def get_netcfg_content(serial=None, platform=None):
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()

    if platform:
        platform = platform
    else:
        platform = statics.Device(serial=serial)

    # netcfg of ifconfig output
    net_conf = adb_connection.parse_cmd_output(platform.get_interfaces_tool)
    return net_conf
Exemplo n.º 27
0
def get_serial(serial=None):
    """ description:
            gets the serial of the device

        tags:
            utils, adb, android, serial
    """

    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    serial_no = adb_connection.get_prop("ro.serialno").strip()
    return serial_no
Exemplo n.º 28
0
def file_exists(file_path, serial=None):
    """ description:
            checks if the file is found on the device

        tags:
            utils, adb, android, file, exists
    """
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    out = adb_connection.parse_cmd_output(cmd="ls {0}".format(file_path),
                                          grep_for=file_path)
    return file_path in out and "No such file or directory" not in out
Exemplo n.º 29
0
def get_device_type(serial=None):
    """
    returns device type from getprop
    """
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()
    product_name_line = adb_connection.parse_cmd_output(
        cmd="getprop",
        grep_for="ro.product.device").split()  # split getprop line into a list
    product_name = product_name_line[
        1]  # this contains product name inside brackets, remove them below
    return product_name[1:-1]
Exemplo n.º 30
0
def is_package_installed(package_name, serial=None):
    """ description:
            checks if the package is installed on the device

        tags:
            utils, adb, android, file, exists
    """
    if serial:
        adb_connection = connection_adb(serial=serial)
    else:
        adb_connection = connection_adb()

    command = "pm list packages"
    out = adb_connection.parse_cmd_output(cmd=command, grep_for=package_name)
    return package_name in out