C # _ Automatic Test 2 _ plug-ins

Source: Internet
Author: User
Tags visual studio 2010

Gui automated testing is similar to plug-ins. It simulates users' mouse and keyboard operations. Writing automation to your own programs is a test, and writing automation to other programs is a plug-in.

The technology used in this article is also applicable to plug-ins for games such as "Confrontation" and "finding fault.

 

Reading directory

  1. Implementation principle of QQ continuous view plug-in
  2. Principles of automated GUI Testing
  3. What is a handle?
  4. P/invoke Mechanism
  5. Get the handle of the game form
  6. Analysis game window
  7. Perform game windows
  8. Write Algorithms Based on Game Rules
  9. Simulate mouse clicks
  10. Source code download

 

Implementation principle of QQ continuous view plug-in

1. Call the Win32 API to obtain the handle of the game window of "Connected View,

2. Obtain the pixels in the game square based on the handle of the game window.

3. Use a two-dimensional array to save the pixels of each square

4. Use an algorithm to determine whether two identical squares can be "eliminated". If the two squares can be "eliminated", click the two squares with a simulated mouse. Continue to remove the next group of squares.

 

Principles of automated GUI Testing

When you click a button in the form, the button responds and then performs some operations. The essence of this process is: you click a button on the screen, and the windows system knows which button you want to click based on the position you click, and then sends the mouse click message to this button.

The principle of automation is to find the control handle and send messages to the control through the handle, such as "keyboard input" message or "mouse click" message.

What is a handle?

All Windows controls are essentially a window ). each control/form has a handle associated with it, which can be used to access, manipulate, and detect the control/form.

A form handle is a value generated by the system. You can think of it as an ID associated with the form. With this ID, you can access the corresponding form.

In. net, the handle type is system. intptr, which is similar to int type.

 

P/invoke Mechanism

The P/invoke mechanism is called the "platform call" mechanism. Because the WIN32API function is a part of the Windows operating system, it is written using a traditional C ++ program, instead of using C # To host code. Therefore, we need a mechanism for C # To Call Win32 API functions.

The specific solution is: first create a C # overwriting function for the Win32 function to be used, or call an alias function, and then call this alias function.

 

Instance:

In Win32 API, The findwindow () function is used to obtain the form handle. Its function signature is described in C ++.

HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName);

In C #, create an alias function for this Win32 Function

You must first reference the namespace: using system. runtime. interopservices;

[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Dllimport: Specifies the DLL file of the function to be used.

Entrypoint: name of the Win32 API function.

Charset. Auto: Let the. NET Framework decide how to convert Character Types

 

When you compile the corresponding C # method alias for a Win32 function, static and extern are almost always used, because most Win32 functions are static functions, rather than Instance functions.

Note: The names of Alias functions and Win32 functions are different. However, consistent names can ensure code readability.

 

Get the handle of the game form

We use the Win32 API function to obtain the handle of the game Form for "continuous view ".

Now we call the alias function findwindow () in C # described in the previous section, which is equivalent to calling the findwindow () function of Win32 API.

The findwindow function receives two parameters, classname or windowname, and then returns the handle.

 

Spy ++ is a built-in. Net tool. We can use it to obtain the form name.

Specifically, start spy ++ and click the "findow window" icon. After the findow window program is displayed, drag the "target" to the form you want to test.

For example. You can get the name of the game form "QQ game-connected Player version"

In this way, we can easily obtain the handle of the game form.

Intptr wndpane = WIN32API. findwindow (null, "QQ game-Player version ");

 

Analysis game window

Using the screen ruler tool, we can measure the game window. (This is complicated. You need to make multiple measurements and make multiple adjustments to obtain accurate data ).

You can find that the horizontal direction of the game window is 15 pixels away from the "game area" and the vertical direction is 182 pixels.

In the game, there are 11 vertical blocks and 19 horizontal blocks.

The length of each square is 31 pixels, and the width is 35 pixels, as shown in figure

 

 

 

Perform all blocks in the game window

A square has 31*35 = 1085 pixels. In fact, we do not need to obtain all the pixels in the square. To save performance, I only need to get a few pixels in a square.

We need two functions to obtain the pixel of the square.

 

[DllImport("user32.dll")]public static extern IntPtr GetDC(IntPtr hwnd);[DllImport("Gdi32.dll")]public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

 

The getdc function is used to specify the client area of the window or the handle of the display context of the entire screen. Its input parameter is the handle of the window, (the last section describes how to use the findwindow function to obtain the window handle ). the DC handle is returned. (Note that the two handles are different)

Getpixel obtains the pixel Based on the DC handle and X coordinate obtained by getdc and Y coordinate.

 

Example: We get the pixel of the square in the "third row and fourth column" of the game. The Code is as follows:

Intptr wndpane = WIN32API. findwindow (null, "QQ game-serialization role version"); intptr HDC = WIN32API. getdc (wndpane); // The pixels in the X axis direction are calculated as 15 + 31*3. // because the game area is 15 pixels away from the left side of the game window, each square is 31 pixels wide, // The pixels in the y-axis direction are calculated as 182 + 35*4 // because the game area is 182 above the game window, each square is 35 pixels high. uint color = WIN32API. getpixel (HDC, 15 + 31*3 + offx, 182 + 35*4 + offy );

 

Write Algorithms Based on Game Rules

We use a two-dimensional array to save all squares in the game.

Private block [,] blocks = new block [11, 19];

The block object represents a square. If the square is empty, the block contains the background color. If there is a square, the block object stores the nine pixels of the square.

For details, see block objects in the code.

 

Then, we analyze game rules to write algorithms to traverse two-dimensional arrays.

Vertical. If two blocks are in the same Y axis, there is no square in the middle,

 

Horizontal Direction. If two squares are on the same X axis and there is no square in the middle, they can be eliminated,

 

Turn one bend. If two of the same squares, one of the X axis and the other y is 90 degrees, and there is no square in the middle, you can remove it,

 

Turn 2 corners,

Design algorithms based on the above game rules. For specific algorithms, refer to the source code.

There are many ways to simulate mouse clicks. One of the Win32 API methods is
[Dllimport ("user32.dll")] public static extern int mouse_event (INT dwflags, int dx, int dy, int cbuttons, int dwextrainfo); const int mouseeventf_move = 0x0001; // move the mouse public const int mouseeventf_leftdown = 0x0002; // press the public const int mouseeventf_leftup = 0x0004 with the left mouse button; // simulate left mouse button lifting const int mouseeventf_rightdown = 0x0008; // simulate right mouse clicking const int mouseeventf_rightup = 0x0010; // simulate right-clicking and raising the const int mouseeventf_middledown = 0x0020; // simulate the middle mouse button and pressing the const int mouseeventf_middleup = 0x0040; // lift the const int mouseeventf_absolute = 0x8000 in the middle mouse of the simulation; // indicates whether absolute coordinates are used.

 

To download the source code, click here to download the source code. Use Visual Studio 2010 to open the source code.

Appendix: automated test series tutorials (coming soon)

Automated Testing (I) 12306 automated logon tool for train ticket websites

Automated Testing (ii) plug-ins for continuous view

Automated Testing (iii) Web automated testing principles

Automated Testing (4) Automatic Software Uninstall

Automated test (5) read and write the registry of a 64-bit Operating System

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.