Rickie Software Test study notes-fifth week

Source: Internet
Author: User

Due to the limitations of the test conditions, we now have a tentative understanding of the black box test

Which about C # in the Black box test

In the daily coding process, we often conduct automated testing. The automated tests here do not refer to unit tests, but instead simulate manual input for fast, high-concurrency testing. The automation tools you can use are LoadRunner, as well as the very powerful test work platform currently in VS2010 (recording operation steps, generating code automatically). However, the proficiency of these tools also has a certain time cost, and, most important, for a programmer, that is not flexible. So, one of the more efficient ways to do this is to call the Windows API and write your own code.

Here's a simple demo. For simplicity, assume that an application exists:

1: Provide a WinForm form with a textbox on it, and a button;

2: Click button, will pop up the prompt box, the content of the box is the value of the textbox;

Now, the test requirements are as follows:

1: Run the above program on 300 machines;

2: Go to these 300 machines click on this button to see if the above function 2 has been implemented;

Obviously, there is no such a simple procedure, the actual situation may be click button, unified download a file, and the test requirements may become the load of the evaluation server. Now, the test department obviously does not have 300 people sitting on the client to verify the results of the test, this time, we need to provide an automated test tool to complete the necessary testing tasks.

The test tool is also a C # program, and its main purpose is to:

1: Get the window handle of the application above, then get the textbox handle and button handle;

2: Randomly fill in some characters for textbox;

3: Analog click button;

1.1:enumchildwindows Introduction

Here we need to introduce the next enumchildwindows,

Enumchildwindows is a good thing. You can enumerate all the child windows of a parent window:

BOOL Enumchildwindows (
HWND hwndparent,//Handle to parent window handle
Wndenumproc Lpenumfunc,//callback function//address of callback functions
LPARAM LPARAM //application-defined value//Your self-defined parameters
);

As simple as this, let's define a callback function like this:

BOOL CALLBACK Enumchildproc (
HWND hwnd,//Handle to child window
LPARAM LPARAM //application-defined value
);

When calling enumchildwindows this function, until the call to the most child window is enumerated or the callback function returns a false, otherwise it will continue to be enumerated.

1.2: The main source of simple examples

The main code for the test tool is as follows:

        private void Button1_Click (object sender, EventArgs e) {//Gets the form handle of the test program INTPTR Mainwnd            = FindWindow (null, "Formlogin");            list<intptr> Listwnd = new list<intptr> ();            Gets the handle of the OK button on the form IntPtr Hwnd_button = FindWindowEx (Mainwnd, New IntPtr (0), NULL, "OK");                Gets the handle of all controls on the form Enumchildwindows (Mainwnd, New CallBack (Delegate (INTPTR hwnd, int lParam) {                Listwnd.add (HWND);            return true;            }), 0);                    foreach (IntPtr item in Listwnd) {if (item! = Hwnd_button) { char[] Userchar = "Luminji".                    ToCharArray ();                    foreach (char ch in userchar) {Sendchar (item, CH, 100);        }}} SendMessage (Hwnd_button, Wm_click, Mainwnd, "0"); } public void Sendchar (IntPtr hand, Char ch,int sleeptime) {PostMessage (hand, WM_CHAR, CH, 0);        System.Threading.Thread.Sleep (Sleeptime);        } public static int wm_char = 0x102;        public static int wm_click = 0X00F5; [DllImport ("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage (IntPtr hWnd, Int. MSG, in          Tptr WParam, String lParam);            [DllImport ("User32.dll")] public static extern IntPtr FindWindowEx (IntPtr hwndparent, IntPtr Hwndchildafter,        String Lpszclass, String lpszwindow); [DllImport ("user32.dll", SetLastError = true)] public static extern IntPtr FindWindow (String lpclassname, String LP        Windowname);        [DllImport ("User32.dll")] public static extern int Anypopup (); [DllImport ("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int GetWindowText (INTPT        R HWnd, StringBuilder lpstring, int nmaxcount); [DllImport ("User32.dll")] public staticextern int EnumThreadWindows (IntPtr dwthreadid, CallBack lpfn, int lParam);        [DllImport ("User32.dll")] public static extern int Enumchildwindows (IntPtr hwndparent, CallBack lpfn, int lParam); [DllImport ("user32.dll", CharSet = CharSet.Ansi)] public static extern IntPtr PostMessage (IntPtr hwnd, int w        MSG, int wParam, int lParam); [DllImport ("user32.dll", CharSet = CharSet.Ansi)] public static extern IntPtr SendMessage (INTPTR hwnd, Int. wmsg, in               Tptr WParam, IntPtr lParam); [DllImport ("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr SendMessageA (IntPtr hwnd, int wmsg               , int wParam, int lParam); [DllImport ("user32.dll", CharSet = CharSet.Auto)] static extern int GetClassName (IntPtr hWnd, StringBuilder lpclass               Name, int nmaxcount); [DllImport ("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern int Getwindowtextlength        (IntPtr hWnd); [DllimPort ("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] public static extern IntPtr GetParent (IntPtr hWnd              ); Public delegate bool CallBack (INTPTR hwnd, int lParam);

Operating effect:

2: Difficulty: How to get the specified control handle

The attentive person may have found that, in the above, the text box is assigned the value of the place, using the following code:

            foreach (IntPtr item in Listwnd)            {                if (item! = Hwnd_button)                {                    char[] Userchar = "Luminji". ToCharArray ();                    foreach (char ch in Userchar)                    {                        Sendchar (item, CH, +);                    }                }            }

Assuming we have more than one text box on our form, in fact, this code will enter "Luminji" for all text boxes. This is not allowed in most applications, and we need to pinpoint the controls that need to be controlled.

When we get the handle to the OK button, we use the function:

IntPtr Hwnd_button = FindWindowEx (Mainwnd, New IntPtr (0), NULL, "OK");

And to get a handle to the text box, this function is not available, because all text boxes are untitled, that is, a value like "OK". Someone says, use the control ID. And look at:

2.1: Get the control ID

Non. NET programs, once the program is generated, the control ID is fixed, so this trick is used in non-. NET program, that is no better.

The function declaration that obtains the control handle according to the ID is as follows:

        [DllImport ("user32.dll", EntryPoint = "GetDlgItem")]        

Where the first parameter is the handle to the form, and the second parameter is the control ID.

But, obviously, this approach does not apply to us. NET program, as we will find that our. NET program does not run once, this ID is changed.

2.2: Get Control location

So, the final solution is to get the control handle we want, based on the position of the control. The declaration of this function is as follows:

OK, now the key is how to get the position of this control. We see in VS that a control has an x-coordinate and a y-coordinate, and in this textbox of the program above, its position in VS is "70,83", but instead of the coordinate value shown in VS, it does not include the title and the border. But this coordinate value can be used as a reference for our artificial comparison.

More precise coordinate values, which we write code to implement, are as follows:

            Enumchildwindows (Mainwnd, New CallBack (Delegate (INTPTR hwnd, int lParam) {LISTWND.A                DD (HWND);                StringBuilder className = new StringBuilder (126);                StringBuilder title = new StringBuilder (200);                GetWindowText (hwnd, title, 200);                RECT Clientrect;                GetClientRect (hwnd, out clientrect);                int controlwidth = Clientrect.width;                int controlheight = Clientrect.height;                int x = 0, y = 0;                IntPtr Parernthandle = GetParent (hwnd);                    if (parernthandle! = IntPtr.Zero) {GetWindowRect (hwnd, out clientrect);                    Rect rect;                    GetWindowRect (Parernthandle, out rect); x = Clientrect.x-rect.                    X y = clientrect.y-rect.                    Y                    Debug.Print (X.tostring ());                Debug.Print (Y.tostring ());     }           return true; }), 0);
Note that the X and Y in the code above are the exact x and Y values of a control, and we can get a precise coordinate value by recording it.    In the example above, the coordinates of our text box are finally "78,113". With this coordinate value, we know the handle of the control, that is, which control the HWND belongs to. 2.3: Get handle based on Enumchildwindows enumeration order if you don't want to be so troublesome, there is a simple solution, which is to take advantage of the Enumchildwindows enumeration order. You know, on different machines, the order in which Enumchildwindows enumerates the child controls on a form is the same, that is, if there are two text boxes, the order in which they are enumerated on this machine is 2, and one is 3, then the order in which they are enumerated on other machines is also this fixed order. By comparison, we can also get their respective handles. Of course, if we have these handles, what else can't be done? 2.4: Using Spy + +

Spy + + is a Microsoft tool that users get information such as the ID or type or handle on a form. Because in our case, the ID and handle are constant on every machine, so this tool is not very useful to us. However, when you hack someone else's program, it will play a role.

Rickie Software Test study notes-fifth week

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.