1、前言
在当今软件开发领域,质量保证是项目成功的关键因素之一。随着软件迭代速度不断加快,传统手动测试方法难以满足与日俱增的测试需求,因此,自动化测试工具的重要性愈发突出。
本篇将介绍一个自动化图形界面应用 - AutoKey,能够运行Python3脚本并实现文本扩展,特别注重宏和按键功能。
2、简介
AutoKey是一个具有GTK和Qt版本的Python3自动化应用程序。它可以进行文本扩展、运行宏和运行脚本,其中任何一个都可以用热键、组合键或键入的缩写来触发,它是完全可定制的,你可以使用它来自动化几乎任何你可以想到的计算机行为。
AutoKey可用于简单的文本扩展(即用静态文本替换缩写词)。在AutoKey中,这些替换文本被称为“短语”。AutoKey还可以响应键盘快捷键(例如[Ctrl]+[Alt]+F8)来扩展短语。为了提高灵活性,你可以在短语中使用宏来动态更改输入的内容。
如果简单的短语扩展不能满足你的需求,您可以发挥Python编程语言的全部功力,用Python 3编写脚本来自动化你的任务。AutoKey脚本可以像短语一样绑定到缩写和快捷键上,并执行你的命令。AutoKey提供了一个API与系统交互,可实现诸如鼠标点击或使用键盘输入文本等操作。
安装:
pip3 install autokey
更多安装可参考:
https://github.com/autokey/autokey/wiki/Installing
Github地址:
https://github.com/autokey/autokey
3、快速上手
1、将文本转换为小写,并用连字符替换空格。
如果你遵循“命名分支”Git开发的风格,这对于将GitHub Issue的名称转换为适合Git分支的字符串非常有用。
例如:FocusProxy返回为:-focusproxy
代码语言:javascript代码运行次数:0运行复制text = clipboard.get_selection()clipboard.fill_clipboard(text.lower().replace(' ', '-'))
2、获取当前平台信息。
可用于制作错误报告,特别是在平台和浏览器版本可能相关的web应用程序上。
例如:
Platform: Linux-4.13.0-37-generic-x86_64-with-LinuxMint-18.3-sylviaBrowser: Google Chrome 65.0.3325.181Browser: Mozilla Firefox 59.0.1
Date Tested :Wed 28 Mar 14:46:48 BST 2024
代码语言:javascript代码运行次数:0运行复制import platformoutput = ""output += "Platform: " + platform.platform() + "\n"output += "Browser: " + os.popen("google-chrome --version").read()output += "Browser: " + os.popen("firefox --version").read()output += "Date Tested :" + system.exec_command("date")keyboard.send_keys(output)
3、按以下格式插入当前日期时间(YYYY.MM.DD HH:MM:SS)。
例如:2024.10.13 23:47:00
代码语言:javascript代码运行次数:0运行复制from datetime import datetimekeyboard.send_keys(datetime.now().strftime('%Y.%m.%d %H:%M:%S'))
4、按以下格式插入当前时间(HH:MM:SS)。
例如:23:47:00
代码语言:javascript代码运行次数:0运行复制from datetime import datetimekeyboard.send_keys(datetime.now().strftime('%H:%M:%S'))
5、按以下格式插入当前日期(YYYY.MM.DD)。
例如:2024.10.13
代码语言:javascript代码运行次数:0运行复制from datetime import datetimekeyboard.send_keys(datetime.now().strftime('%Y.%m.%d'))
6、在浏览器中从剪贴板搜索文本。
代码语言:javascript代码运行次数:0运行复制import webbrowserimport timetime.sleep(0.1)webbrowser.open("http://www.google.de/search?q="+clipboard.get_clipboard())
7、打开某个网站。
代码语言:javascript代码运行次数:0运行复制import webbrowserimport timetime.sleep(0.1)site = "baidu.com"webbrowser.get('google-chrome').open_new_tab(site)# webbrowser.get('firefox').open_new(site) # in case you want to open a new site in firefox# webbrowser.get('firefox').open_new_tab(site)
8、获取当前鼠标坐标。
在弹出对话框中显示当前的X和Y鼠标坐标。
代码语言:javascript代码运行次数:0运行复制from Xlib import X, display # import the necessary classes from the specified moduled = display.Display().screen().root.query_pointer() # get pointer locationx = str(d.root_x) # get x coord and convert to stringy = str(d.root_y) # get y coord and convert to stringdialog.info_dialog("(X, Y)", x+", "+y) # create an info dialog to display the coordinates
9、使用键入或键入并单击输入的GUI对话框。
一种GUI对话框,使用键入或键入和单击输入的组合来启动程序或显示对话框。你可以自定义脚本以执行几乎任何你喜欢的操作。
代码语言:javascript代码运行次数:0运行复制################################################################ ABOUT THIS SCRIPT################################################################ A GUI information dialog will be displayed asking for input.# This script will act on your input.################################################################ USE THIS SCRIPT################################################################ Set a Hotkey for this script (example: Ctrl+K).# The script will activate when you press Ctrl and K.# When it prompts you for input, you can choose ONE of these: # Press the Esc key to cancel the dialog. # ... or ... # Click the Cancel button to cancel the dialog. # ... or ... # Press the Enter key to accept the default example text. # ... or ... # Click the OK button to accept the default example text. # ... or ... # Type in some text and press the Enter key. # ... or ... # Type in some text and click the OK button.################################################################ NOTES################################################################ When using subprocess, remember to use a comma wherever there is a space in a command.# The subprocess.call function is part of the older high-level API and has been replaced with the subprocess.run function.# The subprocess.call function waits for the process to complete and provides a return code with its exit status before allowing you to execute other code.# The subprocess.run function waits for the process to complete and provides a return code with its exit status before allowing you to execute other code.# The subprocess.Popen function allows you to execute other code and/or interact with the process with the subprocess.communicate function while the process is running.# Examples of some browsers that could have been used in the script: # subprocess.Popen(["C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"]) # subprocess.Popen(["chrome.exe"]) # subprocess.Popen(["chromium-browser"]) # subprocess.Popen(["google-chrome"]) # subprocess.Popen(["google-chrome-stable"])################################################################ THE SCRIPT################################################################ Message to display to prompt the user for input:prompt="Please type one of these commands:\n\t* chro\n\t* fire\n\t* goo\n\t* viv\n\t* apple\n\t* banana\n\t* coconut"# Ask for input, offer a default example, and check the exit code of the command:retCode, userInput = dialog.input_dialog(title="Input required", message=prompt, default="example")# If the command was successful (the exit code was zero), take the user's desired action:if retCode == 0: # If chro was typed, open Chromium: if userInput == "chro": program="chromium-browser" subprocess.Popen([program]) # If fire was typed, open Firefox: elif userInput == "fire": program="firefox" subprocess.Popen([program]) # If goo was typed, open Google Chrome: elif userInput == "goo": program="google-chrome" subprocess.Popen([program]) # If viv was typed, open Vivaldi: elif userInput == "viv": program="vivaldi" subprocess.Popen([program]) # If apple was typed, display a dialog: elif userInput == "apple": text="You chose apple." dialog.info_dialog(title='Info', message=text) # If banana was typed, display a dialog: elif userInput == "banana": text="You chose banana." dialog.info_dialog(title='Info', message=text) # If apple was typed, display a dialog: elif userInput == "coconut": text="You chose coconut." dialog.info_dialog(title='Info', message=text) # If anything else was typed or the default example string was accepted, display an invalid dialog: else: text = "You typed: " + userInput + "\n\nPlease enter a valid command." dialog.info_dialog(title="Invalid", message=text, width="200")# If the exit code was 1, display a cancel dialog:elif retCode == 1: text = "You pressed the Esc key or the Cancel button." dialog.info_dialog(title="Cancelled", message=text, width="200") # If the exit code was anything other than 0 or 1, display an error dialog:else: text = "Something went wrong." dialog.info_dialog(title="Error", message=text, width="200")
10、GUI日期对话框。
一个GUI日期选择对话框,等待用户选择日期,然后根据用户是取消/关闭窗口还是选择日期,使用对话框的返回代码显示两个不同对话框中的一个。日期的默认格式为YYYY-MM-DD。
代码语言:javascript代码运行次数:0运行复制# Create a variable for the return code and the date and put the chosen date's value into the date variable:retCode, date = dialog.calendar(title="Choose a date")# Use the following line instead if you'd like to control the format of the date:# retCode, date = dialog.calendar(title="Choose a date", format="%d-%m-%y")# If no date is chosen and the Cancel button is clicked, the Esc key is pressed, or the dialog window is closed:if retCode: # Create a message and display it in a dialog: myMessage = "No date was chosen." dialog.info_dialog(title="Cancelled", message=myMessage, width="200")else: # Display the value of the date variable in a dialog: dialog.info_dialog(title="The date you chose is:", message=date, width="200")
11、获取并打印剪贴板内容。
此脚本将剪贴板的内容(或剪贴板为空时的空字符串)放入变量中,并将变量的内容(如果不是空字符串)打印到当前活动的窗口中。
代码语言:javascript代码运行次数:0运行复制# Get the clipboard contents if the clipboard isn't empty:try: # Get the contents of the clipboard and assign them to a variable: cb = clipboard.get_clipboard() # (Optional) Brief delay to make sure the clipboard has been filled in case you just filled it: time.sleep(0.2)# Handle the exception if the clipboard is empty:except: # Assign an empty string value to the variable: cb = ""# Print the contents of the variable in the active window:keyboard.send_keys(cb)
12、等待鼠标点击。
你可以让脚本等待鼠标点击,如果没有收到鼠标点击,可以使用计时器在指定的延迟后执行操作,也可以不使用计时器,在这种情况下,只有在实际点击鼠标后才会执行操作。
例如:打印文本前等待左键单击。
代码语言:javascript代码运行次数:0运行复制mouse.wait_for_click(1)keyboard.send_keys("hello world")
例如:打印文本前等待中键单击。
代码语言:javascript代码运行次数:0运行复制mouse.wait_for_click(2)keyboard.send_keys("hello world")
例如:在打印文本之前等待左键单击,或者如果没有左键单击发生,则在计时器用完时打印文本。
代码语言:javascript代码运行次数:0运行复制mouse.wait_for_click(1, timeOut=3.0)keyboard.send_keys("hello world")
以上就是AutoKey - 适用于Linux和X11的桌面自动化应用程序的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号