Python automated testing series [v1.0.0] [upload file]

Uploading attachments is a function we often encounter when testing BS systems. However, the automation code for handling uploading attachments is not always effective. Therefore, you need to master a variety of uploading attachments. In this section, I will introduce several uploading attachments The method should meet the vast majority of situations.

Tested page

<html>
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
		<title>上传文件</title>
	</head>
	<body>
		<div class="row-fluid">
			<div class="span6 well">
			<h3>选择文件</h3>
			<input type="file" name="fileupload" />
			</div>
		</div>
	</body>
</html>

Test code

def test_upload_by_sendkeys(self):
    chrome_driver = webdriver.Chrome()
    chrome_driver.get("file:///C:/Users/Administrator/Desktop/fileupload.html")
    chrome_driver.find_element_by_name("fileupload").send_keys("E:\\test_upload_file.txt")
    time.sleep(10)
    chrome_driver.quit()

Upload with AutoIt

If the page tag is not of input type, you can use a third-party tool to complete the upload operation.

  • First download the AutoIt tool in the first step, and the browser visits https://www.autoitscript.com/files/autoit3/autoit-v3-setup.exe to download it directly. Double-click the autoit-v3-setup.exe file after download , The default option can be installed, after the installation is complete, you can see the relevant menu items in the start menu of the operating system
    Insert picture description here
  • Open the fileupload.html file created in the previous section with a browser, and then click "Select File" in the opened page. The window for selecting a file will pop up
    Insert picture description here
  • Click AutoIt Window Info in the start menu. There are two versions of the program (x86) for the 32-bit version and (x64) for the 64-bit version. Reader friends can start the corresponding AutoIt version according to their operating system version.
    Insert picture description here
  • There are several tabs in the middle part of the AutoIt Window Info window, and then drag the Finder Tool to the "Open" button to get the window information of the control
    Insert picture description here
  • Start SciTE Script Editor, you can find him in the path of StartIt AutoIt v3
    Insert picture description here
  • Write a script, write the following content in SciTE Script Editor, and then press the F5 key on the keyboard in the SciTE Script Editor window when the file selection window opens, execute the script, the script runs normally, and you can save it under our PO project In the Util path, named upload_file. After saving successfully, an upload_file.au3 file will be generated
; ControlFocus("title", "text", "ClassnameNN") ControlFocus函数的用法
ControlFocus("打开", "", "Edit1")
; 等待10秒
 WinWait("[CLASS:#32770]", "", 10)
; 在文件名控件里设置要上传的文件全路径
 ControlSetText("打开", "", "Edit1", "E:\test_upload_file.txt")
 Sleep(2000)
; 点击打开按钮
 ControlClick("打开", "", "Button1")
  • However, this upload_file.au3 file cannot be executed by Python. You need to compile it into an .exe file for Python to call. Start Compile Script to .exe. You can find it in the Auto v3 path of the start menu. ) You can choose according to your operating system version
    Insert picture description here
  • Select the previously saved au3 file and click the Convert button to convert it to an .exe file
    Insert picture description here
  • The Python script calls the .exe to complete the file upload
import os  # 引入os模块用于调用.exe文件执行
def test_upload_by_autoit(self):  # 定义测试方法
    chrome_driver = webdriver.Chrome()  # 启动浏览器
	#打开我们的html文件
    chrome_driver.get("file:///C:/Users/Administrator/Desktop/fileupload.html")
	chrome_driver.find_element_by_name("fileupload").click()
    os.system("E:\\PO\\Util\\upload_file.exe")  # 调用我们编译好的.exe文件
    time.sleep(10)  # 强制等待10秒  
    chrome_driver.quit()

Simulate keyboard for upload

Packaging operation shear plate method

# encoding = utf-8
import win32clipboard as wc
import win32con
class Simulate_Clipboard:
    # 读取剪切板
    @staticmethod
    def get_clipboard():
        # 打开剪切板
        wc.OpenClipboard()
        # 获取剪切板中的数据
        data = wc.GetClipboardData(win32con.CF_TEXT)
        # 关闭剪切板
        wc.CloseClipboard()
        # 返回剪切板数据给调用者
        return data
    # 设置剪切板内容
    @staticmethod
    def set_clipboard(content):
        # 打开剪切板
        wc.OpenClipboard()
        # 清空剪切板
        wc.EmptyClipboard()
        # 将数据astring写入剪切板
        wc.SetClipboardData(win32con.CF_UNICODETEXT, content)
        # 关闭剪切板
        wc.CloseClipboard()

Method call

# 将模拟剪切板的类引入到测试代码文件中
from Util.Clipboard_Simulation import Simulate_Clipboard  
def test_simulate_clipboard(self):  # 定义测试方法
    Simulate_Clipboard.set_clipboard("set clipboard")  # 设置剪切板内容
    str = Simulate_Clipboard.get_clipboard()  # 获取剪切板内容并赋给str
    print(str)  # 将剪切板内容打印到控制台

Cutting board with keyboard to achieve upload

def test_upload_by_simulation(self):  # 定义测试方法 
	# 设置剪切板内容,将文件全路径放到剪切板中
    Simulate_Clipboard.set_clipboard("E:\\test_upload_file.txt")      
	chrome_driver = webdriver.Chrome()  # 启动浏览器
	# 打开我们的html文件
    chrome_driver.get("file:///C:/Users/Administrator/Desktop/fileupload.html")
	chrome_driver.find_element_by_name("fileupload").click()
	time.sleep(5)
    Simulate_Keyboard.click_twokey('ctrl', 'v')  # 模拟键盘Ctrl+V组合键,黏贴剪切板内容
    time.sleep(5)
    Simulate_Keyboard.click_onekey('enter')  # 模拟键盘回车键
    time.sleep(20)
Published 231 original articles · praised 188 · 120,000 views

Guess you like

Origin blog.csdn.net/dawei_yang000000/article/details/105648658