How to check if element is a separator? : Automation API

How to check if element is a separator?

Class library usage, coding and language questions.

How to check if element is a separator?

Postby AlexDozer » Thu Jan 20, 2011 1:25 pm

Hello,

I would like to check the order of the menus. So, I have e.g. to test if the element I get from Childrens is a seperator. But how can I check this?

With is or as it doesn't works because Ranorex.Core.Element and Ranorex.Seperator doesn't inherit from the same base class. The only way I know to check is this not really nice way:

Code: Select all
try
{
    Ranorex.Seperator sep = (Ranorex.Seperator) item.Children[x];
    return true;
}
catch(InvalidCastException)
{
    return false;
}


Could anyone help me?

Regards, Alex
AlexDozer
 
Posts: 33
Joined: Wed Sep 09, 2009 6:41 pm

Re: How to check if element is a separator?

Postby AlexDozer » Thu Jan 20, 2011 2:14 pm

Maybe, it exists some other way to test the order. The main problem is that I can't get the children of the menu on the normal way.

If the menu is not opened, the count of Children is 1, but the menu has 12 items. So, I opened the menu first and checked then for the childrens. Here I get a ElementNotFoundException, but the rxPath is still the same. Why Ranorex doesn't find the menu if it is open? Maybe, this is a bug.

So I tried the way that I go from the MenuItem to the Parent. But MenuItem has no Parent-Property. But when I use the Element-Property I have then a Parent-Property and can get the Childrens. Here, the count is 12 what is correct. But now I have Ranorex.Core.Elements and that is my problem :(

Here is now my solution the get the Childrens:
Code: Select all
rep.MenuBar.ItemPatientListAndData.Element.Parent.Children


Any Idea?
AlexDozer
 
Posts: 33
Joined: Wed Sep 09, 2009 6:41 pm

Re: How to check if element is a separator?

Postby Support Team » Thu Jan 20, 2011 3:13 pm

Hi,

So, I have e.g. to test if the element I get from Childrens is a seperator.


if you want to iterate thru your menu items and check if they are Separators or MenuItems you can do this with following code:

foreach (Ranorex.Adapter item in repo.ContextMenuFile.ContextMenuFile.Children)
{
	Ranorex.Separator sep = item.As<Ranorex.Separator>();
	if (sep != null)
		Report.Info("Seperator!");
	
	Ranorex.MenuItem mi = item.As<Ranorex.MenuItem>();
	if (mi != null)
		Report.Info("MenuItem!");
}


You have to assure that the context menu is opened at the time you check it's elements.
You can do this by ading a click item to the context menu.

Regards,
Tobias
Support Team
User avatar
Support Team
Site Admin
 
Posts: 4845
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria

Re: How to check if element is a separator?

Postby AlexDozer » Thu Jan 20, 2011 5:15 pm

Hello Tobias,

thank you! This helps me very much.

I also found the problem that Ranorex couldn't reach MenuItems that are opened. Problem is that we identify the MenuItems with the text which is below the MenuItem because we don't have til now AutomationIDs.
Code: Select all
menuitem/text[@text='File']/..


But when the Menu is opened, the text is gone. So, Ranorex can not find the MenuItem and throws an exception. My workaround is now that I put the Object into a variable on startup, so, I can reach the item also if its open.
Code: Select all
static Ranorex.MenuItem MenuFile;

public static bool OpenMenuFile()
{
    if (MenuFile == null)
        MenuFile = rep.CXX7.MenuBar.MenuFile.Element;

    return OpenMenu(MenuFile, "File");
}


Maybe this helps other tester who have the same problem.

Regards, Alex
AlexDozer
 
Posts: 33
Joined: Wed Sep 09, 2009 6:41 pm


Return to Automation API

Who is online

Users browsing this forum: No registered users and 0 guests