MSDN Magazine - March 2009 - (Page 38) the design issues that affect UI test automation. The CryptoCalc application is a simple, single-window user application that computes a cryptographic hash of a string. The application accepts a user-supplied string of up to characters in a TextBox control, converts the input string into an array of bytes, computes one of three types of crypto-hashes of the input bytes, and displays the resulting hashed bytes in a second TextBox control. I designed the app under test using Visual Studio with C# and named the Project CryptoCalc. The WPF template generates a skeleton application UI definition as a XAML file: Figure 2 WPF App File Menu One of the features I really like about WPF applications is the new Menu control paradigm, which, unlike WinForm menu items, treats menus and submenus as ordinary user controls. First, I dragged a main Menu container control to the top part of the CryptoCalc application: Visual Studio does not support a dragand-drop design for child MenuItem controls, so I added my menu items to the XAML definition file manually by adding a top-level File menu item: Notice that the top-level Window control definition does not contain a Name attribute. This is significant because, as we’ll see shortly, when you write test automation, an easy way to get a reference to a control using the MUIA library is to access the AutomationId property, which is generated by the compiler from the control’s Name attribute. Controls without a XAML Name attribute will not receive an AutomationId property. This idea is a specific, low-level example of the importance of considering application design issues for things such as security, extensibility, and test automation. Next, I added a Label control and a TextBox control by dragging items from the Visual Studio Toolbox onto the design surface: Enter string: WPF design supports the usual accelerator key syntax, such as _File. The tag is clean and easy. The ToolTip attribute will generate code that displays a short help message when the user mouses over the associated MenuItem control. When compiled, the Click OnFileExit attribute will generate C# code that expects an event handler with this signature: public void OnFileExit(object sender, RoutedEventArgs e) { // } So, after I added the regular application logic code to the CryptoCalc app, I manually added the event handler and then supplied functionality by placing this.Close into the method body. Notice Figure 3 StatCalc Application Code private void button1_Click(object sender, RoutedEventArgs e) { string input = textBox1.Text; byte[] inputBytes = Encoding.UTF8.GetBytes(input); if (radioButton1.IsChecked == true) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] hashedBytes = md5.ComputeHash(inputBytes); textBox2.Text = BitConverter.ToString(hashedBytes); } else if (radioButton2.IsChecked == true) { SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider(); byte[] hashedBytes = sha.ComputeHash(inputBytes); textBox2.Text = BitConverter.ToString(hashedBytes); } else if (radioButton3.IsChecked == true) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] blanks = System.Text.Encoding.UTF8.GetBytes(“ “); // 8 spaces des.Key = blanks; des.IV = blanks; des.Padding = PaddingMode.Zeros; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputBytes, 0, inputBytes.Length); cs.Close(); byte[] encryptedBytes = ms.ToArray(); ms.Close(); textBox2.Text = BitConverter.ToString(encryptedBytes); } } Notice that by default, these controls do receive normal Name attributes—label and textBox, respectively. Next, I placed three RadioButton controls inside a GroupBox control. Unlike the old WinForms GroupBox control, a WPF GroupBox accepts only a single item, so I wrapped my three RadioButton controls into a single StackPanel container (which can contain multiple items): MD5 Hash SHA1 Hash DES Encrypt Next, I placed a Button control to trigger the crypto-hash computation and a TextBox control to hold the result onto the main Window control: Compute 38 msdn magazine Test Run
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.