Forum Discussion

nedbacan's avatar
nedbacan
Frequent Contributor
2 years ago
Solved

How can I get the system clock time in a global variable of when button (object) is clicked?

(1) - I would like to create a test in JavaScript that will capture the system date/time (T1) of when the button was clicked and store that time in a global variable.

Next compare the captured time (T1) to a later time (T2) of another event (button clicked) to get the duration time.  The time is not from the AUT, but from MS Windows.

 

I understand I can use the aqDateTime.Compare(DateTime1, DateTime2) but I guess the question is really on getting the button click time from the system clock. 

 

Can you please provide a complete example?

 

(2) - Another question regarding time, is it possible to capture the time zone and display it in this format "2022-12-05T15:18:23+02:00"

 

The following code output this format Time: Tue Jan 24 2023 10:20:00 GMT-0500 (Eastern Standard Time) but I need it in this format "2023-24-01T15:18:23+02:00" 

 

 

 

 

function ClickButton() {
  var currentTime = new Date().toString();
  Log.Message("Time: " + currentTime);
}

 

 

 

 

 

The reason for this test, is to verify an audit report, but there can be users in different time zones. 

 

 

 

 

  • Somethig like this,

        Aliases.Fetchbtn.Click(); // Perform click here
        const t1 = new Date(Date.now());
        Log.Message("First button clicked at: " + t1.toUTCString());

8 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Somethig like this,

        Aliases.Fetchbtn.Click(); // Perform click here
        const t1 = new Date(Date.now());
        Log.Message("First button clicked at: " + t1.toUTCString());
    • nedbacan's avatar
      nedbacan
      Frequent Contributor

      Hello rraghvani , please excuse me, but I am trying to call the function callTimeVariable1() to reuse the time in another script i.e. DurationTime1()

      Can you correct my code?  There is no error and also nothing in the message log. 

       

      Note: TimeVariable1() run fine.

       

      function callTimeVariable1() {
         return new Date(Date.now());
      }
      
      
      function DurationTime1()
      
      {
          const t1 = callTimeVariable1()
          Log.Message("First button clicked at:" + t1.toUTCString());
          aqUtils.Delay(3000);
       
          const t2 = new Date(Date.now());
          Log.Message("Second button clicked at:" + t2.toUTCString());
          aqUtils.Delay(3000);
       
          var diff = (t2 - t1) / 1000;
          Log.Message("Duration Time" +diff);
      }

       

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    For example,

     

    function Split()
    {
        // Peform click here
        const t1 = aqDateTime.Time();
        Log.Message("First button clicked at: " + aqConvert.DateTimeToStr(t1));
        
        aqUtils.Delay(3000);
    
        // Peform click here    
        const t2 = aqDateTime.Time();
        Log.Message("Second button clicked at: " + aqConvert.DateTimeToStr(t2));
        
        var diff = t2 - t1;
        Log.Message("Time difference between clicks: " + diff);
    }

     

    See JavaScript - Working With Time Values

    • nedbacan's avatar
      nedbacan
      Frequent Contributor

      rraghvani, thank you for your help.

       

      Sorry, but I am trying to understand your approach, how do I get the object that is being clicked in your code, for instance 

      1) My first button to click is Fetchbtn.Click() 

      2) My second button to click is Savebtn.Click() 

       

      I ran your example .....

      aqConvert.DateTimeToStr(t1))

      It gave me 1899 instead of my system clock 2023.  Also, any way to show time in seconds instead of ms. 

       

       

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Oops, I pasted the wrong code. It should be this.

    To convert milliseconds to seconds, you divide by 1000.

  • nedbacan's avatar
    nedbacan
    Frequent Contributor

    Awesome !!  I am sorry if I am not JavaScript savvy, but how do I add the object click() to the code?

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    There's nothing wrong with your code. If you run function DurationTime1(), then it will call function callTimeVariable1() and return the Date object. The information is then shown.

     

    If you just run function callTimeVariable1(), the Date object will be returned. But nothing will be shown.