使用自動化腳本工具pyautogui在postman/接口文檔 等實現參數名快速填寫

痛點:一個接口十幾個甚至幾十個業務字段 使用postman進行測試,或者寫接口文檔時,在填寫這些字段名時就要耗費很久並且很弱智的複製粘貼

解決方法:使用 pyautogui工具 省去重複性工作

平臺:macbook pro 16-inch

注意點:

  • 在使用windows時,快捷鍵 和 macos不同;
  • 運行腳本時,輸入法應該爲英文
  • 運行腳本時,儘量不要動 鼠標鍵盤
  • 腳本中的 鼠標開始點擊的位置 按照各自屏幕大小進行修改
  • 腳本每個函數的入參 使用空格分開,因爲 我使用了Alfred的追加複製功能

 

效果圖如下:

postman如下(爲了圖片大小限制,有些模糊):

接口文檔(用的 https://kl3s.w.eolinker.com/ ) 變量名 效果圖(因爲動圖過大,改爲只循環一次,請刷新查看):

接口文檔 參數的說明 效果圖(因爲動圖過大,改爲只循環一次,請刷新查看):

 

安裝方式:

pip install pyautogui

 

腳本如下: 

# -*- coding: utf-8 -*-
"""
All rights reserved
create time '2020/4/28 14:56'

Module usage:
注意點:輸入法爲英文
"""
import time

import pyautogui
import pyperclip

pyautogui.FAILSAFE = True
# 函數每次執行後暫停的時間,因爲可能存在 程序的變化(如postman)跟不上函數的執行速度 出現問題
pyautogui.PAUSE = 0.1


def get_mouse_position():
    while True:
        mouse_x, mouse_y = pyautogui.position()
        print(mouse_x, mouse_y)
        time.sleep(1)


def check_chinese(_str):
    """
    檢查是否存在中文
    :param _str:
    :return:
    """
    import re
    RE = re.compile(u'[\u4e00-\u9fa5]', re.UNICODE)
    match = re.search(RE, _str)
    if match is None:
        pyautogui.typewrite(message=f'{_str}')
    else:
        paste(_str)


def paste(_str):
    """
    對於中文進行 複製粘貼
    :param _str:
    :return:
    """
    pyperclip.copy(_str)
    pyautogui.hotkey('command', 'v')


def str2list(_str):
    """
    字符串轉爲list
    :param _str:
    :return:
    """
    return _str.split(' ')


def postman_fill(_str):
    '''
    postman輸入 http請求 body 參數名
    :param _str:
    :return:
    '''
    # 獲取需要輸入的str list
    _list = str2list(_str)
    # 鼠標移動到指定位置並左擊
    pyautogui.click(320, 376, button='left', duration=0.1)
    pyautogui.click(button='left')
    for item in _list:
        print(f'輸入:{item}')
        check_chinese(item)
        for tab_item in range(3):
            pyautogui.press('tab')


def api_doc_val_name_fill(_str):
    '''
    api文檔輸入參數名
    :param _str:
    :return:
    '''
    # 獲取需要輸入的str list
    _list = str2list(_str)
    # 鼠標移動到指定位置並左擊
    pyautogui.click(376, 906, button='left', duration=0.1)
    pyautogui.click(button='left')
    for item in _list:
        print(f'輸入:{item}')
        check_chinese(item)
        for tab_item in range(7):
            pyautogui.press('tab')


def api_doc_val_note_fill(_str):
    '''
    api文檔輸入參數的說明
    :param _str:
    :return:
    '''
    # 獲取需要輸入的str list
    _list = str2list(_str)
    # 鼠標移動到指定位置並左擊
    pyautogui.click(1061, 906, button='left', duration=0.1)
    pyautogui.click(button='left')
    for item in _list:
        print(f'輸入:{item}')
        check_chinese(item)
        for tab_item in range(7):
            pyautogui.press('tab')


if __name__ == '__main__':
    # postman輸入 http請求 body 參數名
    postman_fill('a b c 你好 d')

    # api文檔輸入參數名
    # api_doc_val_name_fill('a b c')

    # api文檔輸入參數的說明
    # api_doc_val_note_fill('a1 b1 c1')

    # 獲取鼠標當前位置
    # get_mouse_position()

 

相關鏈接:

https://github.com/asweigart/pyautogui

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章