def get_target_element(file_path, target_parameter, target_value, action_name,
                       action_value, step_data):
    """
    Function to get the target element(s) as per 'action'
    """
    sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
    try:
        file_tree = []
        # Function to get the XML file tree
        file_tree = get_file_tree(file_path)

        driver = None
        driver = file_tree[0]
        # Function to get the elements from the XML file
        matching_elements = LE.Get_Element(step_data, driver)
        CommonUtil.ExecLog(
            sModuleInfo,
            ">>> The expected attribute value is: '%s'" % action_value, 1)

        # Function to update the target element
        returned_target_element = update_target_element(
            file_path,
            file_tree[1],
            matching_elements,
            target_parameter,
            target_value,
            action_name,
            action_value,
        )

        return returned_target_element

    except Exception:
        return CommonUtil.Exception_Handler(sys.exc_info())
Example #2
0
def check_for_element(data_set):
    """ Tests whether or not an element is visible on screen """

    sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

    # Parse data set
    try:
        file_name = ""
        for row in data_set:
            if row[1] == "element parameter":
                file_name = row[2]

        if file_name == "":
            CommonUtil.ExecLog(
                sModuleInfo,
                "Valid element not found. Expected Sub-Field to be 'element parameter', and Value to be a filename",
                3,
            )
            return "zeuz_failed"

    except Exception:
        errMsg = "Error parsing data set"
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)

    # Perform action
    try:
        CommonUtil.ExecLog(sModuleInfo,
                           "Performing check action on file %s" % (file_name),
                           0)
        element = LocateElement.Get_Element(data_set, gui)  # (x, y, w, h)

        # CommonUtil.TakeScreenShot(
        #     sModuleInfo
        # )  # Capture screenshot, if settings allow for it

        if element in failed_tag_list:
            CommonUtil.ExecLog(sModuleInfo, "Element not found", 3)
            return "zeuz_failed"
        else:
            CommonUtil.ExecLog(sModuleInfo, "Found element", 1)
            return "passed"

    except Exception:
        errMsg = "Error parsing data set"
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)
Example #3
0
def navigate_listbox(data_set):
    """ Scroll listbox until image element is found or timeout is hit

    Action: Finding element from dropdown list
    Field	        Sub Field	        Value
    image           element parameter	file_name.png
    listbox         desktop action      5
    """
    # Continually presses page down and checks for the image
    # Assumptions: Listbox already has focus - user ought to use click action to click on the listbox, or the drop down menu's arrow
    # Assumptions: User has image of the list item they want to find
    # Produces: Pass/Fail - User is responsible for performing the action they desire now that the listbox is where their element is visible

    sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

    # Maximum number of tries to find the element. Have no way of knowing when we hit the last list item, so we have to hard code a value
    max_tries = 10

    # Delay before checking for image element
    delay = 1

    # Parse data set
    try:
        file_name = ""
        for row in data_set:
            if row[1] == "element parameter":
                file_name = row[2]
            elif row[1] == "action":
                try:
                    max_tries = int(row[2].strip(
                    ))  # Test if user specified a max_tries on the action line
                except:
                    max_tries = 10  # Default max_tries - user did not specify

        if file_name == "":
            CommonUtil.ExecLog(
                sModuleInfo,
                "Valid element not found. Expected Sub-Field to be 'element parameter', and Value to be a filename",
                3,
            )
            return "zeuz_failed"
    except Exception:
        errMsg = "Error parsing data set"
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)

    # Perform action
    try:
        # Get coordinates for source and destiniation
        for i in range(max_tries):
            CommonUtil.ExecLog(sModuleInfo, "Checking listbox for element", 0)
            element = LocateElement.Get_Element(data_set, gui)  # (x, y, w, h)
            if element in failed_tag_list:  # Error reason logged by Get_Element
                CommonUtil.ExecLog(
                    sModuleInfo,
                    "Could not locate element - trying a new position. Attempt #%d"
                    % i,
                    0,
                )
                gui.hotkey("pgdn")
                time.sleep(delay)  # Wait for listbox to update
            else:
                CommonUtil.ExecLog(sModuleInfo,
                                   "Found element after %d tries" % (i + 1), 1)
                return "passed"

        # CommonUtil.TakeScreenShot(
        #     sModuleInfo
        # )  # Capture screenshot, if settings allow for it

        CommonUtil.ExecLog(
            sModuleInfo,
            "Could not locate element after %d attempts" % max_tries, 3)
        return "zeuz_failed"

    except Exception:
        errMsg = "Error while trying to perform drag action"
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)
Example #4
0
def Drag_Element(data_set):
    """ Drag element from source to destination

    Action: Drag an element to a specific coordinates
    Field	        Sub Field	        Value
    coordinates     element parameter	100,250
    image           source parameter    source_image.png
    drag            desktop action      left

    Action: Drag element by images
    Field	        Sub Field	        Value
    image           element parameter	destination_image.png
    image           source parameter    source_image.png
    drag            desktop action      left
    """

    sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

    # Parse data set
    try:
        cmd = ""
        file_name = ""
        src_file_name = ""
        position = "centre"
        Drag_Element_to_a_coordinate = False

        for row in data_set:
            if row[1] == "action":
                if row[0] == "drag":
                    cmd = "drag"
                    position = row[2]
            elif row[1] == "element parameter":
                if "coordinates" in row[0].lower():
                    dst_x, dst_y = row[2].replace(" ", "").split(",")
                    dst_x, dst_y = int(dst_x), int(dst_y)
                    Drag_Element_to_a_coordinate = True
                else:
                    file_name = row[2]
            elif row[1] == "source parameter":
                src_file_name = row[2]

        if cmd == "":
            CommonUtil.ExecLog(
                sModuleInfo,
                "Valid action not found. Expected Field set to 'drag' and the Value one of: %s"
                % str(positions),
                3,
            )
            return "zeuz_failed"
        if position not in positions:
            CommonUtil.ExecLog(
                sModuleInfo,
                "Will click on centre of element. Expected Value to be one of: %s"
                % str(positions),
                2,
            )
            position = "centre"

        if src_file_name == "":
            CommonUtil.ExecLog(
                sModuleInfo,
                "Valid element not found. Expected Sub-Field to be 'source parameter', and Value to be a filename",
                3,
            )
            return "zeuz_failed"

    except Exception:
        errMsg = "Error parsing data set"
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)

    # Perform action
    try:
        # Get coordinates for source and destiniation
        for filename in (file_name, src_file_name):
            if Drag_Element_to_a_coordinate and filename == file_name:
                continue
            tmp_data_set = [("image", "element parameter", filename)]
            # Find image coordinates for destination element
            CommonUtil.ExecLog(
                sModuleInfo,
                "Performing %s action on file %s" % (cmd, filename), 0)
            element = LocateElement.Get_Element(tmp_data_set,
                                                gui)  # (x, y, w, h)
            if element in failed_tag_list:  # Error reason logged by Get_Element
                CommonUtil.ExecLog(sModuleInfo,
                                   "Could not locate element: %s" % filename,
                                   3)
                return "zeuz_failed"

            # DESTINATION  Get coordinates for position user specified
            x, y = getCoordinates(element, position)  # Find coordinates (x,y)
            if x in failed_tag_list:  # Error reason logged by Get_Element
                CommonUtil.ExecLog(sModuleInfo,
                                   "Error calculating coordinates", 3)
                return "zeuz_failed"
            CommonUtil.ExecLog(sModuleInfo,
                               "Image coordinates on screen %d x %d" % (x, y),
                               0)

            # Put the x,y in usable variables
            if filename == file_name:
                dst_x, dst_y = x, y
            else:
                src_x, src_y = x, y

        # Drag source to destination
        gui.moveTo(src_x, src_y)  # Move to source
        result = gui.dragTo(
            dst_x, dst_y, 2, button="left"
        )  # Click and drag to destination, taking two seconds, then release - the 2 seconds is important for some drags because without the time, it happens too fast and the drag command is missed by the window manager

        # CommonUtil.TakeScreenShot(
        #     sModuleInfo
        # )  # Capture screenshot, if settings allow for it

        # Check result and return
        if result in failed_tag_list:
            CommonUtil.ExecLog(sModuleInfo,
                               "Couldn't dragged element with given images", 3)
            return "zeuz_failed"
        elif Drag_Element_to_a_coordinate:

            max_x, max_y = gui.size()
            dst_x = max_x if dst_x > max_x else dst_x
            dst_x = 0 if dst_x < 0 else dst_x
            dst_y = max_y if dst_y > max_y else dst_y
            dst_y = 0 if dst_y < 0 else dst_y

            CommonUtil.ExecLog(
                sModuleInfo,
                "Successfully dragged element to the %d, %d coordinates" %
                (dst_x, dst_y),
                1,
            )
            return "passed"
        else:
            CommonUtil.ExecLog(
                sModuleInfo, "Successfully dragged element with given images",
                1)
            return "passed"

    except Exception:
        errMsg = "Error while trying to perform drag action"
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)
Example #5
0
def Click_Element(data_set):
    """ Single or double mouse click on element """

    sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

    # Parse data set
    try:
        cmd = ""
        file_name = ""
        position = "centre"
        for row in data_set:
            if row[1] == "action":
                if row[0] == "click":
                    cmd = "click"
                    position = row[2]
                elif row[0] in ("doubleclick", "double click"):
                    cmd = "doubleclick"
                    position = row[2]
            elif row[1] == "element parameter":
                file_name = row[2]

        if cmd == "":
            CommonUtil.ExecLog(
                sModuleInfo,
                "Valid action not found. Expected Field set to 'click' or 'doubleclick', and the Value one of: %s"
                % str(positions),
                3,
            )
            return "zeuz_failed"
        if position not in positions:
            CommonUtil.ExecLog(
                sModuleInfo,
                "Will click on centre of element. Expected Value to be one of: %s"
                % str(positions),
                2,
            )
            position = "centre"

        if file_name == "":
            CommonUtil.ExecLog(
                sModuleInfo,
                "Valid element not found. Expected Sub-Field to be 'element parameter', and Value to be a filename",
                3,
            )
            return "zeuz_failed"

    except Exception:
        errMsg = "Error parsing data set"
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)

    # Perform action
    try:
        # Find image coordinates
        CommonUtil.ExecLog(
            sModuleInfo, "Performing %s action on file %s" % (cmd, file_name),
            0)
        element = LocateElement.Get_Element(data_set, gui)  # (x, y, w, h)
        if element in failed_tag_list:  # Error reason logged by Get_Element
            CommonUtil.ExecLog(sModuleInfo, "Could not locate element", 3)
            return "zeuz_failed"

        # Get coordinates for position user specified
        x, y = getCoordinates(element, position)  # Find coordinates (x,y)
        if x in failed_tag_list:  # Error reason logged by Get_Element
            CommonUtil.ExecLog(sModuleInfo, "Error calculating coordinates", 3)
            return "zeuz_failed"
        CommonUtil.ExecLog(sModuleInfo,
                           "Image coordinates on screen %d x %d" % (x, y), 0)

        # Click on image
        if cmd == "click":
            result = gui.click(x, y)  # Single click
        elif cmd == "doubleclick":
            result = gui.doubleClick(x, y)  # Double click

        # CommonUtil.TakeScreenShot(
        #     sModuleInfo
        # )  # Capture screenshot, if settings allow for it

        # Check result and return
        if result in failed_tag_list:
            CommonUtil.ExecLog(sModuleInfo,
                               "Couldn't click on element with given images",
                               3)
            return "zeuz_failed"
        else:
            CommonUtil.ExecLog(
                sModuleInfo,
                "Successfully clicked on element with given images", 1)
            return "passed"

    except Exception:
        errMsg = "Error while trying to perform click action"
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)
Example #6
0
def Wait_For_Element_Pyautogui(data_set):
    """
    You need to add an image in the attachment section and mention the filename as below. This action will then search
    for the image attached in the test session and wait for the image to appear or disappear. You can set the time to
    wait in second. By default it is 10 seconds.
    Example:

    Action: Wait for an item to appear
    Field	    Sub Field	        Value
    image       element parameter	attachment.png
    wait gui    desktop action      10

    Action: Wait for an item to disappear
    Field	            Sub Field	        Value
    image               element parameter	attachment.png
    wait disable gui    desktop action      10

    """
    sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

    try:
        wait_for_element_to_disappear = False

        # Find the wait time from the data set
        for left, _, right in data_set:
            if "wait disable gui" in left.lower():
                wait_for_element_to_disappear = True
                timeout_duration = int(right.strip())
            elif "wait gui" in left.lower():
                wait_for_element_to_disappear = False
                timeout_duration = int(right.strip())

        # Check for element every second
        end_time = (time.time() + timeout_duration
                    )  # Time at which we should stop looking
        for i in range(
                timeout_duration
        ):  # Keep testing element until this is reached (likely never hit due to timeout below)
            # Wait and then test if we are over our alloted time limit
            if (
                    time.time() >= end_time
            ):  # Keep testing element until this is reached (ensures we wait exactly the specified amount of time)
                break
            time.sleep(1)

            # Test if element exists or not
            Element = LocateElement.Get_Element(data_set, gui)

            # Check if element exists or not, depending on the type of wait the user wanted
            if wait_for_element_to_disappear == False:  # Wait for it to appear
                if Element not in failed_tag_list:  # Element has appeared !
                    CommonUtil.ExecLog(sModuleInfo, "Found element", 1)
                    return "passed"
                else:  # Element not found, keep waiting
                    CommonUtil.ExecLog(
                        sModuleInfo,
                        "Element does not exist. Sleep and try again - %d" % i,
                        0,
                    )
            else:  # Wait for it to be removed/hidden/disabled
                if Element in failed_tag_list:  # Element has disappeared !
                    CommonUtil.ExecLog(sModuleInfo, "Element disappeared", 1)
                    return "passed"
                else:  # Element found, keep waiting
                    CommonUtil.ExecLog(
                        sModuleInfo,
                        "Element still exists. Sleep and try again - %d" % i,
                        0,
                    )

        # Element status not changed after time elapsed, to exit with failure
        CommonUtil.ExecLog(sModuleInfo, "Wait for element failed", 3)
        return "zeuz_failed"

    except Exception:
        return CommonUtil.Exception_Handler(sys.exc_info())
Example #7
0
def Enter_Text(data_set):
    """ Insert text """

    sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME

    # Parse data set
    try:
        element_parameter = False
        text_value = ""
        for row in data_set:
            if "action" in row[1]:
                text_value = row[2]
            if row[1] == "element parameter":  # Indicates we should find the element instead of assuming we have keyboard focus
                element_parameter = True

        if text_value == "":
            CommonUtil.ExecLog(sModuleInfo,
                               "Could not find value for this action", 3)
            return "zeuz_failed"
    except:
        return CommonUtil.Exception_Handler(sys.exc_info(), None,
                                            "Error parsing data set")

    # Perform action
    try:
        # Find image coordinates
        if element_parameter:
            CommonUtil.ExecLog(sModuleInfo, "Trying to locate element", 0)

            # (x, y, w, h)

            # Get element object
            # try for 10 seconds with 2 seconds delay
            max_try = 5
            sleep_in_sec = 2
            i = 0
            while i != max_try:
                try:
                    Element = LocateElement.Get_Element(data_set, gui)
                except:
                    True
                if Element is None:
                    CommonUtil.ExecLog(
                        sModuleInfo,
                        "Could not find element.  Waiting and Trying again .... ",
                        2)
                else:
                    break
                time.sleep(sleep_in_sec)
                i = i + 1
            if Element in failed_tag_list:
                CommonUtil.ExecLog(sModuleInfo, "Could not find element", 3)
                return "zeuz_failed"

            # Get coordinates for position user specified
            x, y = getCoordinates(Element, "centre")  # Find coordinates (x,y)
            if x in failed_tag_list:  # Error reason logged by Get_Element
                CommonUtil.ExecLog(sModuleInfo,
                                   "Error calculating coordinates", 3)
                return "zeuz_failed"
            CommonUtil.ExecLog(sModuleInfo,
                               "Image coordinates on screen %d x %d" % (x, y),
                               0)
            gui.click(x, y)  # Single click
        else:
            CommonUtil.ExecLog(
                sModuleInfo,
                "No element provided. Assuming textbox has keyboard focus",
                0,
            )

        # Enter text
        gui.typewrite(text_value)
        CommonUtil.ExecLog(
            sModuleInfo,
            "Successfully set the value of to text to: %s" % text_value, 1)
        return "passed"

    except Exception:
        errMsg = "Could not select/click your element."
        return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg)