Ui自动化测试上传文件方法都在这里了
2019-07-18 21:55
1716 查看
前言
实施UI自动化测试的时候,经常会遇见上传文件的操作,那么对于上传文件你知道几种方法呢?今天我们就总结一下几种常用的上传文件的方法,并分析一下每个方法的优点和缺点以及哪种方法效率,稳定性更高
被测HTML代码
<!--upload_file.html--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <input id="upload_file" type="file"> </body> </html>
第一种方法:使用webdriver api 提供的方法send_keys上传文件
send_keys方法
实例
------------------------------------ @Time : 2019/7/17 19:03 @Auth : linux超 @File : upload_file_send_keys.py @IDE : PyCharm @Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error! @QQ : 28174043@qq.com @GROUP: 878565760 ------------------------------------ """ from selenium import webdriver import time driver = webdriver.Firefox() driver.get(r'file:///e:/CnblogCode/upload_file.html') upload_element = driver.find_element_by_xpath('//*[@id="upload_file"]') time.sleep(3) # 为了看效果 upload_element.send_keys(r'E:\CnblogCode\upload_file.html')
执行过程
""" ------------------------------------ @Time : 2019/7/18 17:14 @Auth : linux超 @File : keyboard.py @IDE : PyCharm @Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error! @QQ : 28174043@qq.com @GROUP: 878565760 ------------------------------------ """ import win32api import win32con import time class KeyBoard(object): """模拟按键""" # 键盘码 vk_code = { 'enter' : 0x0D, 'ctrl' : 0x11, 'v' : 0x56 } @staticmethod def keyDown(key_name): """按下键""" key_name = key_name.lower() try: win32api.keybd_event(KeyBoard.vk_code[key_name], 0, 0, 0) except Exception as e: print('未按下enter键') raise e @staticmethod def keyUp(key_name): """抬起键""" key_name = key_name.lower() win32api.keybd_event(KeyBoard.vk_code[key_name], 0, win32con.KEYEVENTF_KEYUP, 0) @staticmethod def oneKey(key): """模拟单个按键""" key = key.lower() KeyBoard.keyDown(key) time.sleep(2) KeyBoard.keyUp(key) @staticmethod def twoKeys(key1, key2): """模拟组合按键""" key1 = key1.lower() key2 = key2.lower() KeyBoard.keyDown(key1) KeyBoard.keyDown(key2) KeyBoard.keyUp(key1) KeyBoard.keyUp(key2) if __name__ == '__main__': passkeyboard.py 剪切板操作代码
""" ------------------------------------ @Time : 2019/7/18 17:18 @Auth : linux超 @File : clipboard.py @IDE : PyCharm @Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error! @QQ : 28174043@qq.com @GROUP: 878565760 ------------------------------------ """ import win32con import win32clipboard as WC class ClipBoard(object): """设置剪切板内容和获取剪切板内容""" @staticmethod def getText(): """获取剪切板的内容""" WC.OpenClipboard() value = WC.GetClipboardData(win32con.CF_TEXT) WC.CloseClipboard() return value @staticmethod def setText(value): """设置剪切板的内容""" WC.OpenClipboard() WC.EmptyClipboard() WC.SetClipboardData(win32con.CF_UNICODETEXT, value) WC.CloseClipboard() if __name__ == '__main__': passclipboard.py
上传文件代码
""" ------------------------------------ @Time : 2019/7/18 17:20 @Auth : linux超 @File : upload_file_board.py @IDE : PyCharm @Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error! @QQ : 28174043@qq.com @GROUP: 878565760 ------------------------------------ """ from clipboard import ClipBoard from keyboard import KeyBoard from selenium import webdriver import time from selenium.webdriver.common.action_chains import ActionChains if __name__ == '__main__': ClipBoard.setText(r'E:\CnblogCode\upload_file.html') # 待上传文件设置到剪切板 driver = webdriver.Firefox() driver.get(r'file:///e:/CnblogCode/upload_file.html') upload_element = driver.find_element_by_xpath('//*[@id="upload_file"]') action = ActionChains(driver) action.move_to_element(upload_element).click().perform() action.release() time.sleep(3) # 为了看效果 KeyBoard.twoKeys('ctrl', 'v') KeyBoard.oneKey('enter')upload_file_board.py
执行过程
""" ------------------------------------ @Time : 2019/7/17 19:14 @Auth : linux超 @File : upload_file_winspy.py @IDE : PyCharm @Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error! @QQ : 28174043@qq.com @GROUP: 878565760 ------------------------------------ """ from selenium.webdriver.common.action_chains import ActionChains from selenium import webdriver import win32con import win32gui import time def upload_file(browser: str, file: str): browser_type = { "firefox": "文件上传", "chrome": "打开", "ie": "选择要加载的文件" } time.sleep(2) dialog = win32gui.FindWindow("#32770", browser_type[browser]) # 火狐浏览器为”文件上传“,谷歌为”打开“ combobox_ex32 = win32gui.FindWindowEx(dialog, 0, "ComboBoxEx32", None) combobox = win32gui.FindWindowEx(combobox_ex32, 0, 'ComboBox', None) edit = win32gui.FindWindowEx(combobox, 0, 'Edit', None) button = win32gui.FindWindowEx(dialog, 0, 'Button', None) win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file) win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button) if __name__ == '__main__': driver = webdriver.Firefox() driver.get(r'file:///e:/CnblogCode/upload_file.html') upload_element = driver.find_element_by_xpath('//*[@id="upload_file"]') action = ActionChains(driver) action.move_to_element(upload_element).click().perform() action.release() time.sleep(3) # 为了看效果 upload_file('firefox', r'E:\CnblogCode\upload_file.html')load_file_winspy.py
执行过程
此方法也比较稳定,只要通过传递不同的浏览器参数,即可完成不同浏览器中的上传文件操作,而且相比于AutoId而言,这种方法可以直接使用python编写代码且不需要编译成exe程序,可以随时对代码进行修改,易维护
总结
最后总结一下每个方法的优缺点
1. send_keys(): 如果所遇见的上传文件使用的是input标签且属性type=file,均可使用,也最好采用此方法,方便,简单,执行效率高
2. AutoId: 需编写VBS代码并编译为exe可执行文件,过程繁琐,编写selenium测试用例时无法随时调试内部代码,但send_keys()无法使用时,可以采用此方法,执行效率较高,稳定性也算可以
3. 模拟键盘和剪切板: 此方法需要掌win32api,win32gui模块的使用,且代码编写繁琐,上传文件时如果鼠标点击其他地方会导致上传失败,脚本稳定性差,且效率低,但是如果用到一些特殊地方会起到比较好的效果
4. WinSpay:windows窗口句柄抓取工具,不需要编译脚本为exe文件,且上传文件操作使用的脚本是python第三方库实现,易维护,执行效率也较高,推荐send_keys()不可使用时,首先考虑此方法
相关文章推荐
- ftp上传下载文件、解压方法
- php上传文件大小限制解决方法
- spring mvc RestFul风格PUT方法修改上传文件
- 简单的文件上传方法
- SELinux限制导致VSFTP匿名用户不能上传文件问题解决方法
- php实现SAE上使用storage上传与下载文件的方法
- 笔记:上传文件时遮罩使用方法
- 封装ThinkPHP的一个文件上传方法实例
- uploadify文件上传方法
- Python模拟浏览器上传文件脚本的方法(Multipart/form-data格式)
- ASP.NET FileUpload上传图片方法并解决上传文件大小
- wordpress上传文件自动重命名的方法-原创-避免中文乱码
- 通过SSH向安装在虚拟机上的centOS 上传文件出现的问题及解决方法
- ASP.NET中文件上传下载方法集合
- org.apache.commons.fileupload上传临时文件删除方法2则
- 使用ASP实现文件上载(上传)的各种方法
- 动态input file多文件上传到后台没反应的解决方法!!!
- JS中使用FormData上传文件、图片的方法
- Java通过FTP服务器上传下载文件的方法
- 文件上传接收方法