Suppose you have program A, by clicking A button in the B can open another form C, there are A lot of control in the form C, suppose you need to call button elements in the form C D, I met this question is as follows:
1) in A way test1 by AutomationElement can find and control button D, but in the process of call need to close and reopen the application, if after reopen the need to control button D, the need to write to find A - & gt; D process, and need to rename to control again, but the actual lookup code format is the same, what method can simplify the process? The code below
2) if you need in another way test2 action button D, now my practice is again A - & gt; D find it again, and then control D, excuse me what method can be directly in the test2 directly control test1 D have found the button elements?
Private void test1 ()
{
The Process p=Process. The Start (@ "D: \ \ Program Flie \ A.e xe");
AutomationElement root=AutomationElement. RootElement;
AutomationElement formA=null;
Int numWait=0;
Do
{
FormA=root. FindFirst (TreeScope. Children, new PropertyCondition (AutomationElement NameProperty, "A"));
Thread.sleep (50);
NumWait++;
} while (numWait & lt; 500 & amp; & Null==formA);
If (null==formA)
{
Throw new NullReferenceException (" Could not find A ");
}
The else
{
AutomationElement buttonB=formA. FindFirst (TreeScope Descendants, new PropertyCondition (AutomationElement NameProperty, "B"));
UIA. ButtonClick (buttonB);//get a UIA. Cs file, can simulate click on the button action
AutomationElement formC=formA. FindFirst (TreeScope Descendants, new PropertyCondition (AutomationElement NameProperty, "C"));
AutomationElement btnD=formC. FindFirst (TreeScope Descendants, new PropertyCondition (AutomationElement NameProperty, "D"));
UIA. ButtonClick (btnD);
P.K ill ();
P. tart ();
AutomationElement root1=AutomationElement. RootElement;
AutomationElement formA1=null;
Int numWait1=0;
Do
{
FormA=root. FindFirst (TreeScope. Children, new PropertyCondition (AutomationElement NameProperty, "A"));
Thread.sleep (50);
NumWait1 + +;
} while (numWait & lt; 500 & amp; & Null==formA1);
If (null==formA1)
{
Throw new NullReferenceException (" Could not find A ");
}
The else
{
AutomationElement buttonB1=formA. FindFirst (TreeScope Descendants, new PropertyCondition (AutomationElement NameProperty, "B"));
UIA. ButtonClick (buttonB1);//get a UIA. Cs file, can simulate click on the button action
AutomationElement formC1=formA. FindFirst (TreeScope Descendants, new PropertyCondition (AutomationElement NameProperty, "C"));
AutomationElement btnD1=formC. FindFirst (TreeScope Descendants, new PropertyCondition (AutomationElement NameProperty, "D"));
UIA. ButtonClick (btnD1);
}
}
}
CodePudding user response:
Imagination QingQi!You said the original requirements under, often there will be a better way to handle your needs.
CodePudding user response:
Is in fact the original requirements to achieve automation program, but because there is no known API program, only through UIAutomation classes implement control, and then need to close the program during the process of A open again, in the same method to the finder A UI elements will exist A lot of repetitive code, so I want to ask is there any way to simplify the code,Another problem is that if you are in the same class test2 A method of direct manipulation has found another way test1 program in A UI elements, in addition to write it again in the test2 to find UI elements code, if there are other easy ways of UI elements can be implemented in different ways,
CodePudding user response:
You put the repetitive code into a method have to, if there is a different place, can design into different parameters, the similar function overloading,CodePudding user response:
That it can be: a. don't shut down, shut down when hidden form, cancel to close, the next call directly display.The algorithm to A program? It is better to reverse or check information themselves under implementation.
CodePudding user response:
You want to make a encapsulation, manipulation of the control program start for the first time, to find elements in a wrapper class, and then put the wrapper class in a public collection, no matter where you are less loop back just visit this collection,