Automated test programming learning summary

At present automated testing of contact There are two main libraries: 1, selenium 2, win32gui

1, selenium: for operating a browser, access to web information

 

2, win32gui, win32api, win32con plus get a handle tools (spy ++, etc.) used to operate non-browser software, but not all controls all software can get get a handle

For those buttons does not recognize the handle, it is currently used in a way I find the entire control of the handle, and then find the coordinates of this control through the print control coordinates, and then the way win32gui simulate a mouse click operation.

def mouse_left_click(button):  #鼠标移动到元素位置,点击鼠标左键
    left, top, right, bottom = win32gui.GetWindowRect(button) #获取按钮句柄的坐标
    #print(left,top,right,bottom)
    win32api.SetCursorPos((left+5,top+5)) #设置鼠标位置
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)  #按住鼠标左键
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)  #释放鼠标左键

button to get the handle in the following ways:

button=win32gui.FindWindow('Button',u'开始')

Where 'Button' for the class name, 'start' is the title (Untitled compared None), can be viewed in the spy ++.

If the window has a parent, the window button below TrayNotifyWnd

Then get the handle using the following method:

    dialog1=win32gui.FindWindow('Shell_TrayWnd',None)
    dialog2=win32gui.FindWindowEx(dialog1,0,'TrayNotifyWnd',None)

If there are more of the same window, you need to use the following function to get:

# 已知子窗口的窗体类名,窗口名,寻找第index号个同类型的兄弟窗口
def find_idxSubHandle(pHandle, winClass, index):
    #    assert type(index)==int and index>=0       #assert断言,错误时报错
    handle = win32gui.FindWindowEx(pHandle, 0, winClass, None)
    while index >= 0:
        handle = win32gui.FindWindowEx(pHandle, handle, winClass, None)
        index -= 1
    #print('%x' % handle)
    return handle

Which pHandle parent window handle, winClass for the class name, index number for the need to find a window, the window may not be allowed numbers, you can print ( '% x'% handle) handle print to see if the handle is needed, if not the modification index.

 

For those who can not use spy ++ text box to identify the handle, the current method I use is to call windows api text box shots were OCR (image character recognition) use pytesseract.

import win32gui,win32con,win32api,win32ui
def window_capture(filename): #窗口截图
  dialog = win32gui.FindWindow('VanDyke Software - SecureCRT', None)  # 对话框
  child_dialog = win32gui.FindWindowEx(dialog, 0, 'AfxFrameOrView80u', None)



  #left, top, right, bottom = win32gui.GetWindowRect(child_dialog ) #获取按钮句柄的坐标
  #print(left, top, right, bottom)

  hwnd = child_dialog # 窗口的编号,0号表示当前活跃窗口
  # 根据窗口句柄获取窗口的设备上下文DC(Divice Context)
  hwndDC = win32gui.GetWindowDC(hwnd)
  # 根据窗口的DC获取mfcDC
  mfcDC = win32ui.CreateDCFromHandle(hwndDC)
  # mfcDC创建可兼容的DC
  saveDC = mfcDC.CreateCompatibleDC()
  # 创建bigmap准备保存图片
  saveBitMap = win32ui.CreateBitmap()
  # 获取监控器信息
  MoniterDev = win32api.EnumDisplayMonitors(None, None)
  w = MoniterDev[0][2][2]
  h = MoniterDev[0][2][3]
  # print w,h   #图片大小
  # 为bitmap开辟空间
  saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
  # 高度saveDC,将截图保存到saveBitmap中
  saveDC.SelectObject(saveBitMap)
  # 截取从左上角(0,0)长宽为(w,h)的图片
  saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY)
  saveBitMap.SaveBitmapFile(saveDC, filename)

The above function is used to control the interception of the picture and save the picture.

import pytesseract
text = pytesseract.image_to_string(Image.open(r'.\picture.jpg'))

Function is used to identify the picture above the text. Before you need to use pytesseract

Download tesseract: https: //digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w64-setup-v4.0.0-beta.1.20180414.exe, and install directories to an environment variable, and then pip install pytesseract installation.

Published 24 original articles · won praise 30 · views 50000 +

Guess you like

Origin blog.csdn.net/yufen9987/article/details/87880049