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)
{
}
...
- MainWindow: /dom[@domain='domain' and @caption~'^MainWindowTitle']
ItemWindow: /dom[@domain='domain' and @caption~'^ItemWindowTitle'']
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;
}
mrrioes