Open ModalDialog blocks TestClient

Class library usage, coding and language questions.
lasombra
Posts: 14
Joined: Tue Mar 04, 2008 1:36 pm

Open ModalDialog blocks TestClient

Post by lasombra » Tue Mar 04, 2008 1:54 pm

My client application opens a new modal dialog to create a new user:

Code: Select all

Ranorex.Element elemMenuItem = mainForm.Element.FindChild(Ranorex.Role.MenuItem, "New User");
elemMenuItem.DoDefaultAction();
Ranorex.Form myForm2 = Ranorex.Application.FindFormTitle("Standard User");
While the dialog is open, the client application is blocked until I close the modal dialog by hand. What or how I can do with ranorex, that the client application don't block and continues running?

I know that there is an issue with UIA, where I must to subscribe to an UIA event (e.g. SubscribeToInvoke) before I click on a button, that opens a modal dialog. But this possibility I miss in ranorex.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Tue Mar 04, 2008 5:03 pm

The Element.DoDefaultAction() method blocks until the default action is executed. So, if the default action is an EventHandler that opens a modal dialog, the DoDefaultAction method will block until the EventHandler returns. It is similar to the .NET Control.Invoke mechanism.

The solution is either to use Mouse.ClickElement to click on the menu item or to spawn a new thread that searches for the Element and calls DoDefaultAction:

Code: Select all

Thread thread = new Thread(new ThreadStart(delegate
{
    Ranorex.Element elemMenuItem = mainForm.Element.FindChild(Ranorex.Role.MenuItem, "New User");
    elemMenuItem.DoDefaultAction();
}));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

Thread.Sleep(500);
Ranorex.Form myForm2 = Ranorex.Application.FindFormTitle("Standard User");
// do stuff with myForm2
Regards,
Alex
Ranorex Support Team

lasombra
Posts: 14
Joined: Tue Mar 04, 2008 1:36 pm

Post by lasombra » Tue Mar 04, 2008 7:43 pm

Thanks for your answer. Unfortunately I have a strange problem with the Mouse.ClickElement, for which reason I'd like the DoDefaultAction(). Well, the problem is the following: I have a loop (can be a sequence too, the problem appears anyway) to create various users:

Code: Select all

 for (int i = 0; i < 5; i++)
{
   myTreeView = myHelper.FindTreeView("mTreeView");
   myTreeView.SelectItem("Everyone");
   Ranorex.Application.Sleep(300);
   myHelper.clickMenuItem("File/New/User");
   myHelper.clickMenuItem("Edit/Rename");
   Ranorex.Application.SendKeys("UserGroup" + i + "{ENTER}");

and in the class myHelper

Code: Select all

public void clickMenuItem(String menuPath)
{
   foreach (String menuItem in menuItems)
  {
	String[] menuItems = menuPath.Split('/');
	foreach (String menuItem in menuItems)
	{
		Ranorex.Element elemMenuItem = mainForm.Element.FindChild(Ranorex.Role.MenuItem, menuItem);
		Ranorex.Mouse.ClickElement(elemMenuItem);
	}
   }
}
The code opens a menu "File>New>User". This works well the first time. The second time the element "User" found by FindChild() contains the wrong x coordinate, which sends the mouse to the wrong point. The x coordinates seems to be the same as the x coordinate of the main application window (Ranorex.Form).

I don't understand why the first time to object properties are correct and the second time not.

Update
In some cases (e.g. other menu items) the use of the Ranorex.Mouse.ClickElement() don't work either the first time.

The menu is a MenuStrip.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Wed Mar 05, 2008 11:39 am

Well, that's in fact a strange problem. Are your testing application and the application under test separate processes?

You can also try using the MenuStrip.SelectItemText() method that basically replaces your myHelper.clickMenuItem method.

Anyway, if your using a separate thread to find the MenuItem element and invoke DoDefaultAction in that thread (like in my sample code posted before), it should also work.

Regards,
Alex
Ranorex Support Team

lasombra
Posts: 14
Joined: Tue Mar 04, 2008 1:36 pm

Post by lasombra » Fri Mar 07, 2008 4:31 pm

Thanks Alex. In this form (threads) I could achieve what I wanted.