Effective GUI Test Automation Developing an Automated GUI Testing Tool phần 9 pot

46 175 0
Effective GUI Test Automation Developing an Automated GUI Testing Tool phần 9 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

351 Summary FIGURE 10.2 The appearance of the GUITestDataCollector form when collecting data to test a TextBox control 5. Go back to the testing tool interface. Repeat step 4 to locate and add the Add and Copy but- ton clicks into the testing data store, but leave the Text Entry field empty. Click the OK button. 6. Close the C# API Text Viewer application. Click the Run Test button from the testing tool. When the save file dialog box appears, navigate to a folder and type a filename for the data store, such as C:\Temp\TestTextEntry.xml. Click the Save button. The first execution of this testing case will be completed in a few seconds and the test result appears. From the result, you can see that the C# marshaling code for the Win32 AccessCheckAnd- AuditAlarm() function is copied into the clipboard. The testing tool performed the testing task as expected and the test passes. Summary In the previous chapters, the AutomatedGUITest tool was enabled to generate inputs from mouse actions. This chapter used the SendKeys class of the System.Windows.Forms namespace of the .NET Framework to generate keystrokes for GUI testing. The chapter included an example that showed how to store text input and enable the tool to test a TextBox control. You can also use the AutomatedGUITest tool to test a shortcut for triggering a GUI event with a set of keystrokes. 4351Book.fm Page 351 Tuesday, September 28, 2004 11:21 AM 4351Book.fm Page 352 Tuesday, September 28, 2004 11:21 AM Chapter 11 Testing RadioButton and CheckBox Controls 4351Book.fm Page 353 Tuesday, September 28, 2004 11:21 AM 354 Chapter 11 • Testing RadioButton and CheckBox Controls T he C# API Text Viewer has been implemented with various kinds of GUI controls to help with our Win32 API programming as well as to serve as a sample application to be tested automatically. The previous chapters have demonstrated how to build the AutomatedGUITest tool to test Command Button, ListBox, TextBox, and Label controls. This chapter introduces methods to upgrade the tool for testing RadioButton and CheckBox controls. Characteristics of RadioButton and CheckBox Controls When the values of a property are expressed with a Boolean type, the software developer usually places a RadioButton or a CheckBox control on the front-end GUI interface. Users can check or uncheck such a control to change its value between true and false (or 1 and 0). Thus, the RadioButton or CheckBox control of the .NET Framework has a property with a name of Checked . A checked RadioButton object has a solid black dot inside an empty circle, and a checked CheckBox object has a check mark inside a square box. Otherwise the circle or the square is empty. In order to test whether a RadioButton or CheckBox control acts as desired, the value of the Checked property is in need of inspection. However, the RadioButton and the CheckBox controls are used under different circumstances. With regard to increasing automation of software testing, here I list some differences in the usage of these two controls: ● When an application needs to provide several options for the users to choose from, the RadioButton controls are used. RadioButton controls are presented in groups; that is, a group has more than one RadioButton control. No more than one control in a group can be checked. To test a RadioButton control, the tester should be aware of the behaviors of the other RadioButton controls in the same group. A CheckBox control can be presented alone. ● A group of RadioButton controls are usually contained within a GroupBox control. An application can have more than one group of RadioButton controls to represent different option categories. The RadioButton controls within the same GroupBox control are related to one another and only one control’s Checked property can have a true value. The others must have false values. But when an application has a group of CheckBox controls, an individual CheckBox control behaves independently from the others. More than one or all of the CheckBox controls can be checked. ● The Checked property of a RadioButton control becomes true whenever the RadioButton control is clicked. For example, when an unchecked RadioButton control is clicked, the value of its Checked property changes from false to true. But if the RadioButton control is already true and is clicked, the value of its Checked property remains true. However, the Checked property of a CheckBox control changes from true to false or from false to true whenever the control is clicked. A testing tool should be able to read the Checked value before and after a click is performed and assign an expected result for the automated testing. 4351Book.fm Page 354 Tuesday, September 28, 2004 11:21 AM 355 Updating the AutomatedGUITest Project Based on the discussion in this section, the rest of this chapter will show how to update the AutomatedGUITest tool to test RadioButton and CheckBox controls of an application. Updating the AutomatedGUITest Project To complete this chapter, the GUITestActionLib.xml document and three classes, GUITest- Utility , GUITestVerification , and GUITestScrip , are in need of updating. After the updating, the AutomatedGUITest tool will be able to test whether a click turns on/off a CheckBox control and whether only the desired option is selected from a group of RadioButton controls. Overloading a Method in the GUITestUtility Class The AutomatedGUITest project has implemented methods in the GUITestUtility class to start applications under test, serialize and deserialize testing data and results, and find the appropriate handling method for testing a specified GUI object. It also provides methods to determine whether the member under verification is a field or a property of the application under test. Among them is the VerifyField() method to ensure that the member in need of verification is a field. The VerifyField() method takes an object of the application being tested and the name of the GUI control of interest as parameters. The first coding task in updating the AutomatedGUITest tool is to overload the VerifyField() method so that it can pass the application object and the handle of the GUI control as param- eters. Such a method is necessary when a RadioButton control is under test. The overloaded method will be used to find the GroupBox control as a parent window containing a group of RadioButton controls. As you have for the previous chapters, make a C:\GUISourceCode\Chapter11 folder and copy the AutomatedGUITest, GUITestLibrary, and XmlTreeViewer project folders from the C:\GUISourceCode\Chapter10 folder to the C:\GUISourceCode\Chapter11 folder. When a RadioButton or CheckBox control is tested, the mouse action on the control is the same as clicking a command Button control. Thus, this chapter will use the existing HandleCommandButton() method for testing a RadioButton or CheckBox control. Before start the coding tasks, let’s add this GUI handling method into the C:\GUISourceCode\ Chapter11\AutomatedGUITest\bin\Debug\GUITestActionLib.xml document as shown in bold in Listing 11.1. ➲ Listing 11.1 The Modified GUITestActionLib.xml Document <GUIActions> <System.Windows.Forms.CheckBox>HandleCommandButton ➥ </System.Windows.Forms.CheckBox> <System.Windows.Forms.RadioButton>HandleCommandButton 4351Book.fm Page 355 Tuesday, September 28, 2004 11:21 AM 356 Chapter 11 • Testing RadioButton and CheckBox Controls ➥ </System.Windows.Forms.RadioButton> <HandleTextBoxWithTextEntry>HandleTextBoxWithTextEntry ➥ </HandleTextBoxWithTextEntry> <System.Windows.Forms.Label>HandleCosmeticGUIs ➥ </System.Windows.Forms.Label> <System.Windows.Forms.GroupBox>HandleCosmeticGUIs ➥ </System.Windows.Forms.GroupBox> <System.Windows.Forms.ListBox>HandleListBox ➥ </System.Windows.Forms.ListBox> <System.Windows.Forms.RichTextBox>HandleTextBox ➥ </System.Windows.Forms.RichTextBox> <System.Windows.Forms.TextBox>HandleTextBox ➥ </System.Windows.Forms.TextBox> <System.Windows.Forms.Button>HandleCommandButton ➥ </System.Windows.Forms.Button> <Field>VerifyField</Field> <Property>VerifyProperty</Property> <Synchronization>SynchronizeWindow</Synchronization> </GUIActions> Then open the AutomatedGUITest project from the new C:\GUISourceCode\Chapter11 folder. When the AutomatedGUITest project is open, the GUITestLibrary and the XmlTreeViewer projects are also open in the Solution Explorer. From the Solution Explorer, navigate to the GUITestLibrary project and the GUITestUtility.cs file. Double-click the GUITestUtility.cs filename. The cursor is now in the code editor for the GUITestUtility class. Next, start a new VerifyField() method. The code for overloading the method is in Listing 11.2. ➲ Listing 11.2 The Code for the Overloaded VerifyField() Method //chapter 11 overload for finding Parent window public static object VerifyField(object typeUnderTest, int fieldHandle) { System.Windows.Forms.Form frm = (System.Windows.Forms.Form)typeUnderTest; string fieldName = ""; foreach (Control ctrl in frm.Controls) { if ((int)ctrl.Handle == fieldHandle) fieldName = ctrl.Name; } return VerifyField(typeUnderTest, fieldName); } As discussed, the new VerifyField() method takes an object of the application under test and the handle of the GUI control of interest as parameters. Since the application is passed as an object, the first line of the code uses an unboxing method to explicitly convert the object to 4351Book.fm Page 356 Tuesday, September 28, 2004 11:21 AM 357 Updating the AutomatedGUITest Project ➲ ➲ a Form object. A Form object is a parent window to house the other child GUI controls. These child GUI controls are declared as the fields of the application. After a fieldName variable is initialized as a string object, a foreach loop enumerates the child controls until the handle value of the passed parameter matches the enumerated child GUI control. Then it assigns the name of this GUI control to the fieldName variable. After the foreach iteration, the name of the GUI control is found and the original VerifyField() method can be invoked to return an object of this GUI control. Now you can build the GUITestLibrary project to make sure the code is correctly added. If there are compiling errors, you can correct them by comparing your code with that in Listing 11.2. Adding Code to the TestExpectation Class The TestExpectation class is included in the GUITestVerification.cs file beside the GUITest- Verification class. You have implemented quite a few public fields for the TestExpectation class to hold data of the expected results and actual results for testing different aspects of a GUI control. Since a RadioButton or a CheckBox control has a Checked property, you need to add public fields to the TestExpectation class as shown in Listing 11.3. Listing 11.3 The Public Fields for Testing RadioButton and CheckBox Controls in the TestExpectation Class // Chapter 11 // Properties of RadioButton and CheckBox controls public bool ExpectedCheckVal; public bool ActualCheckVal; public bool RadioCheckboxPass; public string RadioBtnErrMsg; The first and the second Boolean fields get the checked status of the expected and the actual value, respectively. The third Boolean field determines whether the testing passes or fails. If the test fails, the RadioBtnErrMsg field reports the cause of the failure. After preparing these fields, you can code an AssertRadioButtonCheckBox() method to read and assign values to these fields. Listing 11.4 shows the code for the AssertRadioButton- CheckBox() method. Listing 11.4 The Added Code for the AssertRadioButtonCheckBox() of the TestExpectation Class // Chapter 11 public void AssertRadioButtonCheckBox(bool oneRdChecked, string errMsg) { if (!oneRdChecked) { 4351Book.fm Page 357 Tuesday, September 28, 2004 11:21 AM 358 Chapter 11 • Testing RadioButton and CheckBox Controls ➲ RadioCheckboxPass = false; RadioBtnErrMsg = errMsg; return; } if (ActualCheckVal.Equals(ExpectedCheckVal)) { RadioCheckboxPass = true; } else { RadioCheckboxPass = false; } } The AssertRadioButtonCheckBox() method uses two if statements. The first if statement takes the values of the passed parameters and determines that only one RadioButton control is checked in the group. Otherwise, the test fails and the RadioBtnErrMsg field is assigned. The second if statement executes when only one of the RadioButton controls in a group has a true Checked value. It compares the ActualCheckVal against the ExpectedCheckVal to deter- mine whether the test passes or fails and then completes the verification. After you finish typing the code, you can compile the project and correct the compiling errors if there are any. The full code list for the updated TestExpectation class is also included in the downloadable sample code from www.sybex.com. Enhancing the Testing Scope of the GUITestScript Class The fields and methods in the GUITestUtility and TestExpectation classes are implemented to help the GUITestScript class conduct the testing. In this section, you will add the needed helper methods to the GUITestScript class and enable the AutomatedGUITest to test RadioButton and CheckBox controls. Listing 11.5 shows the code for a GetCheckedButtonInGroup() method to count the number of true Checked values in a group of RadioButton controls. Listing 11.5 The Code for the GetCheckedButtonInGroup() Method of the GUITestScript Class private bool GetCheckedButtonInGroup(RadioButton rdBtn, ref string ErrorMsg) { int parentHandle = GUITestActions.GetParent((int)rdBtn.Handle); Control parentGrp = (Control)GUITestUtility.VerifyField(AUT, parentHandle); foreach (Control ctrl in parentGrp.Controls) { try { RadioButton rdCtrl = (RadioButton)ctrl; 4351Book.fm Page 358 Tuesday, September 28, 2004 11:21 AM 359 Updating the AutomatedGUITest Project 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 GetCheckedButtonInGroup() method takes two parameters, the RadioButton control under inspection and the ErrorMsg string as a reference parameter, which will be reassigned if an error occurs. The first line of the GetCheckedButtonInGroup() method calls the GetParent() method from the GUITestActions class to obtain the handle value of the GroupBox control that hosts the group of the RadioButton controls. Then it calls the overloaded VerifyField() method to find the GroupBox control as a Control object. A Control object can be a child of another win- dow or it can have child GUI objects. In the case of testing a RadioButton control, a foreach loop enumerates the group of RadioButtons and determines if the clicked RadionButton control has a true Checked value or whether any of the not-clicked RadioButton controls have true Checked values. If the clicked RadioButton control is the only control that has a true Checked value, the method returns a true value to conclude the enumeration. After the GetCheckedButtonInGroup() method is coded, you can code a VerifyCheckedValue() helper method, as shown in Listing 11.6. ➲ Listing 11.6 The Code for the VerifyCheckedValue() Method private void VerifyCheckedValue(ref TestExpectation fieldName, ➥RadioButton resulted) { try { 4351Book.fm Page 359 Tuesday, September 28, 2004 11:21 AM 360 Chapter 11 • Testing RadioButton and CheckBox Controls ➲ fieldName.ActualCheckVal = resulted.Checked; string errMsg = ""; bool oneIsChecked = GetCheckedButtonInGroup(resulted, ref errMsg); fieldName.AssertRadioButtonCheckBox(oneIsChecked, errMsg); } catch (Exception ex) { fieldName.RadioBtnErrMsg = ex.Message; } } A TestExpectation object, fieldName, and the actual object of the RadioButton control under verification are passed into the VerifyCheckedValue() method. Within a try-catch clause, the VerifyCheckedValue() method obtains the actual value of the Checked property. Then it invokes the GetCheckedButtonInGroup() in Listing 11.5 and the AssertRadioButtonCheckBox() method of the fieldName object. If the executions in the try clause encounter difficulties, the catch clause assigns an error message to the fieldName.RadioBtnErrMsg field. Listings 11.5 and 11.6 are the code of the helper methods for RadioButton control testing. Now you need to add another field and two helper methods in the GUITestScript class for test- ing a CheckBox control. Listing 11.7 is the code for a preChecked field and a DeterminePre- CheckedStatus() method in the GUITestScrip class. Listing 11.7 Code for Creating a preChecked Field and a DeterminePreCheckedStatus() Method in the GUITestScript Class //Determine prechecked conditions for assigning expected check value private bool preChecked; private bool DeterminePreCheckedStatus(GUITestUtility.GUIInfo guiUnit) { bool isChecked = false; if (guiUnit.GUIControlType == "System.Windows.Form.CheckBox") { CheckBox chckBx = ➥(CheckBox)GUITestUtility.VerifyField(AUT, guiUnit.GUIControlName); isChecked = chckBx.Checked; } return isChecked; } After the declaration for the preChecked field, the DeterminePreCheckedStatus() method uses an if statement to inspect whether a CheckBox object is already checked or not before the click occurs. The value of the preChecked field will be assigned by calling the DeterminePreChecked- Status() method in the RunsScript() method of the GUITestScript class. 4351Book.fm Page 360 Tuesday, September 28, 2004 11:21 AM [...]... Clause to Handle the Nonexisting GUI Controls private void RunsScript() { guiTestActionLib = Path.Combine(progDir, "GUITestActionLib.xml"); GUITestUtility.GUIInfo guiUnit = ➥(GUITestUtility.GUIInfo)seqGUIUT.GUIList[clickNum]; string ctrlAction = ➥GUITestUtility.GetAGUIAction(guiTestActionLib, guiUnit.GUIControlType); StringBuilder sb = new StringBuilder(10000); try //chapter 12 { Control ctrlTested =... the handle of the application’s main form 386 Chapter 12 • Menu Clicking for GUI Test Automation Therefore, an if-else clause is used after a name of the GUI handling method is enumerated from the GUITestActionsLib.xml document At this point, you have completely updated the AutomatedGUITest project for discovering and testing menu items of an application You can build and run the AutomatedGUITest tool. .. AddTestVerification() method The execution of the new code is the last step to completely testing and verifying a GUI control at this point Now, if there is no error in the code, you can press F5 to build and run the AutomatedGUITest tool As I’ve done in the previous chapters, in the next section I will use an example to demonstrate the new capabilities of the AutomatedGUITest tool 364 Chapter 11 • Testing. .. close the C# API Text Viewer and click the Run Test button in the testing tool When the save file dialog box appears, type in a filename, such as C:\Temp\TestRadioButtons.xml, and click the Save button to save the testing input data After the testing data is saved, the tool automatically completes the test driven by the saved data You can see the testing actions and view the test results displayed on... for each item click An alternative is to use the submenu handle to obtain its parent window handle Now that you have an understanding of the menu handles and window handles, the rest of this chapter will add methods for the AutomatedGUITest tool to automatically conduct a menu survey and a specific menu clicking sequence Updating the GUITestAction Class with API Programming Menu testing is a new topic... methods coded in the GUITestActions class are useful for performing desired actions on various GUI objects and menu items The methods in the GUISurveyClass class, which use methods from the GUI test library, are for discovering and collecting all the GUI objects and menu items into a list Testers specify GUI testing cases by making choices from the list Specifying GUI objects for testing doesn’t really... use these methods to enhance your testing tool for testing other GUI objects with similar behaviors If the project under test and the tool to be developed are NET applications, you can directly apply the methods and the sample code Otherwise, you can only use the introduced methods and translate the sample code into your selected language in order to develop a similar tool In the last three chapters,... objects as GUITestUtility.GUIInfo objects The StartMenuSurvey() method is all the code needed to update the GUISurveyClass class for this chapter After the addition, the project can be built without compiling errors For the frmMain class (the AutomatedGUITest main form) and the GUITestScript class, only a little modification is needed, as discussed in the next section Enabling the AutomatedGUITest Tool for... paramArr[2] paramArr[3] paramArr[4] = = = = sb.ToString(); guiUnit.GUIClassName; guiUnit.GUIParentText; guiUnit.TextEntry; //chapter 10 Type guiTestLibType = new GUITestActions().GetType(); object obj = Activator.CreateInstance(guiTestLibType); MethodInfo mi = guiTestLibType.GetMethod(ctrlAction); //chapter 11 preChecked = DeterminePreCheckedStatus(guiUnit); try { mi.Invoke(obj, paramArr); } catch (Exception... that is created at the time the GUI object is created and visible on the screen It is easy to understand that the AutomatedGUITest tool doesn’t have the capability to see the invisible GUI objects of an application But the menu items on the top level are visible from the menu bar of every Windows application The GUI survey of the AutomatedGUITest tool has inspected the GUI interface of the C# API Text . keystrokes for GUI testing. The chapter included an example that showed how to store text input and enable the tool to test a TextBox control. You can also use the AutomatedGUITest tool to test a shortcut. document and three classes, GUITest- Utility , GUITestVerification , and GUITestScrip , are in need of updating. After the updating, the AutomatedGUITest tool will be able to test. to update the AutomatedGUITest tool to test RadioButton and CheckBox controls of an application. Updating the AutomatedGUITest Project To complete this chapter, the GUITestActionLib.xml

Ngày đăng: 12/08/2014, 16:21

Từ khóa liên quan

Mục lục

  • Chapter 11: Testing RadioButton and CheckBox Controls

  • Chapter 12: Menu Clicking for GUI Test Automation

  • Chapter 13: User-Defined and COM-Based Controls

Tài liệu cùng người dùng

Tài liệu liên quan