前端自动化测试之pywinauto实战对notepad++进行操作【初学者】

  1. 打开notepad++
  2. 文件另存为为:xxx.txt
  3. 对xxx.txt进行键入及保存
  4. 退出notepad++
import pywinauto
from pprint import *
from pywinauto.keyboard import send_keys
import time



def pywinauto_creatfile():
    app = pywinauto.Application(backend='uia').connect(process=8836)
    pprint(app.windows())
    dlg = app.window(title='*新文件 1 - Notepad++')
    dlg.print_control_identifiers()
    menu = dlg.child_window(title="应用程序", auto_id="MenuBar", control_type="MenuBar")
    menu.print_control_identifiers()
    menu.child_window(title="文件(F)", control_type="MenuItem").click_input()
    menu.item_by_path('文件(F)->新建(N)').click_input()


def pywinauto_connectapp(pid, addr, handleid):
    """
    与app建立连接的三种方式:
    通过进程id连接
    通过句柄连接
    通过安装路径连接
    :return: none
    """
    if pid:
        app = pywinauto.Application(backend='uia').connect(process=pid)
    elif addr:
        app = pywinauto.Application(backend='uia').start(addr)
    elif handleid:
        app = pywinauto.Application(backend='uia').connect(handle=handleid)
    else:
        app = None
        print("[ERROR]: The key_words of connection is wrong!")
    return app

def pywinauto_wfile(addr):
    """
    操作文件:在notepad++中键入文字并保存
    bug:多次打开未关闭会引发异常
    :return:
    """
    app = pywinauto.Application(backend='uia').start(addr)
    pprint(app)
    dlg = app['新文件 1 - Notepad++']
    # dlg.print_control_identifiers()
    menu = dlg.child_window(title="应用程序", auto_id="MenuBar", control_type="MenuBar")
    # menu.print_control_identifiers()
    file = menu.child_window(title="文件(F)", control_type="MenuItem")
    # file.print_control_identifiers()
    file.click_input()
    menu.item_by_path('文件(F)->另存为(A)...').click_input()
    save_as = dlg['另存为']
    # save_as.print_control_identifiers()
    # 文件命名为pywinauto_test保存
    save_name = "pywinauto_test"
    save_as.child_window(title="文件名:", auto_id="1001", control_type="Edit").type_keys(save_name)
    save_as.child_window(title="保存(S)", auto_id="1", control_type="Button").click_input()
    check_save_as = dlg['确认另存为']
    check_save_as.child_window(title="是(Y)", auto_id="CommandButton_6", control_type="Button").click_input()
    time.sleep(5)
    new_win = app[r'C:\Users\ruanjin\PycharmProjects\pythonProject\pywinauto_test.txt - Notepad++']
    # new_win.child_window(title="pywinauto_test.txt", control_type="TabItem").type_keys("hello world!!")
    new_win.child_window(title="pywinauto_test.txt", control_type="TabItem")
    new_win.print_control_identifiers()
    send_keys("hello world!!")
    new_menu = new_win.child_window(title="应用程序", auto_id="MenuBar", control_type="MenuBar")
    new_menu.child_window(title="文件(F)", control_type="MenuItem").click_input()
    new_menu.item_by_path('文件(F)->保存(S)').click_input()
    time.sleep(4)
    new_win.close()

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    pid = 30064
    addr = r"D:\下载安装\Notepad++\notepad++.exe"
    handleid = None
    #pywinauto_creatfile()
    #app = pywinauto_connectapp(pid, addr, handleid)
    #pprint(app)
    pywinauto_wfile(addr)

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值