Method for realizing stable file uploading in UI automation
By optimizing window lookup and exception handling, and combining it with a timeout handling mechanism, the stability issue of file uploads in UI automation testing was resolved. A stable upload method compatible with cross-browser was achieved, improving the success rate of file uploads and the reliability of testing.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-10
- Publication Date
- 2026-04-07
AI Technical Summary
In UI automation testing, file uploads often fail due to issues such as window search failures, unexpected pop-up blocking, and lack of timeout control, affecting the continuity and reliability of testing. In particular, traditional desktop automation scripts cannot operate the file selection dialog box due to system permission restrictions on macOS, and existing solutions lack cross-browser compatibility.
By optimizing window finding, operation flow, and exception handling mechanisms, implementing timeout handling in conjunction with decorator functions, encapsulating the FindWindow and FindWindowEx functions to add a retry mechanism, dynamically determining the browser window title, locating the file input box and simulating click operations, and handling abnormal pop-ups and timeouts, the stability of file uploads is ensured.
It significantly improves the stability and reliability of file uploads, adapts to the file upload window layout of various browsers, conforms to Python programming habits, is easy to develop and maintain, and has good compatibility and scalability.
Smart Images

Figure CN121814756A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a file upload method, specifically a method for achieving stable file uploads in UI automation, belonging to the field of UI automation technology. Background Technology
[0002] In UI automation testing scenarios, file upload functionality is a common test point. Traditional file upload methods, such as using Selenium's send_keys or autoit tools, have many limitations and are not "Pythonic" enough. Furthermore, in actual operation, various complex situations can affect the stability of file uploads, such as a non-existent window, an empty file address, a non-existent file, or an "not found" error. These situations can easily lead to upload failures, thus affecting the consistency and accuracy of the entire automated testing process.
[0003] In existing technologies, 1) a file upload and download method, device, and storage medium disclosed in CN116074306A addresses network instability through file fragmentation and MD5 verification, but its core focus is on the backend transmission protocol and fragmentation strategy; 2) a file upload method, device, and storage medium disclosed in CN115987965A involves improvements to the file upload component based on ElementUI, mainly targeting large file fragment uploads and progress display, adjusting fragment size to adapt to network conditions. These solutions do not fundamentally solve the problem of stable interaction between UI automation and graphical interface elements. Specifically, in automation scenarios, such as on Mac systems, due to system permission restrictions, traditional desktop automation scripts can hardly operate the file selection dialog box. Although uploads can be triggered by monitoring URL requests and protocol headers, this method is tightly coupled to specific Webkit kernel behaviors and lacks adaptive recognition and fault tolerance capabilities for common controls across various browser windows. Therefore, the file upload process in current UI automation is often interrupted due to problems such as window search failure, unexpected pop-up blocking, and lack of timeout control, which seriously affects the continuity and reliability of automated testing.
[0004] Therefore, there is an urgent need for a stable upload method that can comprehensively handle window lifecycle, abnormal pop-ups, and operation timeouts, and has good cross-browser compatibility. Summary of the Invention
[0005] The purpose of this invention is to provide a method for achieving stable file upload in UI automation in order to solve at least one of the above-mentioned technical problems. By removing traditional XPath expressions, optimizing window search, operation flow, and handling mechanisms for various abnormal situations, the success rate of file upload is improved, ensuring the stability and reliability of file upload function in UI automation testing.
[0006] This invention achieves the above objective through the following technical solution: a method for stable file upload in UI automation, the method comprising the following steps: Step 1: Initialization and window handling, obtain the title of the file upload window corresponding to the current browser; Step 2: Window check. Check if there is an open file upload window. If so, close the upload window. Step 3: File upload operation. Receive the file path and waiting time as parameters; check if the file path is empty; if empty, cancel the upload operation; handle any "file not found" error windows; locate the file input box and send the file path; locate the "open" button and simulate a click operation. Step 4: Timeout handling mechanism, using the decorator function overtime to implement timeout handling; Step 5: Optimize window search by encapsulating the native FindWindow and FindWindowEx functions and adding retry mechanisms and delay handling.
[0007] As a further aspect of the present invention: In step one, dynamically determining the window title based on the browser type includes: If using Chrome or Edge browser, the window title will be "Open"; If you are using the Firefox browser, the window title will be "File Upload".
[0008] As a further aspect of the present invention: in step two, the step of closing the upload window of the already opened file includes: Find the window with the class name "#32770" and the title that matches the browser window title; Locate the "Cancel" button in the window and simulate clicking it to close the window.
[0009] As a further aspect of the present invention: In step three, when receiving the path of the uploaded file and the waiting time as parameters, the main window for file upload is located. If the main window does not exist, the upload operation is terminated directly, and the corresponding prompt information is returned.
[0010] As a further aspect of the present invention: In step three, if the path of the uploaded file is empty and the upload operation is canceled, the currently open file upload window is closed and a prompt message indicating that the file name is empty is returned.
[0011] As a further solution of the present invention: In step three, when dealing with the "file not found" error window, if such a window appears at the beginning or after uploading the file, the window is located and closed by using the window search logic.
[0012] As a further aspect of the present invention: Step three, locating the file input box and sending the file path specifically includes: In the file upload window, locate the "ComboBoxEx32", "ComboBox", and "Edit" windows in sequence; Get the handle of the file input box, and then send the file path; Locate the "Open" button and simulate a click.
[0013] As a further aspect of the present invention: the step of locating the "open" button includes: In the file upload window, find the control with the word "Button" and the text "Open (&O)"; Obtain its handle and simulate a click operation.
[0014] As a further aspect of the present invention: in step four, the timeout handling specifically includes: Set a timeout period when the "Open" button is clicked. If the operation times out, terminate the thread and throw a TimeoutError exception. Provide appropriate prompts and follow-up actions during exception handling to ensure the program does not freeze due to prolonged waiting.
[0015] As a further aspect of the present invention: in step five, the window search optimization specifically includes: After a failed search, wait for the preset time and try again. Set the maximum number of retries until the search is successful or the number of retries is exhausted.
[0016] The beneficial effects of this invention are: 1) This invention has corresponding handling mechanisms for various possible abnormal situations, such as missing windows, file path problems, and missing files, which effectively avoids upload failures caused by these abnormalities and significantly improves the stability of file uploads. 2) This invention conforms to Python programming habits. Compared with traditional methods such as using Selenium and AutoIt, this method is closer to actual user operations and facilitates the development and maintenance of UI automation testing in the Python environment. 3) This invention is scalable and compatible. The method used has good compatibility with the file upload window layout of different browsers (such as Google Chrome, Firefox, etc.). At the same time, its modular design makes it easy to expand the functionality according to different needs in subsequent development. Attached Figure Description
[0017] Figure 1 This is a schematic diagram of the process flow of the present invention. Detailed Implementation
[0018] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0019] Example 1, as Figure 1 As shown, this embodiment provides a method for achieving stable file uploads in UI automation, which includes the following steps: First, initialize and handle the window, and obtain the title of the file upload window corresponding to the current browser.
[0020] Generally, the window title is dynamically determined by the browser type, and includes: If using Chrome or Edge browser, the window title will be "Open"; If you are using the Firefox browser, the window title will be "File Upload".
[0021] Second, check the windows to see if there are any open file upload windows. If so, close the upload window.
[0022] The specific steps to close the upload window for an open file include: Find the window with the class name "#32770" and the title that matches the browser window title; Locate the "Cancel" button in the window and simulate clicking it to close the window.
[0023] Third, the file upload operation receives the file path and waiting time as parameters; checks if the file path is empty, and cancels the upload operation if it is empty; handles possible "file not found" error windows; locates the file input box and sends the file path; locates the "open" button and simulates a click operation.
[0024] When receiving the path to the file to be uploaded and the waiting time as parameters, the system first locates the main window for file upload. If the main window does not exist, the upload operation is terminated directly, and the corresponding prompt message is returned.
[0025] If the path to the uploaded file is empty and the upload operation is canceled, close the currently open file upload window and return a message indicating that the file name is empty.
[0026] When dealing with potential "file not found" error windows, if such a window appears at the beginning or after uploading certain files, locate and close the window using specific window search logic.
[0027] Locating the file input box and sending the file path specifically includes: In the file upload window, look for windows with the names "ComboBoxEx32", "ComboBox", and "Edit" in sequence; Get the handle of the file input box, and then send the file path; Locate the "Open" button and simulate a click.
[0028] The steps to locate the "Open" button include: In the file upload window, find controls of type "Button" with the text "Open (&O)"; Obtain its handle and simulate a click operation.
[0029] Fourth, the timeout handling mechanism uses the decorator function overtime to implement timeout handling.
[0030] Timeout handling specifically includes: Set a timeout period when the critical action of clicking the "Open" button is performed. If the operation times out, terminate the thread and throw a TimeoutError exception. Provide appropriate prompts and follow-up actions during exception handling to ensure the program does not freeze due to prolonged waiting.
[0031] Fifth, window search optimization: the native FindWindow and FindWindowEx functions are encapsulated, and a retry mechanism and delay handling are added.
[0032] Window lookup optimization specifically includes: After a failed search, wait for the preset time and try again. Set the maximum number of retries until the search is successful or the number of retries is exhausted.
[0033] Example 2: This example provides a method for achieving stable file uploads in UI automation. The method is further explained in detail with specific code examples: import time import sys import wraps from functools import os import ctypes import threading import win32gui import win32con def overtime(timeout):
[0034] def _overtime(func): return wraps(func)(lambda *args, **kwargs: _overtime_(timeout, func,args=args, arg2=kwargs)) def _overtime_(_timeout, func, args=(), arg2=None): if not arg2: arg2 = {} if not args: args = () ret = [] exception = [] is_stopped = False def funcwrap(args2, kwargs2): try: ret.append(func(*args2, **kwargs2)) except TimeoutError: pass except Exception as e: exc_info = sys.exc_info() if is_stopped is False: e.__traceback__ = exc_info[2].tb_next exception.append(e) thread = StoppableThread(target=funcwrap, args=(args, arg2)) thread.daemon = True thread.start() thread.join(_timeout) if thread.is_alive(): is_stopped = True thread.stop_thread(TimeoutError) thread.join(min(.1, _timeout / 50.0)) raise TimeoutError('Out of %s seconds' % _timeout) else: thread.join(.5) if exception: raise exception[0] from None if ret: return ret[0] class StoppableThread(threading.Thread): def stop_thread(self, exception, raise_every=2.0): if self.is_alive() is False: return True self._stderr = open(os.devnull, 'w') jt = JoinThread(self, exception, repeat_every=raise_every) jt._stderr = self._stderr jt.start() jt._stderr = self._stderr class JoinThread(threading.Thread): def __init__(self, other_thread, exception, repeat_every=2.0) threading.Thread.__init__(self) self.otherThread = other_thread self.exception = exception self.repeatEvery = repeat_every self.daemon = True def run(self): self.otherThread._Thread__stderr = self._stderr while self.otherThread.is_alive(): ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(self.otherThread.ident), ctypes.py_object(self.exception)) self.otherThread.join(self.repeatEvery) try: self._stderr.close() except: ... return _overtime class _Win32: """
[0035] """ def __init__(self, win32gui, win32con): self.win32gui = win32gui self.win32con = win32con def find_window(self, class_, title, n: int = 5): """
[0036] """ time.sleep(0.2) while n: n -= 1 try: handle_id = self.win32gui.FindWindow(class_, title) except: handle_id = 0 if not handle_id: time.sleep(0.2) return self.find_window(class_, title, n) return handle_id def find_window_ex(self, dialog, m, class_, text, n: int = 5): """
[0037] """ time.sleep(0.2) while n: n -= 1 try: handle_id = self.win32gui.FindWindowEx(dialog, m, class_, text) except: handle_id = 0 if not handle_id: time.sleep(0.2) return self.find_window_ex(dialog, m, class_, text, n) return handle_id BROWSER = 'chrome' class upload: """
[0038] """ def __enter__(self): self.close_if_opened() return self def __exit__(self, exc_type, exc_val, exc_tb): ... def __init__(self): self.__title__ = self.__get_title() assert self.__title__ self.__win32gui = win32gui self.__win32con = win32con self.__win32 = _Win32(win32gui, win32con) def close_if_opened(self): """
[0039] ups.close_if_opened page.click(' / / button') ups.upload """ dialog = self._dialog(n=1) if dialog: return self.__cancel(dialog) def upping(self, file_path: str, timeout: float = 6): """
[0040] :param file_path: Address of the file to be uploaded # :param browser_type: Browser type :param timeout: Waiting time """ # This object can be reused in the 'Open' window and the 'File Not Found' window. dialog = self._dialog() If not dialog: print("The 'Open' window does not exist") return False # This indicates there is a problem with the click; setting it to false means that the external trigger should occur again.
[0041] if not file_path: print("File name is empty, upload canceled") self.__cancel(dialog) return True # The file name is empty, it's not a click issue, no external trigger is needed.
[0042] self.__occur_no_exist(n=1) # If a 'not found' window appears initially / after uploading certain files, and you need to close this window, I will fill in this regardless of whether it appears or not, so there is no need to create an object to receive it. return self.__fill_and_open(file_path, timeout - 1) @staticmethod def __get_title(): brs = BROWSER.lower() title = "Open" if brs in ["chrome", 'edge'] else "File Upload" if brs == "firefox" else False return title; if title else print("We recommend using Google Chrome") # Print returns None def _dialog(self, n=2):
[0043] dialog = self.__win32.find_window("#32770", self.__title__, n=n) return dialog if dialog else False def __cancel(self, dialog):
[0044] self.__win32gui.SendMessage(cancel, self.__win32con.WM_LBUTTONDOWN,0, 0) self.__win32gui.SendMessage(cancel, self.__win32con.WM_LBUTTONUP, 0,0) return False def __occur_no_exist(self, n=3): """
[0045] """ # Except for self.__title__, other layouts are the same in Chrome and Firefox new_dialog = self.__win32.find_window("#32770", self.__title__, n=n) sure1 = self.__win32.find_window_ex(new_dialog, 0, 'DirectUIHWND',None, n=n) if not sure1: return False sure2 = self.__win32.find_window_ex(sure1, 0, 'CtrlNotifySink', None,n=n) sure3 = self.__win32.find_window_ex(sure2, 0, 'Button', 'OK') self.__win32gui.SendMessage(new_dialog, self.__win32con.WM_COMMAND,1, sure3) return True def __fill_and_open(self, file_path, delay):
[0046] dialog = self.__win32.find_window("#32770", self.__title__) edit_out2 = self.__win32.find_window_ex(dialog, 0, "ComboBoxEx32",None) edit_out1 = self.__win32.find_window_ex(edit_out2, 0, "ComboBox",None) edit = self.__win32.find_window_ex(edit_out1, 0, "Edit", None) # Send file path self.__win32gui.SendMessage(edit, self.__win32con.WM_SETTEXT, None,file_path) time.sleep(0.2) # Locate 'Open', layout is consistent in Chrome and Firefox. open_button = self.__win32.find_window_ex(dialog, 0, 'Button', "Open(&O)") # Click to open @overtime(1) def _click_open(): self.__win32gui.SendMessage(dialog, self.__win32con.WM_COMMAND, 1,open_button) try: click_open() except TimeoutError: print("File does not exist, clicking the open button timed out") self.__occur_no_exist() self.__cancel(dialog) return False # Check if a 'file not found' window has appeared. if self.__occur_no_exist(): self.__cancel(dialog) return False else: if delay >= 2: delay = delay – 2 time.sleep(delay) return True
[0047] with upload() as ups: result = ups.upping('C: / test / file.txt', timeout = 6) if result: print("File upload successful") else: print("File upload failed") Example 3: This example is based on the method in Example 1. It can be installed via pip, which greatly simplifies the implementation process.
[0048] Installation command: pip install uploads After installing uploads, assume that the upload window is already open in the UI automation test.
[0049] The calling code is the same as in Example 2: with upload() as ups: result = ups.upping('C: / test / file.txt', timeout = 6) if result: print("File upload successful") else: print("File upload failed") Through the above two specific implementation methods, this invention elaborates on the method of achieving stable file upload in UI automation. From initialization and exception handling to the specific upload operation process, it comprehensively demonstrates the implementation of this method in practical applications, effectively improving the stability and reliability of file upload.
[0050] Workflow: First, the current browser type is identified and the corresponding upload window title is obtained, such as "Open" for Chrome and Edge, and "File Upload" for Firefox. During the initialization phase, any existing legacy upload windows are actively detected and closed to avoid interference from the operating environment. Then, the main upload process is entered, receiving the file path parameter and performing multiple checks, including determining whether the main window exists and whether the file path is valid. Through the encapsulated and optimized FindWindow and FindWindowEx functions, a retry mechanism is used to cyclically locate the main window with the class name "#32770" and its internal hierarchical controls, including "ComboBoxEx32", "ComboBox", and "Edit", to fill in the file path into the input box. Finally, the "Open" button is located and clicked to complete the upload. For the "File Not Found" system pop-up that may occur during the execution process, a special capture and closing logic is designed to eliminate the abnormal state by finding a specific button and simulating clicking "OK".
[0051] It will be apparent to those skilled in the art that the present invention is not limited to the details of the exemplary embodiments described above, and that the invention can be implemented in other specific forms without departing from its spirit or essential characteristics. Therefore, the embodiments should be considered in all respects as exemplary and non-limiting, and the scope of the invention is defined by the appended claims rather than the foregoing description. Thus, all variations falling within the meaning and scope of equivalents of the claims are intended to be included within the present invention. No reference numerals in the claims should be construed as limiting the scope of the claims.
[0052] Furthermore, it should be understood that although this specification describes embodiments, not every embodiment contains only one independent technical solution. This narrative style is merely for clarity. Those skilled in the art should consider the specification as a whole, and the technical solutions in each embodiment can also be appropriately combined to form other embodiments that can be understood by those skilled in the art.
Claims
1. A method for achieving stable file uploads in UI automation, characterized in that, The method includes the following steps: Step 1: Initialization and window handling, obtain the title of the file upload window corresponding to the current browser; Step 2: Window check. Check if there is an open file upload window. If so, close the upload window. Step 3: File upload operation. Receive the file path and waiting time as parameters; check if the file path is empty; if empty, cancel the upload operation; handle any "file not found" error windows; locate the file input box and send the file path; locate the "Open" button and simulate a click operation. Step 4: Timeout handling mechanism, using the decorator function overtime to implement timeout handling; Step 5: Optimize window search by encapsulating the native FindWindow and FindWindowEx functions and adding retry mechanisms and delay handling.
2. The method according to claim 1, characterized in that: In step one, dynamically determining the window title based on the browser type includes: If using Chrome or Edge browser, the window title will be "Open"; If you are using the Firefox browser, the window title will be "File Upload".
3. The method according to claim 1, characterized in that: Step two, the step of closing the upload window of the opened file, includes: Find windows with the class name "#32770" and a title that matches the browser window title; Locate the "Cancel" button in the window and simulate clicking it to close the window.
4. The method according to claim 1, characterized in that: In step three, when receiving the path of the uploaded file and the waiting time as parameters, the main window for file upload is located. If the main window does not exist, the upload operation is terminated directly, and the corresponding prompt information is returned.
5. The method according to claim 1, characterized in that: In step three, if the path to the uploaded file is empty and the upload operation is canceled, the currently open file upload window is closed, and a message indicating that the file name is empty is returned.
6. The method according to claim 1, characterized in that: In step three, when dealing with the "file not found" error window, if such a window appears at the beginning or after uploading the file, locate and close the window using the window search logic.
7. The method according to claim 1, characterized in that: Step three, locating the file input box and sending the file path, specifically includes: In the file upload window, locate the "ComboBoxEx32", "ComboBox", and "Edit" windows in sequence; Get the handle of the file input box, and then send the file path; Locate the "Open" button and simulate a click.
8. The method according to claim 7, characterized in that: The steps for locating the "Open" button include: In the file upload window, find the control with the word "Button" and the text "Open (&O)"; Obtain its handle and simulate a click operation.
9. The method according to claim 1, characterized in that: Step four, specifically the timeout handling, includes: Set a timeout period when the "Open" button is clicked. If the operation times out, terminate the thread and throw a TimeoutError exception. Provide appropriate prompts and follow-up actions during exception handling to ensure the program does not freeze due to prolonged waiting.
10. The method according to claim 1, characterized in that: Step five, window lookup optimization, specifically includes: After a failed search, wait for the preset time and try again. Set the maximum number of retries until the search is successful or the number of retries is exhausted.
Citation Information
Patent Citations
File uploading method and device, equipment and storage medium
CN115987965A
File uploading and downloading method and device and storage medium
CN116074306A