def unlock_android_app(serial=""):
    """ Attempt to enter password for locked app.  It is up to the user to put proper logic to figure out if the app is password protected.  
    We will assume user have already checked that"""

    sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
    try:
        # Get password
        if (sr.Test_Shared_Variables("device_password") == False
            ):  # Make sure user stored password in shared variables
            CommonUtil.ExecLog(sModuleInfo,
                               "Can't unlock phone - no password specified.",
                               3)
            return "zeuz_failed"
        password = sr.Get_Shared_Variables(
            "device_password")  # Read device password from shared variables
        if serial != "":
            serial = "-s %s" % serial  # Prepare serial number with command line switch
        # Unlock app
        subprocess.check_output("adb %s shell input keyevent 82" % (serial),
                                shell=True,
                                encoding="utf-8")  # Wakeup device
        time.sleep(1)
        CommonUtil.ExecLog(sModuleInfo,
                           "Serial number of the device used - %s" % serial, 1)
        subprocess.check_output(
            "adb %s shell input text %s" % (serial, password),
            shell=True,
            encoding="utf-8",
        )  # Enter password
        time.sleep(0.5)
        subprocess.check_output(
            "adb %s shell input keyevent KEYCODE_ENTER" % serial,
            shell=True,
            encoding="utf-8",
        )  # Press ENTER key
        time.sleep(
            2)  # Give time for foreground to switch and unlock to complete
    except Exception:
        errMsg = "Unable to unlock app"
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)
def unlock_android(serial=""):
    """ Attempt to enter password for locked phone """
    # Caveat 1: Only works if device has PIN or PASSWORD, not if they use a pattern, or pattern as a fingerprint backup
    # Caveat 2: Only works if the user connects USB and unlocks the phone. Then, if the phone is locked, we still have an ADB connection, and can work with it.

    sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
    try:
        lock_status = ""
        if serial != "":
            serial_with_id = "-s %s" % serial
        subprocess.check_output(
            "adb %s shell svc power stayon usb" % (serial_with_id),
            shell=True,
            encoding="utf-8",
        )  # Wakeup device
        lock_status = check_if_device_is_unlocked(serial="")
        if lock_status == True:
            CommonUtil.ExecLog(
                sModuleInfo, "Device is already unlocked. No action is needed",
                1)
            return "passed"

        # Get password
        if (sr.Test_Shared_Variables("device_password") == False
            ):  # Make sure user stored password in shared variables
            CommonUtil.ExecLog(sModuleInfo,
                               "Can't unlock phone - no password specified.",
                               3)
            return "zeuz_failed"
        password = sr.Get_Shared_Variables(
            "device_password")  # Read device password from shared variables
        # Unlock phone

        CommonUtil.ExecLog(sModuleInfo, "Attempting to unlock using adb", 1)
        subprocess.check_output(
            "adb %s shell svc power stayon usb" % (serial_with_id),
            shell=True,
            encoding="utf-8",
        )  # Wakeup device
        time.sleep(0.5)
        subprocess.check_output(
            "adb %s shell input keyevent 82" % (serial_with_id),
            shell=True,
            encoding="utf-8",
        )  # Wakeup device
        time.sleep(0.5)
        subprocess.check_output(
            "adb %s shell input keyevent 82" % (serial_with_id),
            shell=True,
            encoding="utf-8",
        )  # Wakeup device
        time.sleep(0.5)
        CommonUtil.ExecLog(
            sModuleInfo,
            "Serial number of the device used - %s" % serial_with_id, 1)
        subprocess.check_output(
            "adb %s shell input text %s" % (serial_with_id, password),
            shell=True,
            encoding="utf-8",
        )  # Enter password
        time.sleep(0.5)
        subprocess.check_output(
            "adb %s shell input keyevent KEYCODE_ENTER" % serial_with_id,
            shell=True,
            encoding="utf-8",
        )  # Press ENTER key
        time.sleep(
            2)  # Give time for foreground to switch and unlock to complete

        # Unlock phone

        lock_status = check_if_device_is_unlocked(serial)
        if lock_status == True:
            CommonUtil.ExecLog(sModuleInfo,
                               "Successfully unlocked your device", 1)
            return "passed"
        else:
            CommonUtil.ExecLog(
                sModuleInfo,
                "We could not unlock using adb, we will try with uiautomator",
                1,
            )
            Enter_Password_UIAutomator(password, serial)

            lock_status = check_if_device_is_unlocked(serial)
            if lock_status == True:
                CommonUtil.ExecLog(sModuleInfo,
                                   "Successfully unlocked your device", 1)
                return "passed"

            CommonUtil.ExecLog(sModuleInfo, "We could not unlock your device",
                               3)
            return "zeuz_failed"

    except Exception:
        errMsg = "Unable to unlock device"
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)
Exemplo n.º 3
0
gui.FAILSAFE = False

#########################
#                       #
#    Global Variables   #
#                       #
#########################

MODULE_NAME = inspect.getmodulename(__file__)

# Valid image positions
positions = ("left", "right", "centre", "center")

# Recall dependency, if not already set
dependency = None
if Shared_Resources.Test_Shared_Variables(
        "dependency"):  # Check if driver is already set in shared variables
    dependency = Shared_Resources.Get_Shared_Variables(
        "dependency")  # Retreive appium driver
else:
    raise ValueError("No dependency set - Cannot run")

# Recall file attachment, if not already set
file_attachment = []
if Shared_Resources.Test_Shared_Variables("file_attachment"):
    file_attachment = Shared_Resources.Get_Shared_Variables("file_attachment")

#########################
#                       #
#   Helper Functions    #
#                       #
#########################