Page 1 of 1

Creating a Button Representation C#

Posted: Sat Dec 13, 2008 8:22 am
by IntegralAffiliate
I'm a little confused about the whole element thing - particularly by the fact that it doesn't have a constructor. So, say I want to drive an app by making a representation of an Important Button in the App. C#

say I have the string strRxPath of the button I want from the Spy...

One might think I could do something like ...

Button ImportantButton = new Button(strRxPath);

or

Button ImportantButton = new Button(new RxPath(strRxPath));

or

Button ImportantButton = new Button(new Element ( new RxPath(strRxPath)));


But actually the Button Constructor takes an Element which has no constructor! Where does this magic Element come from?

First, How do I create a button representation knowing its RxPath?

Two, What am I missing in my paradigm here? IOW: Why is what I'm writing not implemented?


Thank You.

Posted: Mon Dec 15, 2008 9:45 am
by Support Team
The short answer:

Code: Select all

Button ImportantButton = strRxPath;
// or
ImportantButton = new Button(strRxPath);
So, your first guess was right, the first code line you posted should work.

The long answer:
The Element class does not have a constructor, you can only get Element instances by searching for them by RxPaths. You do not construct an Element for a specific RxPath, but you specify an RxPath that searching for yields zero or more Elements. RxPaths can be relative or absolute. Absolute paths always search from the Host element (i.e. the element get from the ElementEngine.Instance.RootElement property), relative elements from the element that Find is invoked on.

So, when you begin with you automation, the only element you got is the root element (ElementEngine.Instance.RootElement) and you get all the elements you want by searching from this element using RxPaths:

Code: Select all

Element buttonElement = ElementEngine.Instance.RootElement.FindSingle(new RxPath("RxPathToMyButtonElement");
// or better: work with Adapters
Button button = Host.Local.Find<Button>("RxPathToMyButtonElement");
As this is a long line of code for such a simple operation, we introduced a shortcut notation that implicitly creates an RxPath instance from a string and searches for that RxPath from the root element:

Code: Select all

// implicitly searches for the element from the Host element
Element buttonElement = "RxPathToMyButtonElement";
You can then construct a Button adapter from this element:

Code: Select all

Button button = new Button(buttonElement);
Or even simpler, there's another shortcut notation for that:

Code: Select all

Button button = "RxPathToMyButtonElement";
The thing is that you do not create elements, you are always searching for them. And usually you should not even need the Element class or the Ranorex.Core namespace at all, as all the functionality is provided by the corresponding adapter classes (e.g. Button, Host, Form, ...) in the Ranorex namespace.

Regards,
Alex
Ranorex Support Team

Posted: Tue Dec 16, 2008 8:31 pm
by IntegralAffiliate
Thanks Alex, That Was Helpful.

- Alex