Example #1
0
    def save_screenshot(self, browser_instance=None, filename=None,
                        directory=None):
        """
            Save screenshot of the specified/current browser
        """
        status = True
        if browser_instance is None:
            browser_instance = self.current_browser

        if filename is None:
            current_datetime = str(get_current_timestamp())
            current_datetime = current_datetime.replace(" ", "_")
            current_datetime = current_datetime.replace(":", "-")
            filename = "screenshot_" + current_datetime + ".png"
        else:
            filename += ".png"

        print_info("Screenshot will be saved by the name {0} in directory {1}".format(filename, directory))

        directory = os.path.join(directory, filename)
        try:
            browser_instance.save_screenshot(directory)
            sleep(10)
        except Exception as e:
            print_error("Could not save screenshot {0}". format(e))
            status = False

        return status
    def generate_timestamp_delta(self, stored_delta_key, timestamp_key,
                                 desired_status):
        """
            test keyword created for runmode_timer
            Generate a delta from comparing current time with store timestamp
            save the delta and current timestamp in repo for keyword verify_delta
        :Argument:
            stored_delta_key = key name to store the list of delta
            timestamp_key = key name to store the timestamp
            desired_status = user desired status
                input pass->true, fail->false and everything else ->exception
        """
        cur_ts = datetime_utils.get_current_timestamp()
        result_dict = {timestamp_key: cur_ts}
        status = self.local_data_test(desired_status)

        previous_time = data_Utils.get_object_from_datarepository(
            timestamp_key)
        stored_delta = data_Utils.get_object_from_datarepository(
            stored_delta_key)
        if previous_time:
            delta = datetime_utils.get_time_delta(previous_time, cur_ts)
            if stored_delta:
                stored_delta.append(delta)
                result_dict.update({stored_delta_key: stored_delta})
            else:
                result_dict.update({stored_delta_key: [delta]})
        return status, result_dict
Example #3
0
    def get_current_timestamp(self, current_time="current_time"):
        """Returns system current timestamp.
           :Arguments:
                  1. current_time (string) : name of the key to store in data repository

           :Returns:
                1. status(boolean)
                2. current_time (dict element) : name = current_time given in the argument,
                    value = Current System Time in the  object format of Year, Month, Date,
                    Time(without microseconds)
                     Ex :datetime.datetime(2018, 10, 22, 5, 51, 21)

        """
        wdesc = "To get the current timestamp in the format of yyyy-mm-dd hh:mm:ss"
        Utils.testcase_Utils.pNote(wdesc)
        currentdate = datetime_utils.get_current_timestamp()
        print_info("current timestamp : {0}".format(currentdate))
        output_dict = {current_time: currentdate}
        status = True
        return status, output_dict
Example #4
0
def send_command(session_object,
                 start_prompt,
                 end_prompt,
                 command,
                 timeout=60):
    """
    Send an command to pexpect session object and resturns the status of the command sent
    - Checks for the availability of the start_prompt.
    - if start prompt was available sends the command
    - if failure response is not None and failure response fond in
    response then returns False.
    - else if failure repsonse was not found
    and end prompt also not found returns False.
    - else if failure response was not found and end prompt found,
    then returns true.
    """
    print_warning("This method is obsolete and will be deprecated soon. Please"
                  " use 'send_command' method of 'PexpectConnect' class "
                  "in 'warrior/Framework/ClassUtils/warrior_connect_class.py'")

    tmout = {None: 60, "": 60, "none": 60}.get(timeout, str(timeout).lower())
    session_object.timeout = int(tmout)
    pNote("Command timeout: {0}".format(session_object.timeout))
    response = ""
    msg = ""
    end_time = False
    status = False
    cmd_timedout = False
    #time_format = "%Y-%b-%d %H:%M:%S"
    try:
        boolprompt = session_object.expect(start_prompt)
    except Exception as exception:
        pNote(
            "Could not find the start_prompt '{0}'!! exiting!!".format(
                str(start_prompt)), "error")
        boolprompt = -1
    if boolprompt == 0:
        start_time = datetime_utils.get_current_timestamp()
        pNote("[{0}] Sending Command: {1}".format(start_time, command))
        _send_cmd_by_type(session_object, command)
        try:
            while True:
                result = session_object.expect([end_prompt, pexpect.EOF, pexpect.TIMEOUT]) if end_prompt\
                else -1
                end_time = datetime_utils.get_current_timestamp()
                if result == 0:
                    curr_time = datetime_utils.get_current_timestamp()
                    msg1 = "[{0}] Command completed successfully".format(
                        end_time)
                    msg2 = "[{0}] Found end prompt '{1}' after command had timed out".format(
                        curr_time, end_prompt)
                    status = {True: "ERROR", False: True}.get(cmd_timedout)
                    msg = {True: msg2, False: msg1}.get(cmd_timedout)
                    break
                elif result == -1:
                    pNote("[{0}] end prompt not provided".format(end_time),
                          "error")
                    status = "ERROR"
                    break
                elif result == 1:
                    msg = "[{0}] EOF encountered".format(end_time)
                    status = "ERROR"
                    break
                elif result == 2:
                    tmsg1 = "[{0}] Command timed out, command will be marked as error".format(
                        end_time)
                    tmsg2 = "Will wait 60 more seconds to get end prompt '{0}'".format(
                        end_prompt)
                    tmsg3 = "Irrespective of whether end prompt is received or not command will "\
                           "be marked as error because command had timed out once."
                    if not cmd_timedout:
                        session_object.timeout = 1
                        pNote(tmsg1, "debug")
                        pNote(tmsg2, "debug")
                        pNote(tmsg3, "debug")
                        tstamp = datetime_utils.get_current_timestamp()
                    cmd_timedout = True
                    status = "ERROR"
                    tdelta = datetime_utils.get_time_delta(tstamp)
                    if int(tdelta) >= 60:
                        msg = "[{0}] Did not find end prompt '{1}' even after 60 seconds post"\
                              "command time out".format(datetime_utils.get_current_timestamp(),
                                                        end_prompt)
                        break
                    else:
                        continue
        except Exception as exception:
            print_exception(exception)
        else:
            response = session_object.before
            response = str(response) + str(session_object.after)
            if session_object.env is not None and 'TERM' in session_object.env and session_object.env[
                    'TERM'] == 'dumb':
                escape_seq = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]')
                response = escape_seq.sub('', response)
            pNote("Response:\n{0}\n".format(response))
            pNote(msg, "debug")
            if status is True:
                duration = datetime_utils.get_time_delta(start_time, end_time)
                pNote("Command Duration: {0} sec".format(duration))
    return status, response