Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 49 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
49
Dung lượng
1,17 MB
Nội dung
397 Needed Components for Testing User-Defined Controls this event, find the try-catch clause. You need to comment out the Messege.Show() line in the curly brackets of the catch statement and add two lines of code like this: formUT = GUITestUtility.StartControlUT(applicationUT, startupForm); formUT.Show(); The first line calls the StartControlUT() method coded in the preceding section and assigns the test bench to the formUT object as the application under test. The formUT object has a Show() method that makes the application under test visible and surveyable on the screen. Listing 13.2 shows the new code and the commented-out code in bold for the btnStartGUITest_Click() event. ➲ Listing 13.2 New and Commented Code in Bold for the btnStartGUITest_Click() Event private void btnStartGUITest_Click(object sender, System.EventArgs e) { TypesToVerify = new TypeVerificationSerializable(); GUISequence = 0;//added for chapter 8 GUITestSeqList = new GUITestUtility.GUIInfoSerializable(); opnAUT.Title = "Specify an Application Under Test"; opnAUT.Filter = "GUI Applications(*.EXE; ➥*.DLL)|*.EXE;*.DLL|All files (*.*)|*.*"; if (opnAUT.ShowDialog() == DialogResult.OK) { applicationUT = opnAUT.FileName; GUITestSeqList.AUTPath = applicationUT; GetTypeToTestFromAUT(); try { formUT = (Form)GUITestUtility.StartAUT(applicationUT, startupForm); } catch (InvalidCastException ex) { //Chapter 13 //MessageBox.Show(ex.Message); formUT = GUITestUtility.StartControlUT(applicationUT, startupForm); formUT.Show(); } } else { return; } } 4351Book.fm Page 397 Tuesday, September 28, 2004 11:21 AM 398 Chapter 13 • User-Defined and COM-Based Controls ➲ As discussed, the try-catch clause in the btnStartGUITest_Click() event starts the appli- cation to test. When the object under test is a custom GUI control, the execution of the state- ment in the try curly brackets throws an error. The error will be caught and the statements in the catch curly brackets will work smoothly to start the custom GUI control on the screen. Thus, the updating of the AutomatedGUITest.cs file is completed. You can compile the project and correct errors. The coding task in the next section for modifying the GUITestScript class seems a little clumsy. But it has been refactored and works pleasantly for testing the new GUI components. Handling Exceptions in the GUITestScript Class If you run the existing GUITestScript class at this moment and start to test a custom GUI con- trol, several code fragments throw exceptions when they accept custom GUI controls as param- eters. The code addition in this section will add a handful of try-catch statements to catch these exceptions. The existing code in the try clause has taken care of the regular .NET applications. New code will be added to process the custom control inside the catch statements. A StartAUT() method has also been implemented for the GUITestScript class since Chapter 7, and it becomes the first target for code modification. You can locate the StartAUT() method in the GUITestScript.cs file and add some lines of code. Listing 13.3 shows the code that should be after the modification. Listing 13.3 The Modified StartAUT() Method for the GUITestScript Class with the New Code and the Commented Code in Bold private void StartAUT() { seqGUIUT = new GUITestUtility.GUIInfoSerializable(); object obj = (object)seqGUIUT; GUITestUtility.DeSerializeInfo(guiTestDataStore, ref obj); seqGUIUT = (GUITestUtility.GUIInfoSerializable)obj; string AUTPath = seqGUIUT.AUTPath; string startupType = seqGUIUT.AUTStartupForm; int hwnd = 0; if (AUT == null) { try //chapter 13 { AUT = (Form)GUITestUtility.StartAUT(AUTPath, startupType); hwnd = (int)AUT.Handle; } catch (InvalidCastException ex) //chapter 13 4351Book.fm Page 398 Tuesday, September 28, 2004 11:21 AM 399 Needed Components for Testing User-Defined Controls ➲ { AUT = GUITestUtility.StartControlUT(AUTPath, startupType); AUT.Show(); Control ctrlAUT = AUT.Controls[0]; hwnd = (int)ctrlAUT.Handle; } } //int hwnd = (int)AUT.Handle; StringBuilder sbClsName = new StringBuilder(128); GUITestActions.GetClassName(hwnd, sbClsName, 128); string clsName = sbClsName.ToString(); string winText = AUT.Text; string pText = ""; GUITestActions.SynchronizeWindow(ref hwnd, ➥ref winText, ref clsName, ref pText); } The first added line prepares a hwnd variable as an integer object to hold the handle of the application under test (note that the commented-out line at the end of the new code section has served this purpose). The statements inside the try clause were implemented for testing the regular .NET applications and have been discussed in previous chapters. Inside the catch clause, the first two lines of code are copied from the btnStartGUITest_Click() event of the AutomatedGUITest.cs file. As it has been explained, the custom control is contained in the control test bench. The test bench serves as the parent window of the custom control. The custom control contains child controls, which will receive the testing actions. Thus, the test script needs to use the custom control window to retrieve the parent window handle. The third and fourth lines of code inside the catch clause simply get the window handle of the custom control, indexed to be zero inside the Controls property of the test bench. Thus, the custom control is invoked as a window application on the screen smoothly. The second modification for the GUITestScript class happens to be the RunsScript() method, which has been modified a few times since its inception. The modified code is in Listing 13.4. Listing 13.4 The Code for the RunsScript() Method with the New and Commented Lines in Bold and the Second Part Omitted private void RunsScript() { guiTestActionLib = Path.Combine(progDir, "GUITestActionLib.xml"); GUITestUtility.GUIInfo guiUnit = ➥(GUITestUtility.GUIInfo)seqGUIUT.GUIList[clickNum]; 4351Book.fm Page 399 Tuesday, September 28, 2004 11:21 AM 400 Chapter 13 • User-Defined and COM-Based Controls string ctrlAction = ➥GUITestUtility.GetAGUIAction(guiTestActionLib, guiUnit.GUIControlType); StringBuilder sb = new StringBuilder(10000); try //chapter 12 { //Control ctrlTested = ➥(Control)GUITestUtility.VerifyField(AUT, guiUnit.GUIControlName); Control ctrlTested; //chapter 13 try { ctrlTested = ➥(Control)GUITestUtility.VerifyField(AUT, guiUnit.GUIControlName); } catch(Exception ex) { ctrlTested = ➥(Control)GUITestUtility.VerifyField(AUT.Controls[0], guiUnit.GUIControlName); } GUITestActions.GetWindowText((int)ctrlTested.Handle, sb, 10000); } //catch{} catch { sb = new StringBuilder(guiUnit.GUIText); } //object[] paramArr = new object[4]; } Within an existing try clause, a new try-catch clause is inserted. The first existing assignment for the Control object, ctrlTested, is commented out. But a new addition declares ctrlTested as a Control variable without assignment. Then the try statement starts to initialize the declared ctrlTested variable for a regular .NET application under test. If the try statement throws an exception, the catch statement locates the custom control inside the test bench by a zero index value of the Controls collection. Last, the window handle is found and the GetWindowText() method is invoked to find the current window text of the GUI object under test. Finally, the existing catch clause, which has been empty, is modified for testing the ActiveX controls. At this point, the window text for a GUI object in an ActiveX control can not be found by calling the GetWindowText() method. When the GetWindowText() method fails, the existing catch statement catches the error and initializes the StringBuilder object with the value the window text of that GUI object had when the testing data was collected. 4351Book.fm Page 400 Tuesday, September 28, 2004 11:21 AM 401 Needed Components for Testing User-Defined Controls ➲ NOTE To avoid repeating discussions in previous chapters, the AutomatedGUITest tool will not be implemented for verification of testing the ActiveX controls. Third, you need to locate the AddTestVerification() method within the GUITestScript class and add a couple more try-catch clauses within the existing try-catch clause. The mod- ified AddTestVerification() method is in Listing 13.5. Listing 13.5 The Modified AddTestVerification() Method with the Newly Added try-catch Clause in Bold. private void AddTestVerification() { if (AUT == null) return; string VerifyDataStore = guiTestDataStore.Replace(".xml", "_verify.xml"); TypeVerificationSerializable verifyTypes = new TypeVerificationSerializable(); object obj = (object)verifyTypes; GUITestUtility.DeSerializeInfo(VerifyDataStore, ref obj); verifyTypes = (TypeVerificationSerializable)obj; TypeVerification oneType = ➥(TypeVerification)verifyTypes.TypeList[clickNum - 1]; object resulted = null; foreach (TestExpectation fieldName in oneType.MemberList) { TestExpectation tested = fieldName; try { try { resulted = GUITestUtility.VerifyField(AUT, tested.VerifyingMember); } catch //chapter 13 { resulted = ➥GUITestUtility.VerifyField(AUT.Controls[0], tested.VerifyingMember); } tested.isField = true; } catch(Exception ex4) { 4351Book.fm Page 401 Tuesday, September 28, 2004 11:21 AM 402 Chapter 13 • User-Defined and COM-Based Controls ➲ try { resulted = GUITestUtility.VerifyProperty(AUT, tested.VerifyingMember); } catch { resulted = ➥GUITestUtility.VerifyProperty(AUT.Controls[0], tested.VerifyingMember); } tested.isProperty = true; } VerifyAlphanumericResult(ref tested, resulted); VerifyClipboard(ref tested, resulted); //chapter 9 VerifyLabel(ref tested, resulted); VerifyGroupBox(ref tested, resulted); //Chapter 11 VerifyRadioButtonCheckBox(ref tested, resulted); } } You may have discovered that the added code inside the try clause is for testing the regular .NET applications. The code added into the catch clause is for testing the custom GUI control. The test script always locates the GUI object under test within the Control collection of the test bench by looking for a zero index value. We can use this deduction to complete the modification for two more methods, the GetCheckedButtonInGroup() method and the DeterminePreCheckedStatus() method. The new code for these two methods are in Listing 13.6 and Listing 13.7, respectively. Listing 13.6 The Added try-catch Clause for the GetCheckedButtonInGroup() Method with the New Code in Bold private bool GetCheckedButtonInGroup(RadioButton rdBtn, ref string ErrorMsg) { int parentHandle = GUITestActions.GetParent((int)rdBtn.Handle); //Control parentGrp = (Control)GUITestUtility.VerifyField(AUT, parentHandle); Control parentGrp; try { parentGrp = (Control)GUITestUtility.VerifyField(AUT, parentHandle); } catch { parentGrp = ➥(Control)GUITestUtility.VerifyField(AUT.Controls[0], parentHandle); } 4351Book.fm Page 402 Tuesday, September 28, 2004 11:21 AM 403 Needed Components for Testing User-Defined Controls ➲ foreach (Control ctrl in parentGrp.Controls) { try { RadioButton rdCtrl = (RadioButton)ctrl; if (rdCtrl.Name == rdBtn.Name) { if (!rdBtn.Checked) { ErrorMsg = rdBtn.Name + " is not checked!"; return false; } } else { if (rdCtrl.Checked) { ErrorMsg = "Other than or beside the " + rdBtn.Name + " is checked, the " + rdCtrl.Name + " is also checked!"; return false; } } } catch{} } return true; } The modification of Listing 13.6 simply comments and rewrites the existing statement: Control parentGrp = (Control)GUITestUtility.VerifyField(AUT, parentHandle); The rewritten code includes the commented-out statement inside a newly added try-catch statement in order to verify a check box or a radio button within a group box. Listing 13.7 The Added try-catch Clause for the DeterminePreCheckedStatus() Method with the New Code in Bold private bool DeterminePreCheckedStatus(GUITestUtility.GUIInfo guiUnit) { bool isChecked = false; if (guiUnit.GUIControlType == "System.Windows.Form.CheckBox") { //CheckBox chckBx = ➥(CheckBox)GUITestUtility.VerifyField(AUT, guiUnit.GUIControlName); CheckBox chckBx; try { chckBx = 4351Book.fm Page 403 Tuesday, September 28, 2004 11:21 AM 404 Chapter 13 • User-Defined and COM-Based Controls ➥(CheckBox)GUITestUtility.VerifyField(AUT, guiUnit.GUIControlName); } catch { chckBx = ➥(CheckBox)GUITestUtility.VerifyField(AUT.Controls[0], guiUnit.GUIControlName); } isChecked = chckBx.Checked; } return isChecked; } Similar to the modification in Listing 13.6, Listing 13.7 commented out the existing statement: CheckBox chckBx = ➥(CheckBox)GUITestUtility.VerifyField(AUT, guiUnit.GUIControlName); The statement was rewritten in an added try-catch clause. The try statement helps to find out whether a CheckBox control under test in a regular .NET application is checked or not. The catch statement helps to find out whether a CheckBox control under test in a custom GUI control is checked or not. Now the code is complete and you can press F5 to build and run the AutomatedGUITest tool. If there are any compiling errors, you can correct the code by comparing your code with the sample code downloaded from www.sybex.com. Thus, the AutomatedGUITest project is prepared for testing custom GUI controls, such as the developed user-defined .NET and the COM-based ActiveX controls. We will conclude this chapter with two testing examples. Two More Examples After the AutomatedGUITest project has been updated, testing a custom .NET GUI control is identical to testing a regular .NET application. It is well known that .NET programs produce managed assemblies and COM-based components are not managed code. Therefore, we still need a little trick to convert COM-based ActiveX controls into managed assemblies. The next sections will start with the .NET user-defined control and then solve the conversion dilemma. Testing a Customized .NET GUI Control After you build and run the AutomatedGUITest tool with the new enhancement, you can click the Start GUI Test button to test the customized LoginCtrl control developed with the Microsoft Visual Studio .NET IDE. The steps are as follows: 1. When the open file dialog box appears, navigate to the C:\GUISourceCode\Chapter13\ LoginCtrl\bin\Debug folder to select the LoginCtrl.dll assembly. Click the Open button. 4351Book.fm Page 404 Tuesday, September 28, 2004 11:21 AM 405 Two More Examples 2. The Types under Test form appears with the control class LoginCtrl.UserControl1. The class name, UserControl1, is assigned by the Microsoft Visual Studio .NET IDE by default. (You can change the default value, but for simplification, we didn’t change it when we developed it.) Select the check box and click the OK button. 3. The LoginCtrl control appears within the test bench (Figure 13.3). From the Automated- GUITest tool, click the GUI Survey button. The testing tool completes the GUI survey in a few seconds (depending on the computer system). 4. When the AutomatedGUITest tool reappears, locate and double-click the left edge of textBox1 under the Window Text column in the DataGrid. When the GUI Test Data Collector pops up, in the Text Entry field, type {BS} and {DEL} key code to let the test script press the Backspace key and the Delete key nine times in order to clear the text box for a new entry and append a user ID to the key code. The complete entry looks similar to {BS 9}{DEL 9}my user ID. (If it is needed, you can refer to Table 10.1 for code of the special keys.) Select the simple verification method. At this moment, the GUI Test Data Collector looks like Figure 13.4. Click the OK button. 5. Locate and double-click the left edge of textBox2 in the DataGrid under the Window Text column. This time, type a password after the Backspace and Delete key code in the Text Entry field of the GUI Test Data Collector, such as {BS 9}{DEL 9}my password. Select the simple verification method and click the OK button. 6. Last, locate and double-click the left edge of the OK button in the DataGrid. Select the simple verification method and click the OK button. 7. Close the test bench and click the Run Test button from the AutomatedGUITest tool. When the save file dialog box appears, type in a filename such as C:\Temp\TestLoginCtrl.xml. After you click the Save button, the testing data is saved and the test completes in a few seconds. FIGURE 13.3 The test bench with the LoginCtrl control started by the testing tool 4351Book.fm Page 405 Tuesday, September 28, 2004 11:21 AM 406 Chapter 13 • User-Defined and COM-Based Controls FIGURE 13.4 Collecting data to test the txtUserID object Note that the LoginCtrl control has only the standard GUI controls, but it has no custom code. The purpose of developing such a control is purely for testing the updated Automated- GUITest tool in this chapter. This statement also applies to the ActiveX example. Testing an ActiveX GUI Control The developed tool is now able to test .NET Windows Forms applications and custom con- trols. Although code has been added to the tool to handle the ActiveX control testing, a special treatment is in need before you submit the ActiveX control to testing. The ActiveX controls are COM-based applications. The testing tool developed in C# doesn’t consider the COM-based controls as .NET Windows forms. In order to adapt the Automated- GUITest tool to test the ActiveX controls, you must generate a wrapper control that derives from the System.Windows.Forms.AxHost class. This wrapper control contains an instance of the underlying ActiveX control. It knows how to communicate with the ActiveX control, but it appears as a Windows Forms control. The testing tool uses the wrapper control to access the properties, methods, and events of the ActiveX control. To make things easy, the Microsoft Visual Studio .NET IDE comes with a tool, aximp.exe, which can be found in the C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin folder. The tool is also called ActiveX Control Importer. It can convert ActiveX control type defi- nitions in a COM type library into a Windows Forms control. When a line command is issued from the Visual Studio .NET 2003 command prompt, the aximp.exe generates a wrapper class 4351Book.fm Page 406 Tuesday, September 28, 2004 11:21 AM [...]... code of the Win32 API functions and for testing the AutomatedGUITest project as the book progressed Chapters 4, 5, and 6 laid the foundation for a GUI test library This library provided methods for GUI survey and testing In Chapters 7 and 8, you used the foundation of the AutomatedGUITest project to develop the testing tool with basic functions for GUI survey, testing, and verification In Chapters 9... error to halt the testing tool when a non NET application is started by the tool In the real testing world, you can drag and drop the monkey head inside the front-end interface of any application If you drag and release the newly implemented GUI probe to a point outside of the AutomatedGUITest tool, a GUI survey starts However, if you drag and release the probe inside the AutomatedGUITest tool interface,... the OK window text and enter the correct values for testing the OK command button as shown in Figure 13.9 Click the OK button from the GUI Test Data Collector 6 Close the test bench and click the Run Test button from the testing tool When the save file dialog box appears, type in C:\Temp\TestActiveX.xml as the filename to save the testing data store The testing data is saved and the testing completes... This entry enables the testing tool to find the correct GUI handling method for this testing step Also, you need to type some text in the Text Entry field, such as Effective GUI Test Automation This entry will be typed in Notepad when it is under test by the AutomatedGUITest tool The GUI Test Data Collector now appears as it does in Figure 14.2 Click the OK button This is the only testing step for now... you can save and build the project But the newly added method is still not used in the AutomatedGUITest tool at this point You need complete the rest of the updating before a test can be done 416 Chapter 14 • Testing Issues for Non NET Applications Making the AutomatedGUITest Tool Probe the GUI Interface When the AutomatedGUITest tool starts the application with the late binding method, the tool holds... Rashka, and John Paul Automated Software Testing: Introduction, Management, and Performance Boston: Addison-Wesley, 1999 Dustin, Elfriede Effective Software Testing: 50 Specific Ways to Improve Your Testing Boston: Addison-Wesley, 2002 Graham, Dorothy, and Mark Fewster Software Test Automation: Effective Use of Test Execution Tools Boston: Addison-Wesley, 2000 Koomen, Time, and Martin Pol Test Process... practical step-by-step guide to structured testing Boston: Addison-Wesley, 1999 Li, Kanglin, and Mengqi Wu Effective Software Test Automation: Developing an Automated Software Testing Tool Alameda, Calif.: Sybex, 2004 Marick, Brian When Should a Test Be Automated? [Cited Dec 22, 2003] Available from World Wide Web www .testing. com/writings/automate.pdf, 1998 Messerschmidt, Thomas, and Mark Harbin WinRunner... starts, the tool holds a copy of it as an object Therefore, the GUI survey result list has two sources of data to specify testing steps and verification Later, when testing starts, the GUITestScript class follows the testing steps in the testing data store and compares the consequences of the selected GUI components, the status changes of the object copy of the application held by the tool, and the expected... enabling the testing tool to test NET applications This chapter updated the AutomatedGUITest tool with a few methods for a GUI survey of applications other than NET applications Although the sample code in the book has not been updated to carry out a fully automated verification for testing these applications, you can incorporate the methods you learned in the book and make a tool for your testing purposes... 14.2 The appearance of the GUI Test Data Collector after the entries for testing the Notepad application 426 Chapter 14 • Testing Issues for Non NET Applications You can close the Notepad application, which was open for the GUI survey Click the Run Test button from the AutomatedGUITest tool The save file dialog box appears Give a name to save the testing data, such as TestNotepad.xml, and click the . control and then solve the conversion dilemma. Testing a Customized .NET GUI Control After you build and run the AutomatedGUITest tool with the new enhancement, you can click the Start GUI Test. • Testing Issues for Non .NET Applications Making the AutomatedGUITest Tool Probe the GUI Interface When the AutomatedGUITest tool starts the application with the late binding method, the tool. pleasantly for testing the new GUI components. Handling Exceptions in the GUITestScript Class If you run the existing GUITestScript class at this moment and start to test a custom GUI con- trol,