Need VB example of how to verify whether a dialog exists

Ask general questions here.
MikeV
Posts: 37
Joined: Wed Apr 04, 2012 6:01 pm

Need VB example of how to verify whether a dialog exists

Post by MikeV » Wed Jul 24, 2013 10:58 pm

Does anybody have a small piece of VB.NET code they can share, as an example of how to verify whether a desktop dialog exists? I have been all other the Forum and User Guide, and I can't seem to find a complete example that does this, "complete" being the key here, with all the Dim statements needed. I do not use the repository; I use the xpath. This is not a web-based product. I include error handling and log errors into a .log file.

This should be really simple, but the correct syntax seems to be evading me.

What I need to do: Look for the titlebar of a dialog that is named "Message" and click on the titlebar. If the titlebar is not found within 1 minute, error out or fail in some way that I can handle it.

Right now, the dialog is not being displayed, so I need to have it go to the error handling, which will enter the failure into a log file.

I'm using Ranorex Studio 4.0.5.

Code I have tried:

Code: Select all

xpath = "/form[@title='Message']/titlebar[@accessiblerole='TitleBar']"
Ranorex.Validate.Exists(xpath, 60000)
Dim titlebar As New Ranorex.TitleBar(xpath)
titlebar.Click()
In this case, Ranorex.Validate.Exists(xpath, 60000) seems to be ignored, but I don't understand why; I'm error handling for Ranorex.ValidationException, but it never gets into that part of the error handler. Instead, it gets Ranorex.ElementNotFoundException when it tries to do Dim titlebar As New Ranorex.TitleBar(xpath) with message "No element found for path '/form[@title='Message']/titlebar[@accessiblerole='TitleBar']' within 10s.", which is not surprising, but that doesn't help me wait for a specified duration of 60s, instead of the default 10s.

I've tried various ways of using FindSingle with a duration of 60000, but how can I do FindSingle on titlebar if I can't Dim titlebar? When I try to build the test with FindSingle, I keep getting build errors. TryFindSingle does not seem any better than FindSingle.

When the application that is being tested displays the Message dialog, the test code works fine, so it can identify the titlebar when it exists.

Any help is appreciated!

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Need VB example of how to verify whether a dialog exists

Post by Ciege » Wed Jul 24, 2013 11:36 pm

Have you tried getting the response from the validate to a bool variable then using and if bool = true do this, else that...?

Have you tried stepping through your code in debug mode and see what is going on during runtime?

You mention "I'm error handling for Ranorex.ValidationException". How are you doing this? I don't see it in your code snippet.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

MikeV
Posts: 37
Joined: Wed Apr 04, 2012 6:01 pm

Re: Need VB example of how to verify whether a dialog exists

Post by MikeV » Thu Jul 25, 2013 12:06 am

I've tried this:

Dim exists As Boolean = True
exists = Ranorex.Validate.Exists(xpath, 60000)

and it doesn't build: "Expression does not produce a value. (BC30491)"


Have not stepped through the code yet, but can try it.


Error trapping is the following Try/Catch format, with code that prints to a log file after each Catch.

Code: Select all

Try
  xpath = "/form[@title='Message']/titlebar[@accessiblerole='TitleBar']"
  Ranorex.Validate.Exists(xpath, 60000)
  Dim titlebar As New Ranorex.TitleBar(xpath)
  titlebar.Click()
Catch ex As Ranorex.ElementNotFoundException
  'Print to log
Catch ex As Ranorex.ValidationException
  'Print to log
Catch ex As System.Exception
  'Print to log
End Try
Actual output in the log looks like this:

2013.07.24_14:03:28 trying Ranorex.Validate.Exists(xpath, 60000)
2013.07.24_14:03:32 trying Dim titlebar As New Ranorex.TitleBar(xpath)
2013.07.24_14:03:42 Ranorex.ElementNotFoundException in SADialogYes:
2013.07.24_14:03:42 No element found for path '/form[@title='Message']/titlebar[@accessiblerole='TitleBar']' within 10s.
2013.07.24_14:03:42 at Ranorex.Core.Element.FindSingle(RxPath path, Duration timeout)
at Ranorex.Core.Element.op_Implicit(String path)
at SA.SA.SADialogYes.Run()

and the first 2 lines are temporary debug stuff to make sure that the Dim statement is the one that is failing. Line 3 is informational, and means that it got into Catch ex As Ranorex.ElementNotFoundException.

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Need VB example of how to verify whether a dialog exists

Post by Ciege » Thu Jul 25, 2013 12:21 am

Use one of these Validates to get a bool return.
http://www.ranorex.com/forum/validate-e ... tml#p11290

You could also just skip using the Validate and issue your own FindSingle, kind of like this:

Code: Select all

Dim MyTitleBar As TitleBar = MyForm.FindSingle(".//titlebar[@accessiblerole='TitleBar']")  
Note, my code might be a little off since I use C# and not VB...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

MikeV
Posts: 37
Joined: Wed Apr 04, 2012 6:01 pm

Re: Need VB example of how to verify whether a dialog exists

Post by MikeV » Thu Jul 25, 2013 1:03 am

Thanks, Ciege!

In your code example, how would I get "MyForm", if the dialog does not exist?

A C# example that does what I need to do will be fine, if you or anyone else has one.

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Need VB example of how to verify whether a dialog exists

Post by Ciege » Thu Jul 25, 2013 6:43 pm

Basically the same way as finding the TitleBar...

Code: Select all

Dim MyForm As Form = Host.local.FindSingle(".//form[@title='Message'']") 
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

MikeV
Posts: 37
Joined: Wed Apr 04, 2012 6:01 pm

Re: Need VB example of how to verify whether a dialog exists

Post by MikeV » Thu Jul 25, 2013 11:35 pm

Thanks again, Ciege!

Between the time you last replied, and this current post, I figured this out based on Advanced Code Examples here:
http://www.ranorex.com/support/user-gui ... mples.html
I used the TryFindSingle example shown there.

The code that works for me is this:

Code: Select all

xpath = "/form[@title='Message']/titlebar[@accessiblerole='TitleBar']"
Dim titlebar As Ranorex.TitleBar = Nothing
Dim found As Boolean = Host.Local.TryFindSingle(Of Ranorex.TitleBar)(xpath, 60000, titlebar)
If found = True
  'Continue processing here.
  titlebar.Click()
Else
  'Report the error to the log, including the xpath that was not found.
End If
There are many other ways of doing this. See more in Advanced Code Examples. I did not try the exact method Ciege suggests, but I am sure it would work, since he is a Super-Ranorex-Expert First Class.

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Need VB example of how to verify whether a dialog exists

Post by Ciege » Thu Jul 25, 2013 11:46 pm

MikeV wrote:Thanks again, Ciege!
You are welcome...
MikeV wrote:I did not try the exact method Ciege suggests, but I am sure it would work, since he is a Super-Ranorex-Expert First Class.
Hahahahaahahahaaha :lol: Thanks...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...