Working with menuStrip and its elements in python

I need to create an automation test for a WinForms application. I am using python 3.4 with python for windows extension and pywinauto.

The test is required to access the application menu and click on one of the submenu items.

I used the code below to try and find the menu.

#arrays to store the controls found
classes = []
objects = []

#recursive method to get all the controls
def getClasses(childHwnd, lparam):
    objects.append(childHwnd)
    classes.append(win32gui.GetWindowText(childHwnd))
    return 1

#find handle of the main window
hwnd = win32gui.FindWindow(None, 'Form1')

#get all controls
win32gui.EnumChildWindows(hwnd, getClasses, "a")

# Result:    
# objects [1509794, 3344468] 
# classes ['Test', 'menuStrip1']

#trying to get the menu of the form
win32gui.GetMenu(hwnd) #Returns 0

      

Image of the form on which I tested the code above:

Image of the form used for the tests above

As you can see, menuStrip1 opens, but I haven't found a way to get to its children (Meniu 1, Meniu 2).

Any idea on how to find the menu and its children?

+3


source to share


3 answers


After trying without success to use python 3.4 with python to extend windows and pywinauto to generate automated tests for the project in question. I looked at other tools and finally chose one from Sikul i, which is an image-based automation tool for recognizing elements on the screen. It may not be the perfect tool for the job, but it was enough for me.

Other observations about python for Windows extension and pywinauto:

  • can only be used for controls they know about (not toolstripmenuitem for example)
  • unable to test custom controls (for the reason above)


Observations of Sikuli :

  • all scripts are written in python
  • it cannot use the latest python syntax
  • The moment I used it it had small errors
+1


source


Although the answer has already been posted for an alternative solution, I would like to add a workaround that can be used in pywinauto.

In windows, the ALT key can be used to access menus. In my specific example, I had the option "Settings -> Login remotely". If I wanted to click this menu item, I would first have to send ALT + S and then type L. To do this using pywinauto, if you already have a link to the window, you can do it like this:

myWindow.SetFocus()
myWindow.TypeKeys("%s") #Clicks Settings
myWindow.TypeKeys("l") #Clicks Login Remotely

      



The "%" character is used to represent the "ALT" key on the keyboard. Another note is that if you have multiple menu options starting with the same letter, you may need to send the key more than once to press the one you want and then send the enter key. If there is no ambiguity (in this case with only one submenu starting with L), it was automatically selected.

This requires you to be very familiar with your menu and in what order, but as long as it hasn't changed, it should work for you.

0


source


In the case of Menustrip, an easy workaround is to use pywinauto's basic input modules. using this you can jump to a specific position of the screen using (x, y) coordinates.

eg: pywinauto.mouse.click (button = 'left', coords = (0, 0)) // Click on the specified coordinates

pywinauto.mouse.double_click (button = 'left', coords = (0, 0)) // Double click on the specified coordinates

pywinauto.mouse.move (coords = (0, 0)) // Move The Mouse

pywinauto.mouse.press (button = 'left', coords = (0, 0)) // Click the mouse button

pywinauto.mouse.release (button = 'left', coords = (0, 0)) // Release the mouse button

pywinauto.mouse.right_click (coords = (0, 0)) // Right click on the specified coordinates

pywinauto.mouse.scroll (coords = (0, 0), wheel_dist = 1) // Mouse wheel

pywinauto.mouse.wheel_click (coords = (0, 0)) // The middle mouse button is pressed on the specified coordinates

0


source







All Articles