Using reflection to get repository item

Class library usage, coding and language questions.
mrrioes
Posts: 6
Joined: Wed Oct 10, 2012 8:56 am

Using reflection to get repository item

Post by mrrioes » Wed Oct 24, 2012 1:37 pm

Solved!

Hi,
I would appreciate if you could help me to implement a method using reflection to get a repository item(I have no experience with reflection).
I want to write a method "TypeIntoField" that has two parameters:

Code: Select all

ArasTestEnvironmentRepository MyRepo = ArasTestEnvironment.ArasTestEnvironmentRepository.Instance;
...
public void TypeIntoField(string fieldName, string inputText)
{
}
...
Obvious I want to get the repository element with name "fieldName". In my Item Repository I have defined multiple app folders for each DOM:
  • MainWindow: /dom[@domain='domain' and @caption~'^MainWindowTitle']
    ItemWindow: /dom[@domain='domain' and @caption~'^ItemWindowTitle'']
This is necessary because in different windows can be elements with the same name(and for performance improvements)...
So each app folder must be searched. And thats the part were "it" begins. Let's say for example I will pass the following parameters to the method: fieldName = "Password", inputText = "123"
and the element is located at: MainWindow/LoginPage/Password
"LoginPage" is just a simple Folder in the Item Repository.
I have no idea how I can get this element but I tried something like this:
Edited again:
Here is my Solution:

Code: Select all

public void TypeIntoField(string fieldName, string inputText)
{
	...
	bool res = FindElement(MyRepo, fieldName, inputText);
	...
}			

Code: Select all

public bool FindElement(Object obj, string fieldName, string inputText){
	bool res = false;
	//get properties of current object
	PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
	//iterate over these properties
	foreach (PropertyInfo property in properties)
	{
		//if this object contains folder 
		if(property.PropertyType.Name.Contains("Folder") && property.Name != "ParentFolder")
		{
			Object newObj = property.GetValue(obj,null);
			Object obj2 = property.GetValue(obj,null);
			//recursive call to search the element in the folder
			res = FindElement(property.GetValue(obj,null), fieldName, inputText);
		}
		else if(property.PropertyType.Name == "InputTag")
		{
			if(property.Name == fieldName)
			{			
				MethodInfo click = property.PropertyType.GetMethod("Click", new Type[0]);
				MethodInfo pressKey = property.PropertyType.GetMethod("PressKeys",  new Type[] { typeof(string)});
				Object newObj2 = property.GetValue(obj,null);
				PropertyInfo[] properties2 = newObj2.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
			   
				Object obj3 = newObj2.GetType().GetProperty("Value").GetValue(newObj2,null);
				string currentValue = obj3.ToString();
				int counter = 0;
				while(currentValue != inputText){
					if(counter >10){
						return false;
					}
					click.Invoke(newObj2, new object[0]);
					pressKey.Invoke(newObj2, new object[] { "{LControlKey down}{Akey down}{Akey up}{LControlKey up}" });
					pressKey.Invoke(newObj2, new object[] { "{BACK}" });
					pressKey.Invoke(newObj2, new object[] { inputText });
					obj3 = newObj2.GetType().GetProperty("Value").GetValue(newObj2,null);
					currentValue = obj3.ToString();
					counter++;
				}
				return true;
			}
		}
		else
		{
		}
		if(res == true)
			return true;

	}
	return res;
}
Kind regards

mrrioes

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

Re: Using reflection to get repository item

Post by Support Team » Thu Oct 25, 2012 4:32 pm

Hello mrrioes,

Great that you could solve the issue and thanks for posing your solution.

Regards,
Bernhard
Rnaorex Support Team
.
Image