How to Handle Unhandled exception

Best practices, code snippets for common functionality, examples, and guidelines.
armstronghm24
Posts: 42
Joined: Tue Dec 20, 2016 10:16 pm

How to Handle Unhandled exception

Post by armstronghm24 » Fri Dec 08, 2017 7:35 pm

At any point in testing my application there is a possibility that an unhandled exception or similar critical error could occur. In that scenario, I would like my test case to fail, but close the error and continue with my remaining test cases without intervention. What is the best way to handle this without converting everything to user code? I did see some information about the popup watcher, but it seems like every test case would need to be converted to user code because an error could occur any place in the test script.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to Handle Unhandled exception

Post by odklizec » Sat Dec 09, 2017 10:00 am

Hi,

Popup watchwer should be exactly what you are looking for. It “watch” the application under test from second thread and eventually close unexpected dialogs. There is definitely no need to convert anything to user code. All you have to do is to add popup watcher methods as the very first actions in your test, ideally to program.cs. And of course, you have to define unexpected/error dialogs that should be handled via popup watcher. This must be done within the popup watcher definition. A more detailed explanation of popup watcher (with samples) can be found here:
https://www.ranorex.com/help/latest/cod ... tedDialogs
Hore this helps?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

armstronghm24
Posts: 42
Joined: Tue Dec 20, 2016 10:16 pm

Re: How to Handle Unhandled exception

Post by armstronghm24 » Thu Dec 28, 2017 10:39 pm

It helps a little knowing it can be done, but I do need some more direction. The example and instructions do not provide me with enough detail.
When you add this as the very first actions in your test, what does that mean? Should a module calling this code be used prior to every scenario in the test suite? Or does that mean something else?

armstronghm24
Posts: 42
Joined: Tue Dec 20, 2016 10:16 pm

Re: How to Handle Unhandled exception

Post by armstronghm24 » Fri Dec 29, 2017 8:20 pm

I have tried the following code, but can't get it to compile. I am getting the error: "Overload resolution failed because no accessible 'Watch' can be called with these arguments."
I've pretty much copied an example that works. Any ideas on why I am getting the error?

Code: Select all

Imports System.Threading
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Imports WinForms = System.Windows.Forms

Imports Ranorex
Imports Ranorex.Core
Imports Ranorex.Core.Reporting
Imports Ranorex.Core.Testing

Namespace PCTCSuite
Class Program
		
		'Repository declaration
		Public Shared repo As PCTC_Suite_Repository_SHARE = PCTC_Suite_Repository_SHARE.Instance
		
		<STAThread> _
		Public Shared Function Main(args As String()) As Integer
			Dim [Error] As Integer = 0

			' Uncomment the following 2 lines if you want to automate Windows apps
			' by starting the test executable directly
			'if (Util.IsRestartRequiredForWinAppAccess)
			'    return Util.RestartWithUiAccess();
			


			'define abort test shortcut
			Keyboard.AbortKey = System.Windows.Forms.Keys.Pause
			
			' create popupwatcher
			Dim myPopupWatcher As New PopupWatcher()
			myPopupWatcher.Watch(repo.Unknown_Error, AddressOf ClosePopUpDialogs)
			
			' Start PopupWatcher
			myPopupWatcher.Start()
			Report.Info("Info", "PopupWatcher started.")
			

			Try
				[error] = TestSuiteRunner.Run(GetType(Program), Environment.CommandLine)
			Catch e As Exception
				Report.[Error]("Unexpected exception occurred: " & e.ToString())
				[error] = -1
			End Try
			Return [Error]
		End Function
			
			Public Shared Sub ClosePopUpDialogs(myRepoElement As Ranorex.Core.Repository.RepoItemInfo, myElement As Ranorex.Core.Element)
				If myRepoElement Is repo.Unknown_Error Then
					Report.Warn("Warning - an unexpected dialog appeared!", repo.Unknown_Error.FlavorName)
					Report.Screenshot(repo.Unknown_Error)
					repo.ButtonOK.Click()
				
				End If
				
		 	End Sub
		
	End Class
	
End Namespace

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to Handle Unhandled exception

Post by odklizec » Fri Dec 29, 2017 8:36 pm

Hi,

I believe the watched element must be an Info object. So the path to element must be like this...
repo.Unknown_ErrorInfo

Eventually...
repo.Unknown_Error.SelfInfo

But I’m not quite sure it’s the cause of your problem. I’m not a VB guy.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

armstronghm24
Posts: 42
Joined: Tue Dec 20, 2016 10:16 pm

Re: How to Handle Unhandled exception

Post by armstronghm24 » Tue Jan 02, 2018 4:10 pm

Thanks for your input. When I try to use selfinfo, I get the error that selfinfo is not a member of Ranorex.form. Do I need to import something to be able to use that? Or is there something else I am missing?

McTurtle
Posts: 297
Joined: Thu Feb 23, 2017 10:37 am
Location: Benedikt, Slovenia

Re: How to Handle Unhandled exception

Post by McTurtle » Thu Jan 04, 2018 2:06 pm

Hello,

I have learned that messing with the Program.cs file can cause issues. Therefore, please create standalone code modules for declaring the pop-up watcher and then start the pop-up watcher in your recordings.

Furthermore, have you tried watching for the actual button that closes the error message and not for the form element that contains the message?

If you need more help, then a snapshot of the error that you would like to click away would be very useful. Can you post the snapshot (not screenshot) in your next reply? Ranorex Snapshot

Regards,
McTurtle

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

Re: How to Handle Unhandled exception

Post by Vaughan.Douglas » Thu Jan 04, 2018 4:57 pm

McTurtle wrote:
I have learned that messing with the Program.cs file can cause issues.
This is so true, if you're not into coding don't modify your "program" file. Although, what odklizec is suggesting should be perfectly safe. I've used this method in the past, but I now try to do anything that CAN be done in a recorded module IN a recorded module. And this all can be done in a recorded module.

Step 1: add the automation helpers to your solution.
Step 2: In your recorded module add a new action -> user code -> select from library -> Select "startPopupWatcher" from the PopupWatcherLibrary
Step 3: Drag the element you want to watch for from your repository and drop it into the "FindElement" argument (Ranorex will take care of getting the info object)
Step 4: Drag the element you want to click from your repository and drop it into the "ClickElement" argument (these can be the same element)
Step 5: enjoy
Step 6: when you want to stop the popup watcher you can either add StopPopupWatcher method to stop the specific watcher or StopAllPopupWatchers to kill them all.

Stopping the popup watcher DOES NOT need to be done in the same module. The popup watchers are tracked internally in the PopupWatcherLibrary by the object that is being watched. Trying to start a new PopupWatcher in the same element while the original watcher is still going will throw an error.

This should be all you need to do for getting through those unhanded exception warning. If you want to do something more complicated like grab a screenshot this will get more complicated, but can also be rolled into a custom user code method that can also be pulled into a recorded module.
Doug Vaughan

armstronghm24
Posts: 42
Joined: Tue Dec 20, 2016 10:16 pm

Re: How to Handle Unhandled exception

Post by armstronghm24 » Thu Jan 04, 2018 7:29 pm

Thanks for all the help.

At one point I did add the automation helpers, but it was added in C# although I want them in VB. I converted them to VB through Ranorex automatically, but that caused build errors. Can they be added in VB somehow?

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

Re: How to Handle Unhandled exception

Post by Vaughan.Douglas » Thu Jan 04, 2018 7:39 pm

armstronghm24 wrote:Thanks for all the help.

At one point I did add the automation helpers, but it was added in C# although I want them in VB. I converted them to VB through Ranorex automatically, but that caused build errors. Can they be added in VB somehow?
They're an independent project within your solution. You can cross the streams so to speak. I'm a VB guy myself, but the community at large is mostly C#.
Doug Vaughan

armstronghm24
Posts: 42
Joined: Tue Dec 20, 2016 10:16 pm

Re: How to Handle Unhandled exception

Post by armstronghm24 » Thu Jan 04, 2018 7:58 pm

I guess I can just leave the automation helpers in C#. Do you have to add the popup watcher to every recording module or just one and then it will be started throughout the whole suite until it runs into a module where it is stopped?

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

Re: How to Handle Unhandled exception

Post by Vaughan.Douglas » Fri Jan 05, 2018 1:44 pm

armstronghm24 wrote:I guess I can just leave the automation helpers in C#. Do you have to add the popup watcher to every recording module or just one and then it will be started throughout the whole suite until it runs into a module where it is stopped?
The popup watcher will run until you stop it. If you try to add another watcher to the same object you will get an error. The PopupWatcherLibrary holds all of the PopupWatcherObjects in a dictionary, so when you create a new watcher it gets checked against the dictionary. On the other hand you can stop popup watchers in any other module. If you try to stop a popup watcher that isn't running, you will receive a warning in your report.

What I would do is have the popup watcher started in my set up and run a stop all watchers in my final teardown.
Doug Vaughan

armstronghm24
Posts: 42
Joined: Tue Dec 20, 2016 10:16 pm

Re: How to Handle Unhandled exception

Post by armstronghm24 » Fri Jan 05, 2018 8:24 pm

Thanks for your help. Now how do I trigger test case fail flag and take a screenshot of the error for the report when the unexpected popup comes up? Some of the popup watcher examples had that within the code, but it doesn't look like the popup watcher from the automation helpers includes that in the code.

McTurtle
Posts: 297
Joined: Thu Feb 23, 2017 10:37 am
Location: Benedikt, Slovenia

Re: How to Handle Unhandled exception

Post by McTurtle » Mon Jan 08, 2018 2:00 pm

Hello armstronghm24,

Correct, the pop-up watcher in the automation helpers does not create a screenshot of the pop-up. However, it does create a screenshot of the full screen.

If this is not enough, then you can implement a screenshot of the element yourself.
In order to do that you would have to replace the WatchAndClick() method for the Watch() method and then create and report the screenshot in the callback method.

I assume that you have the PopupWatcherLibrary.cs file in front of you. The following modifications enable you to create a screenshot of the element that will be clicked:
AddCallback.png
The screenshot is now created of the same element that is clicked. You can play around with other methods in order to create a screenshot of the actual form or to click some other element within the form by using the public API. If you have the RanoreXPaths beforehand, then this should be no problem at all.

I recommend that you create your own class with this code if you are going to change the pop-up watcher. If you just modify the existing code, then this will probably lead to issues when you want to update the automation helpers.

Regards,
McTurtle
You do not have the required permissions to view the files attached to this post.

armstronghm24
Posts: 42
Joined: Tue Dec 20, 2016 10:16 pm

Re: How to Handle Unhandled exception

Post by armstronghm24 » Mon Jan 08, 2018 5:51 pm

Thanks for all the help. I am getting it to work and log an info item on the report. However I can't figure out how to change the report logging from Info to failure. An unexpected popup is an issue so I want the test case to fail on the error.