Automated Testing (ii) plug-ins for continuous view

Source: Internet
Author: User

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.

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.