Timers for the programming-impaired

Ranorex Studio, Spy, Recorder, and Driver.
Posts: 69
Joined: Wed Nov 22, 2017 10:08 am

Timers for the programming-impaired

Post by loonquawl » Thu Dec 21, 2017 9:10 am

The old thread https://www.ranorex.com/forum/calculate ... 73-15.html seems to be about what i want, but i can't get it to work, as i am sadly not versed in c#.
What i want: A Functionality where i can position a Recording(calling code)or a CodeModule "Start Timer 1 " somewhere between my other recordings, and another called "Stop Timer 1" somewhere else in the same TestSuite, but not necessarily in the same TestCase. The "Stop Timer X" should compute the difference between the StartTimeX and the StopTimeX, and publish it into the report.
The following was posted by Swisside in the other thread:

Code: Select all

public void getStartTime()  
            System.DateTime start = System.DateTime.Now;                
            starttime = start.ToString();  
        public void getEndTime()  
            System.DateTime end = System.DateTime.Now;    
            System.DateTime start = Convert.ToDateTime(starttime);  
            TimeSpan duration = end - start;  
            string s = duration.Minutes.ToString()+":"+duration.Seconds.ToString()+"."+duration.Milliseconds.ToString();  
            System.IO.File.AppendAllText(@"C:\Users\x\Documents\test.txt","TestCase:"+TestCase.Current.Name+" | ModuleName: "+ TestReport.CurrentTestModuleActivity.TestModuleName+": Duration: "+s+ Environment.NewLine);  
My UserCodeModule looks like this, so i am not sure where to paste the Code from Swisside, because that code has a "public void" while the UserCodeModule has a "public class", a "public" and a "void"

Code: Select all

 * Created by Ranorex
 * User: loonquawl
 * Date: 21.12.2017
 * Time: 08:59
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

namespace Bausteine_Win_VF_HMI_etal.RanorexTools
    /// <summary>
    /// Description of Start_Timer.
    /// </summary>
    [TestModule("6B8E2F81-2C3E-46BD-89A2-32F5FDC1AF8A", ModuleType.UserCode, 1)]
    public class Start_Timer : ITestModule
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public Start_Timer()
            // Do not delete - a parameterless constructor is required!

        /// <summary>
        /// Performs the playback of actions in this module.
        /// </summary>
        /// <remarks>You should not call this method directly, instead pass the module
        /// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method
        /// that will in turn invoke this method.</remarks>
        void ITestModule.Run()
            Mouse.DefaultMoveTime = 300;
            Keyboard.DefaultKeyPressTime = 100;
            Delay.SpeedFactor = 1.0;
It would be great if somebody would be willing to expand on the explanation of Swisside...

Posts: 174
Joined: Mon Mar 21, 2016 3:16 pm

Re: Timers for the programming-impaired

Post by asdf » Tue Jan 02, 2018 4:34 pm

Have you already tried to use the Stopwatch class to achieve your goal?
You can use the following code to count the milliseconds your code needs to execute.
var watch = System.Diagnostics.Stopwatch.StartNew();
//do your stuff
var elapsedMs = watch.ElapsedMilliseconds;
I would also recommend to not use "System.DateTime" for time measuring.

Hope that helps.

Posts: 254
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Timers for the programming-impaired

Post by Vaughan.Douglas » Wed Jan 03, 2018 1:34 pm

Have you added the Ranorex Automation Helpers to your solution?

There is a "StartTimer" and a "StopTimer" in the SystemLibrary library that stores your timer names in a static dictionary so they're accessible from module to module.
Doug Vaughan