传点东西,自动测试的。
class Program
{
[STAThread]
static void Main(string[] args)
{
// Launch the Windows Calculator (calc.exe)
Application application = Application.Launch("calc.exe");
// Get the main and only window
Window window = application.GetWindows()[0];
// Get a reference to the Edit TextBox (AutomationId = "403")
TextBox textBox = (TextBox)window.Get(SearchCriteria.ByAutomationId("403"));
// Get a reference to the RadioButton "Dec" (AutomationId = "307")
RadioButton radioButtonDecimal = window.Get<RadioButton>(SearchCriteria.ByAutomationId("307"));
// If this RadioButton can not be found, the calculator uses the Standard view
// Call the Scientfic/Wetenschappelijk menu to make more options visible
if (radioButtonDecimal == null)
{
// Click on menu View/Beeld (AutomationItem = "Item 2")
window.Get<Menu>(SearchCriteria.ByAutomationId("Item 2")).Click();
// Click on menu Scientific/Wetenschappelijk (AutomationItem = "Item 304")
window.Get<Menu>(SearchCriteria.ByAutomationId("Item 304")).Click();
// The window layout changes. Apparently we need to get the main window
// again to avoid exceptions when searching the buttons
window = application.GetWindows()[0];
}
// Press F6 to make sure we are using the decimal numerical system
window.Keyboard.PressSpecialKey(Core.WindowsAPI.KeyboardInput.SpecialKeys.F6);
// Focus the Edit TextBox and enter the text 54
textBox.Focus();
Keyboard.Instance.Enter("54");
// Press button "3" (AutomationId = "127") so the value will be 543
window.Get<Button>(SearchCriteria.ByAutomationId("127")).Click();
// Press the "+" button (AutomationId = "92")
window.Get<Button>(SearchCriteria.ByAutomationId("92")).Click();
// Enter the second value. Press button "6" (AutomationId = "130")
window.Get<Button>(SearchCriteria.ByAutomationId("130")).Click();
// Press button "7" (AutomationId = "131")
window.Get<Button>(SearchCriteria.ByText("7")).Click();
// Press the "=" button to calculate the result (AutomationId = "112")
window.Get<Button>(SearchCriteria.ByText("=")).Click();
// Focus the Edit TextBox again and copy the result to the clipboard
// by sending the keyboard combination CTRL+C
textBox.Focus();
Keyboard.Instance.HoldKey(Core.WindowsAPI.KeyboardInput.SpecialKeys.CONTROL);
Keyboard.Instance.Enter("C");
Keyboard.Instance.LeaveKey(Core.WindowsAPI.KeyboardInput.SpecialKeys.CONTROL);
// Press F8 to change the numerical system from decimal to binary
window.Keyboard.PressSpecialKey(Core.WindowsAPI.KeyboardInput.SpecialKeys.F8);
// Show the decimal result via the text on the clipboard
// and the corresponding binary value by accessing the text of the Edit TextBox
Console.WriteLine("543 + 67 = {0} (dec)", System.Windows.Forms.Clipboard.GetText());
Console.WriteLine("{0} (dec) = {1} (bin) ", System.Windows.Forms.Clipboard.GetText(), textBox.Text);
// Close the Calculator application
application.Kill();
Console.ReadKey();
}
}