Page 1 of 1

Different instances of teardown

Posted: Mon May 07, 2018 11:25 am
by Hamed.S
Dear Ranorex Team,
once again a beginner question:

I want to implement a teardown which has 3 instances.
First it should try to click on the close button with softkill().
After it should use use the Ranorex function CloseApplication [closeWindow] with the recording mediumkill()
and if both fails it should invoke the CloseApplication[killProcess] which is called hardkill().
If one fails, its important to get a screenshot, so I could detect what happend.

What do I want?
I want to see, if there is something interfering with the regular closing. If yes, it should report it and try an other way to close the software.


My idea is to use try catch but I failed in implementing it.
Any idea how to achieve my goal, if possible even easier?

My coding skills are really basic :)

I wish you all a great day!
Greets H.

Re: Different instances of teardown

Posted: Tue May 08, 2018 2:51 pm
by McTurtle
Hello Hamed,

You could try to make first a click on the close button, then a close application and then a kill application. You could then make them all optional by changing the setting for "Continue on fail" to "true" for all the actions:
close.png
If you need code, then you could use the following snippet:
var Processes=Process.GetProcessesByName("notepad");
			string ProcessName="";
			var repo = KeepTryingToCloseAppRepository.Instance;
			var CloseButton = repo.UntitledNotepad.Close;

			if (Processes.Length!=0)
			{
				ProcessName=Processes[0].ProcessName;
				
				Report.Info(string.Format("Found {0} processees of application {1}. Attempting to close one with x button."
				                          ,Processes.Length.ToString(), ProcessName));
				
				CloseButton.Click();
			}
			else if(Processes.Length==0)
			{
				Report.Info(string.Format("All processes of application {0} closed.", ProcessName));
			}
			
			Delay.Seconds(5);
			Processes=Process.GetProcessesByName("notepad");
			
			if (Processes.Length!=0)
			{
				Report.Info(string.Format("Found {0} processees of application {1}. Attempting to close one with Host.Current.CloseApplication()."
				                          ,Processes.Length.ToString(), ProcessName));
				
				Host.Current.CloseApplication(repo.UntitledNotepad.Self,new Duration(0));
			}
			else if(Processes.Length==0)
			{
				Report.Info(string.Format("All processes of application {0} closed.", ProcessName));
			}
			
			Delay.Seconds(5);
			Processes=Process.GetProcessesByName("notepad");
			
			if (Processes.Length!=0)
			{
				Report.Info(string.Format("Found {0} processees of application {1}. Attempting to close the rest with KillProcess()."
				                          ,Processes.Length.ToString(), ProcessName));
				
				foreach (Process _proces in Processes)
				{
					_proces.Kill();
				}
			}
			else if(Processes.Length==0)
			{
				Report.Info(string.Format("All processes of application {0} closed."
				                          , ProcessName));
			}
			
			Delay.Seconds(5);
			Processes=Process.GetProcessesByName("notepad");
			
			if (Processes.Length!=0)
			{
				Report.Info(string.Format("Found {0} processees of application {1}. Failed to kill processes."
				                          ,Processes.Length.ToString(), ProcessName));
				
				Host.Current.CloseApplication(repo.UntitledNotepad.Self,new Duration(0));
			}
			else if(Processes.Length==0)
			{
				Report.Info(string.Format("All processes of application {0} closed.", ProcessName));
			}
		}
Don't forget to add the using directive "using System.Diagnostics;".

This is for closing notepad. The RanoreXPath to the close button should be /form[@title~'Notepad']/?/?/button[@accessiblename='Close']. Note that I used a regex for the form, else the button might not be found if multiple instances are open. You can open like 10 notepad instances in order to test the code.

Let me know if it helps.

Regard,
McTurtle