Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work with menuStrip and its items in python

I have to build an automation ui 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 menu of the application and click on one of the sub menu 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, the menuStrip1 is discovered, but I have not found a way to get to its children (Meniu 1, Meniu 2).

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

like image 346
CristisS Avatar asked Feb 04 '26 07:02

CristisS


2 Answers

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

eg:

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

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

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

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

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

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

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

pywinauto.mouse.wheel_click(coords=(0, 0)) //Middle mouse button click at the specified coords  
like image 149
Vineesh V Avatar answered Feb 06 '26 21:02

Vineesh V


After trying without success to use python 3.4 with python for windows extension and pywinauto to create automated tests for the respective project. I looked into other tools, and finally chose one called Sikuli, it's an automation tool based on image recognition of the 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 on controls they are aware of (e.g. not toolstripmenuitem)
  • it's impossible to test custom controls (for the reason above)

Observations regarding Sikuli:

  • all scripts are written in python
  • it may not use the latest python syntax
  • at the moment I used it, it had some small bugs
like image 29
CristisS Avatar answered Feb 06 '26 20:02

CristisS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!