Page 1 of 2

Calculate time between steps in a recording

Posted: Tue Jan 28, 2014 12:43 pm
by cdmartinus
Hi all,

I am currently running some tests on a website and need to get the system to calculate the time taken between certain test steps and add them to an accumulating text file.

At the moment, I'm just waiting for the test to complete and then manually working out the differences using the report. Is there anyway I could do this using C#? I can already output data to a text file, it's just a matter of getting the figures I need first.

Thanks

Re: Calculate time between steps in a recording

Posted: Tue Jan 28, 2014 2:51 pm
by Swisside
Hello !

I suggest you have a look at the thread : http://www.ranorex.com/forum/getting-th ... t4613.html


If you want to implement this in a recording module do as follow :
1)Add two module variables let's say starttime and endtime

2) Add a UserCode module where you want to start measuring let's call the method "getStartTime"

3) Add a UserCode module where you want to end measuring and call the methode "getEndTime"

4) Then go to the usercode (Right click => ViewCode )

5) Paste the following code for each method
public void getStartTime()
        	
        {   
        	Report.Log(ReportLevel.Info,"Start");
            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();
        	Report.Log(ReportLevel.Info,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);
This code will write the testcase name, module name and the duration between the steps to a text file. You can of course adapt it to your needs.

If you need additional help do not hesitate :D

Regards

Swiss'

Re: Calculate time between steps in a recording

Posted: Wed Jan 29, 2014 11:30 am
by cdmartinus
I tried your solution and it worked perfectly. I've now got all the time taken values outputting neatly to a text file, which means a lot less work for me when I have to run it 6 times a day!

Re: Calculate time between steps in a recording

Posted: Wed Jan 29, 2014 11:34 am
by Swisside
Glad I could help you :D


Take care

Re: Calculate time between steps in a recording

Posted: Wed Oct 28, 2015 10:50 am
by ejji09
Hi Swisside

Am testing a patient registration application. where i want to measure the time taken to open the patient panel after login in into system. can you please provide the infoemation for that

My Code:

Code: Select all

            var repo = TestRIS_iRepository.Instance;
            var username = repo.LogIn.Username;
            username.Click();
            username.PressKeys("tau1");
            
            var password = repo.LogIn.Password;
            password.Click();
            password.PressKeys("tau1");
            
            var logIn = repo.LogIn.LogIn;
            logIn.Click();
//want to check the how much time taken, after clicking the login button utill the patient panel opens
            var patient = repo.CentricityRISI50.Patient;
            patient.Click();
            var registration = repo.CentricityRISI50.Registration;
            registration.Click();

Re: Calculate time between steps in a recording

Posted: Wed Oct 28, 2015 11:13 am
by odklizec
Hi,

All you need to achieve your goal seems to be nicely described in Swisside's post. You just have to combine the code from his post with your code. I would suggest to add WaitForExists method to make the code wait for the appearance of panel you want test. After this method, you should measure the end time. Here is the code snippet...

Code: Select all

            var logIn = repo.LogIn.LogIn;
            logIn.Click();
//--- added code --- start
            System.DateTime start = System.DateTime.Now; //start time               
            starttime = start.ToString(); 
            repo.CentricityRISI50.PatientInfo.WaitForExists(30000); //waits for existence of repo.CentricityRISI50.Patient
            System.DateTime end = System.DateTime.Now;  //end time   
            System.DateTime start = Convert.ToDateTime(starttime);  
            TimeSpan duration = end - start;  
            string s = duration.Minutes.ToString()+":"+duration.Seconds.ToString()+"."+duration.Milliseconds.ToString();  
            Report.Log(ReportLevel.Info,duration.Minutes.ToString()+":"+duration.Seconds.ToString()+"."+duration.Milliseconds.ToString());  
//--- added code --- end
            var patient = repo.CentricityRISI50.Patient;
            patient.Click();

Re: Calculate time between steps in a recording

Posted: Wed Oct 28, 2015 2:19 pm
by ejji09
To get the report in a log file i tried like this but its not working for me. what is wrong in my code.

My Code:

Code: Select all

System.DateTime start = System.DateTime.Now; //start time
            Host.Local.RunApplication("C:\\medora\\bin\\CentricityRISi.exe", "", "C:\\medora\\bin", false);
            var repo = timeRepository.Instance;
            var nameField = repo.LogIn.NameField;
            nameField.Click();
            System.DateTime end = System.DateTime.Now;
TimeSpan duration = end - start;
            string s = duration.Minutes.ToString()+":"+duration.Seconds.ToString()+"."+duration.Milliseconds.ToString();
            Report.Log(ReportLevel.Info,duration.Minutes.ToString()+":"+duration.Seconds.ToString()+"."+duration.Milliseconds.ToString());
            System.IO.File.AppendAllText(@"C:\\measure\\test.txt","Test Case: "+TestCase.Current.Name+"Duration: "+s+Environment.NewLine);
Am getting the error message as:

at time.UserCodeModule1.Ranorex.Core.Testing.ITestModule.Run() in c:\Users\212453330\Documents\Ranorex\RanorexStudio Projects\time\time\UserCodeModule1.cs:line 71 at Ranorex.Core.Testing.TestSuiteModule.RunInternal(DataContext parentDataContext

line 71 is last line in the code

Re: Calculate time between steps in a recording

Posted: Wed Oct 28, 2015 2:35 pm
by odklizec
I believe there is missing an important part of the error message? Could you please post the entire error message?

BTW, have you started entire Test Suite or just the code module?

Re: Calculate time between steps in a recording

Posted: Wed Oct 28, 2015 2:53 pm
by ejji09
Please find the attachmet of erropr report log file

I have started only the code module.

Re: Calculate time between steps in a recording

Posted: Wed Oct 28, 2015 3:01 pm
by odklizec
ejji09 wrote:I have started only the code module.
This is what I've expected. The reason of the failure is this code...
TestCase.Current.Name
This code will work only if code/recording module is started from Test Case, which means you need to start the entire test suite.

Re: Calculate time between steps in a recording

Posted: Wed Oct 28, 2015 3:14 pm
by ejji09
yeah you are right! :)

when I run the entire test case then its working fine. Is there a way where i can run only the module and get the report in .txt file by changing something at TestCase.Current.Name, I tried a bit i was unable to get the name of the module in the .txt file.

Re: Calculate time between steps in a recording

Posted: Wed Oct 28, 2015 3:42 pm
by odklizec
The name of actual module could be obtained by this code:
Ranorex.Core.Reporting.TestReport.CurrentTestModuleActivity.TestModuleName
But I'm not sure if it would work if the module is started outside the test suite?

Re: Calculate time between steps in a recording

Posted: Wed Oct 28, 2015 3:49 pm
by krstcs
You can also get the recording module's class name (which is the same as the module name) by using this:

Code: Select all

string moduleName = this.GetType().Name;

edit: Should have been GetType, not GetClass...

Re: Calculate time between steps in a recording

Posted: Fri Oct 30, 2015 5:10 pm
by ejji09
Hi.

How to save the file name with time stamp?

Code: Select all

System.IO.File.AppendAllText(@"C:\\izaz\\test.txt"+"DateTime: "+dt+Environment.NewLine+"TestCase: "+TestCase.Current.Name+Environment.NewLine+"Duration: "+s+Environment.NewLine);
Thanks.

Re: Calculate time between steps in a recording

Posted: Mon Nov 02, 2015 11:54 am
by ejji09
:idea:
Solved:

Code: Select all

String filename = System.DateTime.Now.ToString("ddMMyyyyhhmmss");

System.IO.File.AppendAllText(@"C:\\save\\"+filename+.txt"+"DateTime: ",dt+Environment.NewLine+"TestCase: "+TestCase.Current.Name+Environment.NewLine+"Duration: "+s+Environment.NewLine);
or

Code: Select all

System.IO.File.AppendAllText(@"C:\\save\\"+System.DateTime.Now.ToString("ddMMyyyyhhmmss")+".txt", Envireonment.NewLine + "Duration: " + s + Environment.NewLine);
:D :D :D :D