Identifying all the controls & elements present in a for

Class library usage, coding and language questions.
shobhana
Posts: 3
Joined: Mon Nov 05, 2007 9:26 am

Identifying all the controls & elements present in a for

Post by shobhana » Mon Nov 05, 2007 9:43 am

Given a form I want to know whether all the controls and elements present in it are identified by Ranorex.

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

Post by Support Team » Mon Nov 05, 2007 11:35 am

You can use the Ranorex Spy to identify controls and elements present in the form which are accessible by Ranorex.

Michael
Ranorex Support Team

shobhana
Posts: 3
Joined: Mon Nov 05, 2007 9:26 am

Post by shobhana » Mon Nov 05, 2007 11:44 am

I knew that we use Ranorexspy to find out the controls and elements details.But here my requirement is I have only the Form with me and I need to write a program which lists me all the controls and elements present in the form and I have to click on them and make sure it is able to identify.Everything I need to get it from the Ranorex functions,I wont use the ranorexspy at all.

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

Post by Support Team » Mon Nov 05, 2007 1:09 pm

To find all controls in a form use following C++ code

Code: Select all

HWND currentChild = RxFormGetFirstChild(form);
while (currentChild != NULL)
{
	// do something like: RxMouseClickControl(currentChild);
	currentChild = RxFormGetNextChild(form, currentChild);
}
or the Form.Controls property in C#.

To find all the elements in a form you have to write a recursive method that walks the element tree of the form:

Code: Select all

void DoForAllElements(ElementStruct* currentElement)
{
	// do something like: RxMouseClickElement(currentElement);
	for (int i = 0;  i < RxElementGetChildCount(currentElement); i++)
	{
		ElementStruct childElement;
		RxElementGetChild(currentElement, i, &childElement);
		DoForAllElements(&childElement);
	}
}

int main()
{
	HWND form = ...; // search for specific form
	ElementStruct formElement;
	RxControlGetElement(form, &formElement);
	DoForAllElements(&formElement);
}
Alex
Ranorex Support Team

shobhana
Posts: 3
Joined: Mon Nov 05, 2007 9:26 am

Post by shobhana » Mon Nov 05, 2007 2:33 pm

I am doing the C# coding,this is what I am doing in my code....

foreach (Control control in BABMgrForm.Controls)
{
Console.WriteLine("ClassName= " + control.ClassName);
Control ctrl = waitForFindClassName(BABMgrForm, control.ClassName);
Element[] tabelements = ctrl.Element.FindChildren(Role.PageTab);
foreach (Element item in tabelements)
{
Console.WriteLine("Item Name", item.Name);
LogMessage(item.Name);
}
Element[] outlineelements = ctrl.Element.FindChildren(Role.Outline);
foreach (Element outlineitem in outlineelements)
{
Console.WriteLine("Item Name", outlineitem.Name); LogMessage(outlineitem.Name);
}
}

But I am not able to find any element,neither all the controls are getting displayed.Am I doing anything wrong here.

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

Post by Support Team » Mon Nov 05, 2007 3:35 pm

If you have several controls all having the same ClassName, that could be a problem. I can only assume that the line

Code: Select all

Control ctrl = waitForFindClassName(BABMgrForm, control.ClassName);
internally uses the FindClassName method. If so, this will always get you the first instance of all controls matching the specified ClassName.

So the first thing to do is replacing the above line by

Code: Select all

ctrl = control;
Your code should then get you all the elements in all controls of BABMgrForm that have a Role of 'PageTab' or 'Outline'.

If you want all the elements of a control, use the recursive method described in my former post.

Alex
Ranorex Support Team