A Self-Stabilizing Method and System for Web Test Scripts Based on Element Awareness and Layered Decoupling

CN122570355APending Publication Date: 2026-08-14SHAANXI FAST GEAR CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-06-10
Publication Date
2026-08-14

AI Technical Summary

Technical Problem

[0009]本发明的目的是解决现有Web应用的自动化测试脚本因元素定位机制脆弱、缺乏异步加载检测机制及智能异常处理能力,导致页面变更后脚本维护成本高、执行稳定性差以及缺乏智能容错能力的技术问题,而提供基于元素感知与分层解耦的Web测试脚本自稳定方法及系统

Benefits of technology

[0057]1、本发明基于元素感知与分层解耦的Web测试脚本自稳定方法,通过双循环遍历Web应用的所有页面的所有目标元素,并针对每个目标元素采用“获取地址、存储、多策略定位、加载检测、状态记录、元素测试”的闭环自稳定流程,实现了Web应用的测试脚本的自动化修复与持续稳定执行。当页面元素发生变更时,能够自动通过多策略定位、主动轮询和分层重试机制进行自适应调整,无需人工干预即可恢复脚本执行,显著降低了因页面变动或动态加载导致的脚本维护成本,将脚本维护耗时从传统方案的60%-70%大幅降低。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122570355A_ABST
    Figure CN122570355A_ABST
Patent Text Reader

Abstract

This invention relates to a method and system for stabilizing Web test scripts, specifically a self-stabilizing method and system for Web test scripts based on element awareness and layered decoupling. This addresses the problems of high maintenance costs and poor execution stability in existing Web automated test scripts due to fragile element location paths, lack of asynchronous loading detection mechanisms, and insufficient intelligent exception handling capabilities, resulting in instability after page changes. This invention generates test scripts based on a multi-layered isolation architecture, including a test case layer, an action layer, a location layer, and a driver layer. When executing the test script, a dynamic Web element address acquisition mechanism is used to obtain element addresses and convert them into location expressions stored in the location layer. Based on the multi-layered isolation architecture, the target element location process is initiated, attempting location according to various location strategies in sequence. Upon successful location, a dynamic loading completion detection mechanism oriented towards element identifiers is used to determine whether the target element has been loaded completely. If location or detection fails, an intelligent exception handling mechanism is used to perform layered retry.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to a method and system for stabilizing web test scripts, specifically to a self-stabilizing method and system for web test scripts based on element awareness and layered decoupling. Background Technology

[0002] With the widespread adoption of agile development and continuous delivery models for Web (World Wide Web) applications, automated testing has become a crucial means of ensuring software quality. Currently, frameworks based on Selenium + pytest / RobotFramework are widely used for functional testing, regression testing, and cross-platform compatibility testing of Web applications.

[0003] However, existing automated test scripts for web applications generally suffer from high maintenance costs and poor sensitivity to page changes. Maintaining test scripts often accounts for 60% to 70% of the total investment in automated testing, and in some application scenarios, it even requires manpower comparable to that of creating new scripts to fix location failures and operational interruptions caused by page changes.

[0004] To reduce script maintenance costs, existing technologies have proposed layered script design methods. For example, Chinese patent CN106354649A discloses a layered webpage automated test script design method. This method divides the script into a webpage element index layer, a webpage element positioning encapsulation layer, an element basic operation encapsulation layer, and a test step writing layer. By modularizing the process, the coupling of the script design is reduced, thereby lowering maintenance costs.

[0005] However, the above-mentioned layered script design method still has the following technical shortcomings in practical applications:

[0006] First, the element positioning mechanism is fragile. Although this method centrally stores element positioning expressions in the index layer, its positioning strategy still relies on a single static expression, such as ID (element identifier), XPath (XML Path Language), or CSS (Cascading Style Sheets). When the attributes of page elements undergo subtle changes (such as dynamically generated IDs, adjusted CSS class names, or XPath paths becoming invalid due to minor DOM structure adjustments), the expressions in the positioning layer will directly become invalid, causing the test script to break. Therefore, this method lacks dynamic fault tolerance for positioning paths and cannot automatically adapt to common element changes, still requiring testers to manually locate and update the expressions.

[0007] Secondly, there is a lack of adaptive handling for asynchronous loading and rendering delays. Modern web applications widely use technologies such as AJAX and asynchronous rendering, resulting in unpredictable page element loading times. The aforementioned comparison documents do not provide an effective mechanism for detecting the completion of dynamically loaded elements. In actual execution, scripts often fail to execute because elements have not yet finished loading, or require manually inserting fixed waiting times, leading to low testing efficiency and instability.

[0008] Third, the exception handling mechanism is insufficient. This comparison document only focuses on the normal execution path of the script and does not provide a systematic retry and recovery strategy for abnormal scenarios such as location failures and timeouts. Once a step fails due to network fluctuations or a brief rendering delay, the entire test case fails, lacking intelligent fault tolerance. Summary of the Invention

[0009] The purpose of this invention is to solve the technical problems of existing automated test scripts for web applications, which suffer from high maintenance costs, poor execution stability, and lack of intelligent fault tolerance after page changes due to the fragility of element location mechanisms, lack of asynchronous loading detection mechanisms, and lack of intelligent exception handling capabilities. The invention provides a self-stabilizing method and system for web test scripts based on element awareness and layered decoupling.

[0010] To achieve the above objectives, the technical solution provided by this invention is as follows:

[0011] A self-stabilizing method for web test scripts based on element awareness and layered decoupling, characterized by the following steps:

[0012] S1. Construct test scripts for testing the Web application; the Web application includes M pages, each page includes N target elements, where M≥1, N≥1;

[0013] S2. Establish a communication connection between the test script built in step S1 and the web application;

[0014] S3. For the m-th page, obtain the element address and corresponding business action unit of all target elements on the page, and convert each element address into an element location expression; store the element location expression and the corresponding business action unit in the corresponding position of the test script to update the test script; where m is the page number, m=1, 2, ..., M;

[0015] S4. Based on the element positioning expression stored at the corresponding position in the updated test script, locate the nth target element in the current page in sequence according to the positioning strategies of ID, CSS, XPath and Text; where n is the index of the target element, n=1, 2, ..., N;

[0016] If any positioning strategy successfully locates the current target element, proceed to step S5;

[0017] If all positioning strategies fail, an intelligent anomaly handling mechanism is used to attempt multiple repositioning attempts until the preset attempt limit is reached. If the current target element still cannot be located, the target element is determined to not exist, the anomaly status is recorded, and step S7 is executed.

[0018] S5. After locating the current target element, determine whether the current target element has been loaded. If yes, proceed to step S6. If no, retry multiple times until the preset retry limit is reached. If the current target element is still not loaded, determine that the target element is abnormal, record the abnormal status and proceed to step S7.

[0019] S6. Test the current target element based on the corresponding business action unit, obtain the test report of the target element, and execute step S7.

[0020] S7. Determine if there are any unprocessed target elements on the current page; if yes, set n=n+1 and return to step S4 to locate and load the next target element on the current page; if no, proceed to step S8.

[0021] S8. Determine if there are any unprocessed pages. If yes, set m = m + 1 and return to step S3. If no, output all abnormal states and test reports to complete the self-stabilization of the Web test script based on element awareness and layer decoupling.

[0022] Furthermore, in step S1, the test script includes a test case layer, an AW layer, a location layer, and a driver layer;

[0023] The use case layer contains test cases, which include test steps, expected results, and corresponding judgment rules; the positioning layer is used to obtain and store the element positioning expressions of each target element in the page after the page structure is updated; the AW layer is used to obtain and store the business action units formed by the page operation after the page business process is changed; the driver layer is used to encapsulate the basic operations for low-level interaction with the Web application.

[0024] The test case layer interacts with the AW layer, and is used to call the business action unit in the AW layer and receive the test results returned by the AW layer. Based on the judgment rules, the test case is judged whether it passes the test case and the expected results in the AW layer.

[0025] The AW layer interacts with the driver layer to call basic operations within the driver layer and receive test results returned by the driver layer to verify whether the business action unit has been executed successfully; the AW layer also interacts with the positioning layer to reference element positioning expressions stored within the positioning layer.

[0026] The driver layer communicates with the web application and is used to perform tests on the web application based on basic operations, and return test results to verify whether the basic operations were executed successfully.

[0027] Furthermore, step S3 specifically includes:

[0028] For the m-th page, a dynamic web element address acquisition mechanism is used to obtain the element addresses of all target elements on the page, and all element addresses are converted into element location expressions. At the same time, the business action units corresponding to all target elements are obtained. Then, the element location expressions and the corresponding business action units are stored in the location layer and AW layer of the test script respectively to update the test script. Here, m is the page number, m=1,2,...,M.

[0029] Furthermore, step S4 specifically includes:

[0030] S4.1 The AW layer in the test script is based on the element positioning expression stored in the positioning layer in the updated test script. It uses the driver layer to locate the nth target element in the current page in sequence according to the positioning strategies of ID, CSS, XPath and Text; where n is the index of the target element, n=1, 2, ..., N;

[0031] S4.2 Determine whether the ID of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.3.

[0032] S4.3 Determine whether the CSS of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.4.

[0033] S4.4 Determine whether the XPath of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.5.

[0034] S4.5 Determine whether the Text of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.6.

[0035] S4.6. Using an intelligent exception handling mechanism, return to step S4.1 to attempt to relocate the current target element multiple times until the preset attempt limit is reached. If the current target element still cannot be located, it is determined that the target element does not exist, the exception status is recorded, and step S7 is executed.

[0036] Furthermore, step S5 specifically includes:

[0037] S5.1 After locating the current target element, a dynamic loading completion detection mechanism oriented towards element identifiers is adopted to determine whether the current target element has been loaded through active polling; if yes, proceed to step S6; if no, proceed to step S5.2.

[0038] S5.2. Using a multiple retry mechanism, return to step S5.1 and retry multiple times to reload the current target element until the preset retry limit is reached. If the current target element is still not loaded, the target element is determined to be abnormal, the abnormal status is recorded, and step S7 is executed.

[0039] Furthermore, step S6 specifically includes:

[0040] S6.1 The test case layer of the test script is based on the test steps of the test case and calls the corresponding business action unit stored in the AW layer; the AW layer responds to the call of the test case layer, references the element positioning expression stored in the positioning layer, and calls the basic operation encapsulated in the driver layer to test the current target element and obtain the corresponding test result.

[0041] S6.2 The driver layer uses the obtained test results to verify whether the basic operation it executes is successful and records it to obtain the basic operation verification result; then, the test result and the basic operation verification result are returned to the AW layer.

[0042] S6.3 The AW layer verifies whether the business action unit executed by it is successfully executed based on the received test results and records it to obtain the business action unit verification result; then, the test results, basic operation verification results and business action unit verification results are returned to the test case layer.

[0043] S6.4 The test case layer compares the received test results with the expected results stored in it using its internal decision rules to determine whether the test case is successful and records the result.

[0044] S6.5. Based on the basic operation verification results returned by the driver layer, the business action unit verification results returned by the AW layer, and the judgment results, the use case layer generates a test report for the current target element and executes step S7.

[0045] Furthermore, in step S5.1, the dynamic loading completion detection mechanism oriented towards element identifiers includes:

[0046] When the initial existence verification of the target element fails, an exponential backoff retry strategy is automatically triggered, dynamically adjusting the interval between subsequent checks.

[0047] Furthermore, in step S3, the mechanism for dynamically obtaining Web element addresses includes a gradient positioning mechanism, a dynamic path positioning mechanism, and a fuzzy matching mechanism.

[0048] The gradient positioning mechanism attempts to obtain the element address of the target element in the order of its ID, CSS, XPath, and Text positioning strategies.

[0049] The dynamic path positioning mechanism is used to process dynamic lists or target elements with dynamic indexes. It determines the list length or locates the target element by parameterizing the index variable in the positioning expression and cyclically incrementing the index value until the target element is detected to be non-existent.

[0050] The fuzzy matching mechanism is used to locate a target element using multiple attributes simultaneously, and the path expression for locating the element contains a function that performs partial matching on the element class name, text content, or attributes.

[0051] Furthermore, in step S4, the preset maximum number of attempts is 10; starting from the 3rd attempt, the time interval between each attempt is greater than the time interval between the previous attempts;

[0052] In step S5, the preset retry limit is 3 times; the time interval of the third retry is greater than the time interval of the second retry.

[0053] Meanwhile, the present invention also provides a self-stabilizing system for Web test scripts based on element awareness and hierarchical decoupling, which is characterized by including: a processor and a storage device;

[0054] The storage device is used to store computer programs;

[0055] When the computer program is executed by the processor, the processor implements the above-described self-stabilizing method for Web test scripts based on element awareness and hierarchical decoupling.

[0056] Compared with the prior art, the present invention has the following beneficial technical effects:

[0057] 1. This invention is based on a web test script self-stabilization method using element awareness and layered decoupling. It iterates through all target elements of all pages in a web application using a double loop, and employs a closed-loop self-stabilization process for each target element: address acquisition, storage, multi-strategy location, load detection, state recording, and element testing. This achieves automated repair and continuous stable execution of web application test scripts. When page elements change, the method automatically adapts through multi-strategy location, proactive polling, and layered retry mechanisms, restoring script execution without manual intervention. This significantly reduces script maintenance costs caused by page changes or dynamic loading, drastically reducing script maintenance time from 60%-70% of traditional solutions.

[0058] 2. This invention employs a self-stabilizing method for Web test scripts based on element awareness and layered decoupling. The test script is divided into a test case layer, an action layer, a location layer, and a driver layer, clearly defining the interaction and calling relationships between each layer. This achieves strict decoupling of business logic, action encapsulation, element location, and underlying interaction. Changes to the page structure only require modification of the location layer, and changes to the page's business process only require modification of the action layer, while the test case layer remains stable, significantly improving the maintainability and scalability of the script. Simultaneously, a dynamic element address acquisition mechanism captures the element addresses of target elements on the page in real time and converts them into element location expressions, storing them in the location layer. The corresponding business action units for the target elements are also obtained and stored in the action layer. This allows the test script to automatically adapt to changes in the page structure, completing script updates without manual intervention and enhancing the script's adaptability to dynamic pages.

[0059] 3. This invention employs a self-stabilizing method for web test scripts based on element awareness and layered decoupling. It sequentially locates elements using ID, CSS, XPath, and Text in that order, and triggers an intelligent exception handling mechanism to re-locate elements multiple times after all strategies fail, until a preset attempt limit is reached. This ensures both efficient location (prioritizing the most stable ID) and robust fault tolerance (subsequent strategies and multiple retries), effectively solving the script interruption problem caused by the fragility of a single location path. Furthermore, after successful location, a loading completion check is performed; if loading is incomplete, multiple retries are made to ensure the element is truly interactive. This mechanism avoids erroneous operations such as "element located but not loaded" due to asynchronous rendering or network latency, improving the stability and reliability of test execution.

[0060] 4. The present invention is a self-stabilizing method for Web test scripts based on element awareness and layered decoupling. When the target element fails to be verified for the first time, it automatically adopts an exponential backoff strategy to dynamically adjust the subsequent detection interval. This avoids the waste of system resources caused by high-frequency polling and provides sufficient waiting time for slow rendering or network latency scenarios. It achieves a balance between efficiency and reliability and effectively solves the problems of "insufficient waiting" or "redundant waiting" in the traditional fixed interval waiting mechanism.

[0061] 5. This invention is based on a self-stabilizing method for Web test scripts that is element-aware and layered decoupling. Through the coordinated operation of three mechanisms—gradient localization (multi-path fault tolerance), dynamic path localization (adaptive dynamic list), and fuzzy matching (tolerance for subtle attribute changes)—the test scripts can cope with more than 90% of common page change scenarios, significantly reducing the workload of script repair and improving the robustness of the scripts.

[0062] 6. This invention employs a self-stabilizing method for Web test scripts based on element awareness and layered decoupling. It limits the upper limit of location attempts (10 times) and loading retry attempts (3 times), and specifies that the interval between each attempt increases incrementally. This ensures sufficient fault tolerance while avoiding infinite waiting or excessively frequent attempts, providing quantifiable fault tolerance parameters for easier engineering implementation and performance tuning. Furthermore, it clarifies that the AW layer and the location layer independently obtain the business action unit and element location expression after a page update, and the test cases include complete test steps, assertions, and preconditions and postconditions. This allows page changes and business process changes to respond separately and without mutual interference, further reducing maintenance coupling and improving the maintainability of the test suite.

[0063] 7. This invention relates to a self-stabilizing Web test script system based on element awareness and layered decoupling. Through the cooperation of the processor and storage device, it achieves automated execution of a self-stabilizing method for Web application test scripts based on element awareness and layered decoupling. This system can be directly integrated into existing CI / CD pipelines, providing stable and reliable automated testing support for continuous testing and agile development, and has broad application prospects and commercial value. Attached Figure Description

[0064] Figure 1 This is a flowchart illustrating an embodiment of the self-stabilizing method for Web test scripts based on element awareness and layered decoupling of the present invention.

[0065] Figure 2 This is a schematic diagram of the test script and the structure for establishing a communication connection with the web application in step S1 of the embodiment of the self-stabilizing method for web test scripts based on element awareness and hierarchical decoupling of the present invention. Detailed Implementation

[0066] To make the objectives, advantages, and features of this invention clearer, the following detailed description of the self-stabilizing method and system for Web test scripts based on element awareness and layered decoupling, in conjunction with the accompanying drawings and specific embodiments, is provided. Those skilled in the art should understand that these embodiments are merely illustrative of the technical principles of this invention and are not intended to limit the scope of protection of this invention.

[0067] This embodiment provides a self-stabilizing method for web test scripts based on element awareness and layered decoupling. This method achieves adaptive fault tolerance of the test script to page changes in web applications through the coordinated work of a multi-layered isolation architecture, dynamic element address acquisition, gradient localization, dynamic loading detection, and intelligent exception handling mechanisms. Figure 1 As shown, the specific steps include the following:

[0068] S1. Build the test script:

[0069] Construct test scripts for testing a web application; the web application consists of M pages, each page containing N target elements, where M ≥ 1 and N ≥ 1.

[0070] like Figure 2 As shown, the test script includes a Test Case Layer, an Action Word Layer, a Locator Layer, and a Driver Layer.

[0071] The use case layer is used for pure business logic description. It contains test cases, which include test steps, expected results (i.e., assertions), and corresponding judgment rules (i.e., preconditions and postconditions). For example: opening a specified URL, entering search conditions, clicking the query button, and asserting whether the returned results meet expectations. The use case layer does not involve any element location or underlying operation details.

[0072] The positioning layer is used to retrieve and store the element positioning expressions for each target element on the page after the page structure is updated. The positioning layer stores various positioning information for each target element in key-value pairs, including ID positioning values, CSS selector expressions, XPath paths, and text fragments. When the Action Layout (AW) layer executes actions, it retrieves the specific positioning expression by referencing the predefined keys in the positioning layer.

[0073] The Action Execution (AW) layer is used to retrieve and store one or more business action units formed by page operations after changes to the page's business process; such as login, logout, and adding devices to the list. The AW layer provides callable business-level interfaces to the use case layer, and each action unit internally calls the location layer and the driver layer to complete the specific operation.

[0074] The driver layer encapsulates the basic operations for low-level interaction with web applications. Examples include clicking elements, sending text to input fields, retrieving element text content, and waiting for elements to appear. The driver layer directly calls the underlying APIs provided by Selenium WebDriver and encapsulates exception handling logic internally.

[0075] The test case layer interacts with the action layer (AW) layer to invoke business action units within the AW layer and receive test results returned by the AW layer. Based on the expected results and decision rules, the AW layer determines whether the test case passes. The AW layer interacts with the driver layer to invoke basic operations within the driver layer and receive test results returned by the driver layer to verify whether the business action unit executes successfully. The AW layer interacts with the location layer to reference element location expressions stored within the location layer. The driver layer communicates with the web application to execute tests on the web application based on basic operations and receives test results to verify whether the basic operations execute successfully.

[0076] The AW layer obtains the business action unit of the page after a page update (e.g., when the business operation process changes); the positioning layer obtains the element positioning expression of each target element on the page after a page update (e.g., when the DOM structure or element attributes of the page change).

[0077] S2. Establish a communication connection between the test script built in step S1 and the web application.

[0078] S3. Obtain the element location expression and the corresponding business action unit:

[0079] For the m-th page, obtain the element addresses and corresponding business action units of all target elements on that page (obtain all addresses at once to speed up subsequent processing), and convert each element address into an element location expression; store the element location expressions and corresponding business action units in the corresponding positions of the test script to update the test script; where m is the page number, m=1, 2, ..., M; specifically:

[0080] For the m-th page, a dynamic web element address acquisition mechanism is used to obtain the element addresses of all target elements on the page, and all element addresses are converted into element location expressions. At the same time, the business action units corresponding to all target elements are obtained. Then, the element location expressions and the corresponding business action units are stored in the location layer and AW layer of the test script respectively to update the test script. Here, m is the page number, m=1,2,...,M.

[0081] The Web element address is a string / path that precisely locates a target element on a webpage. It's used in front-end development, automated testing, web scraping, and other scenarios, and is categorized into ID (most stable), CSS (flexible), XPath (versatile), and Text (i.e., TextContent, or text positioning). The target element is the page element that needs to be selected, manipulated, located, or interacted with within the web application's page operations, code, or automated scripts. The target element's address is obtained through a dynamic Web element address acquisition mechanism, which includes the following three core sub-mechanisms:

[0082] (1) Gradient localization mechanism

[0083] The gradient positioning mechanism attempts to obtain the element address of the target element in the following order: ID, CSS, XPath, and Text (i.e., from highest to lowest positioning stability). Specifically, for the same target element, four types of positioning expressions are pre-defined. At runtime, the system first attempts to use the ID; if successful, it directly returns the target element. If an exception is thrown indicating the target element does not exist, it automatically switches to CSS; this process continues until a positioning strategy succeeds or all strategies fail. This mechanism requires no manual intervention and automatically implements multi-path fault tolerance.

[0084] (2) Dynamic path positioning mechanism

[0085] The dynamic path locator mechanism is used to handle dynamic lists or target elements with dynamic indexes (such as the Nth item in a product list or the Nth tab in a tab). It determines the list length or locates the target element by parameterizing the index variable in the locator expression and iteratively incrementing the index value until the target element is detected as non-existent. The mechanism parameterizes the index number in the locator expression (e.g., the XPath template ` / / div[@class='item'][{index}]`), then iteratively increments the index starting from 1, constructing a complete locator expression and attempting to find the element each time. When a lookup throws a non-existent element exception, the loop terminates, returning the highest index value successfully located or the located target element. This mechanism can adaptively determine the actual length of the dynamic list and locate the target element at a specified index position.

[0086] (3) Fuzzy matching mechanism

[0087] Fuzzy matching is used to locate elements by simultaneously applying multiple attributes. The path expression for locating an element contains functions that partially match element class names, text content, or attributes. For example, `contains(@class, 'btn')` matches elements whose `class` attribute contains the string "btn"; `contains(text(), 'Submit')` matches elements whose text content contains "Submit"; and `starts-with(@id, 'dynamic_')` matches elements whose `id` attribute begins with "dynamic_". This mechanism allows combining multiple matching conditions to achieve fuzzy location of element class names, text content, or attributes, significantly enhancing tolerance for subtle page changes.

[0088] After successfully locating the target element through the above three mechanisms, the system converts the element address of the target element into an element location expression and stores it in the location layer of the test script for reuse in subsequent tests.

[0089] S4, Locate the target element

[0090] Based on the element location expression stored at the corresponding position in the updated test script, the nth target element in the current page is located sequentially according to the positioning strategies of ID, CSS, XPath, and Text; where n is the index of the target element, n=1, 2, ..., N;

[0091] If any positioning strategy successfully locates the current target element, proceed to step S5;

[0092] If all positioning strategies fail, an intelligent anomaly handling mechanism is used to attempt multiple repositioning attempts until a preset attempt limit is reached. If the target element still cannot be located, it is determined that the target element does not exist, the anomaly status is recorded, and step S7 is executed; specifically:

[0093] S4.1 The AW layer in the test script is based on the element positioning expression stored in the positioning layer in the updated test script. It uses the driver layer to locate the nth target element on the current page in sequence according to the positioning strategies of ID, CSS, XPath and Text. Here, n is the index of the target element, n=1, 2, ..., N.

[0094] S4.2 Determine whether the ID of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.3.

[0095] S4.3 Determine whether the CSS of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.4.

[0096] S4.4 Determine whether the XPath of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.5.

[0097] S4.5 Determine whether the Text of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.6.

[0098] S4.6. Employ an intelligent exception handling mechanism, return to step S4.1, and attempt to relocate the current target element multiple times until the preset attempt limit is reached. If the current target element still cannot be located, it is determined that the target element does not exist, the exception status is recorded, and step S7 is executed. The preset attempt limit is 10 times. Starting from the 3rd attempt, the time interval between each attempt is greater than the time interval between the previous attempts to avoid instantaneous pressure on the server and to provide sufficient waiting time for asynchronous loading and network latency.

[0099] For example, the driver layer can retry low-level interactive operations of a web application, such as retrying element search operations or retrying click operations. Retrying at the driver layer uses short intervals, such as 0.5 seconds, and can be done a maximum of 10 times.

[0100] In other embodiments, the preset attempt limit can be adjusted according to the actual situation.

[0101] S5, Load the target element

[0102] After locating the current target element, determine whether the target element has been fully loaded; if yes, proceed to step S6; if no, retry multiple times until the preset retry limit is reached. If the target element is still not fully loaded, determine that the target element is abnormal, record the abnormal state, and proceed to step S7; specifically:

[0103] S5.1 After locating the current target element, a dynamic loading completion detection mechanism oriented towards element identifiers is adopted to determine whether the current target element has been loaded through active polling. If yes, proceed to step S6; otherwise, proceed to step S5.2.

[0104] The dynamic loading completion detection mechanism for element identifiers includes: automatically triggering an exponential backoff retry strategy when the initial existence verification of the target element fails, and dynamically adjusting the interval of subsequent checks. For example:

[0105] First retry: Wait 1 second;

[0106] Second retry: Wait 1.5 seconds (1 second × 1.5);

[0107] Third retry: Wait 2.25 seconds (1.5 seconds × 1.5);

[0108] And so on.

[0109] This strategy effectively avoids wasting system resources due to high-frequency polling, while providing a gradually increasing waiting time for slowly rendered page elements (such as content loaded asynchronously via AJAX), achieving a balance between efficiency and reliability.

[0110] S5.2. Using a multiple retry mechanism, return to step S5.1 and retry multiple times to reload the current target element until the preset retry limit is reached. If the current target element is still not loaded, the target element is determined to be abnormal, the abnormal status is recorded, and step S7 is executed.

[0111] The system has a preset retry limit of 3 times, a base wait interval of 1 second, and the time interval for the third retry is longer than that for the second retry. In each attempt, an explicit wait condition (waiting for the target element to appear in the DOM) is used, and the wait duration is equal to the current interval. If the target element appears within the wait time, the waiting process ends and proceeds to the next step. If the target element does not appear within the timeout period, the attempt count is incremented, and the next wait interval is extended using an exponential backoff strategy (e.g., multiplied by 1.5 times the current interval), before proceeding to the next attempt. If all attempts fail, the target element is considered abnormal, and the abnormal state is recorded.

[0112] S6, Element Test

[0113] Based on the corresponding business action unit, test the current target element, obtain the test report for the target element, and execute step S7; specifically:

[0114] S6.1 The test case layer of the test script is based on the test steps of the test case and calls the corresponding business action unit stored in the AW layer; the AW layer responds to the call of the test case layer, references the element positioning expression stored in the positioning layer, and calls the basic operation encapsulated in the driver layer to test the current target element and obtain the corresponding test result.

[0115] S6.2 The driver layer uses the obtained test results to verify whether the basic operation it executes is successful and records it to obtain the basic operation verification result; then, the test result and the basic operation verification result are transmitted to the AW layer.

[0116] S6.3 The AW layer verifies whether the business action unit executed by it is successfully executed based on the received test results and records it to obtain the business action unit verification result; then, the test results, basic operation verification results and business action unit verification results are transmitted to the use case layer.

[0117] S6.4 The test case layer compares the received test results with the expected results stored in it using its internal decision rules to determine whether the test case is successful and records the result.

[0118] S6.5. Based on the basic operation verification results returned by the driver layer, the business action unit verification results returned by the AW layer, and the judgment results, the example layer generates a test report for the current target element and executes step S7.

[0119] S7. Determine if there are any unprocessed target elements on the current page; if yes, set n=n+1 and return to step S4 to locate and load the next target element on the current page; if no, proceed to step S8.

[0120] S8. Determine if there are any unprocessed pages. If yes, set m = m + 1 and return to step S3. If no, output all abnormal states and test reports to complete the self-stabilization of the Web test script based on element awareness and layer decoupling.

[0121] The following example, using the "Add to Cart" function of an e-commerce application, illustrates the collaboration process of each layer of the test script:

[0122] (1) Use Case Layer: Write a test case “test_add_to_cart”, which calls the three business action units of the AW layer in sequence: login (passing in username and password), search for products (passing in keywords), and add to cart (no parameters).

[0123] (2) AW layer: The "Add to Cart" action unit internally references the predefined "Add to Cart" button key name in the positioning layer and obtains a set of various positioning expressions (including ID, CSS, XPath, and Text) corresponding to the button. Then, it calls the "Click Element" method of the driver layer and passes the set of positioning expressions as parameters.

[0124] (3) Positioning layer: Stores a set of positioning expressions for the "Add to Cart" button, for example: ID is "add_cart_btn", CSS is ".btn-add-cart", XPath is " / / button[contains(@class,'add')]", and the text fragment is "Add to Cart".

[0125] (4) Driver layer: When the "click element" method is executed, the gradient positioning mechanism is first invoked, and positioning is attempted in the order of priority: ID, CSS, XPath, and text fragment. If ID positioning is successful, the element is clicked directly; if ID positioning fails, CSS positioning is automatically switched, and so on. After successful positioning and clicking, the driver layer returns a success status.

[0126] (5) Dynamic Loading Detection: After clicking "Add to Cart," the test script needs to confirm that the product has been successfully added. The AW layer calls the "Wait for Element to Appear" method of the driver layer to check whether the shopping cart icon has finished loading. The driver layer adopts an exponential backoff polling strategy, waiting for a maximum of 5 seconds until the shopping cart icon appears or a timeout occurs.

[0127] (6) Intelligent anomaly handling: If the initial location fails due to network latency, the driver layer will automatically retry 3 times (with an interval of 0.5 seconds); if it still fails, the AW layer will retry the entire "Add to Cart" business action (with an interval of 2 seconds, and a maximum of 2 retries). If all retries fail, the test case will be marked as failed and logged.

[0128] (7) After the target element is successfully loaded, the test process is carried out and the test report is output. A test report example is shown in Table 1.

[0129] Table 1 Test Report Example

[0130]

[0131] Through the above process, the test script can adaptively respond to common changes such as page element ID changes, XPath path adjustments, and network latency, and can be executed stably without manual intervention, which greatly reduces the cost of automated testing and maintenance caused by the continuous evolution of web application pages.

[0132] This embodiment provides a self-stabilizing Web test script system based on element awareness and layered decoupling. The system includes a processor and a storage device.

[0133] The storage device is used to store computer programs. When the computer program is executed by the processor, it enables the processor to implement the aforementioned self-stabilizing method for web test scripts based on element awareness and layered decoupling.

[0134] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit them. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the present invention.

Claims

1. A self-stabilizing method for Web test scripts based on element awareness and layered decoupling, characterized in that, Includes the following steps: S1. Construct test scripts for testing the Web application; the Web application includes M pages, each page includes N target elements, where M≥1, N≥1; S2. Establish a communication connection between the test script built in step S1 and the web application; S3. For the m-th page, obtain the element address and corresponding business action unit of all target elements on the page, and convert each element address into an element location expression; store the element location expression and the corresponding business action unit in the corresponding position of the test script to update the test script; where m is the page number, m=1, 2, ..., M; S4. Based on the element positioning expression stored at the corresponding position in the updated test script, locate the nth target element in the current page in sequence according to the positioning strategies of ID, CSS, XPath and Text; where n is the index of the target element, n=1, 2, ..., N; If any positioning strategy successfully locates the current target element, proceed to step S5; If all positioning strategies fail, an intelligent anomaly handling mechanism is used to attempt multiple repositioning attempts until the preset attempt limit is reached. If the current target element still cannot be located, the target element is determined to not exist, the anomaly status is recorded, and step S7 is executed. S5. After locating the current target element, determine whether the current target element has been loaded. If yes, proceed to step S6. If no, retry multiple times until the preset retry limit is reached. If the current target element is still not loaded, determine that the target element is abnormal, record the abnormal status and proceed to step S7. S6. Test the current target element based on the corresponding business action unit, obtain the test report of the target element, and execute step S7. S7. Determine if there are any unprocessed target elements on the current page; if yes, set n=n+1 and return to step S4 to locate and load the next target element on the current page; if no, proceed to step S8. S8. Determine if there are any unprocessed pages. If yes, set m = m + 1 and return to step S3. If no, output all abnormal states and test reports to complete the self-stabilization of the Web test script based on element awareness and layer decoupling.

2. The self-stabilizing method for Web test scripts based on element awareness and layered decoupling according to claim 1, characterized in that: In step S1, the test script includes a test case layer, an AW layer, a location layer, and a driver layer; The use case layer contains test cases, which include test steps, expected results, and corresponding judgment rules; the positioning layer is used to obtain and store the element positioning expressions of each target element in the page after the page structure is updated; the AW layer is used to obtain and store the business action units formed by the page operation after the page business process is changed. The driver layer is used to encapsulate the basic operations for low-level interaction with web applications; The test case layer interacts with the AW layer, and is used to call the business action unit within the AW layer and receive the test results returned by the AW layer. The AW layer interacts with the driver layer, and is used to call the basic operations within the driver layer and receive the test results returned by the driver layer; the AW layer interacts with the positioning layer, and is used to reference the element positioning expressions stored within the positioning layer. The driver layer communicates with the web application and is used to perform tests on the web application based on basic operations and receive test results.

3. The self-stabilizing method for Web test scripts based on element awareness and layered decoupling according to claim 2, characterized in that, Step S3 is as follows: For the m-th page, a dynamic web element address acquisition mechanism is used to obtain the element addresses of all target elements on the page, and all element addresses are converted into element location expressions. At the same time, the business action units corresponding to all target elements are obtained. Then, the element location expressions and the corresponding business action units are stored in the location layer and AW layer of the test script respectively to update the test script. Here, m is the page number, m=1,2,...,M.

4. The self-stabilizing method for Web test scripts based on element awareness and hierarchical decoupling according to claim 2 or 3, characterized in that, Step S4 is as follows: S4.1 The AW layer in the test script is based on the element positioning expression stored in the positioning layer in the updated test script. It uses the driver layer to locate the nth target element in the current page in sequence according to the positioning strategies of ID, CSS, XPath and Text; where n is the index of the target element, n=1, 2, ..., N; S4.2 Determine whether the ID of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.

3. S4.3 Determine whether the CSS of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.

4. S4.4 Determine whether the XPath of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.

5. S4.5 Determine whether the Text of the current target element has been located. If yes, proceed to step S5; otherwise, proceed to step S4.

6. S4.

6. Using an intelligent exception handling mechanism, return to step S4.1 to attempt to relocate the current target element multiple times until the preset attempt limit is reached. If the current target element still cannot be located, it is determined that the target element does not exist, the exception status is recorded, and step S7 is executed.

5. The self-stabilizing method for Web test scripts based on element awareness and layered decoupling according to claim 4, characterized in that, Step S5 is as follows: S5.1 After locating the current target element, a dynamic loading completion detection mechanism oriented towards element identifiers is adopted to determine whether the current target element has been loaded through active polling. If yes, proceed to step S6; otherwise, proceed to step S5.

2. S5.

2. Using a multiple retry mechanism, return to step S5.1 and retry multiple times to reload the current target element until the preset retry limit is reached. If the current target element is still not loaded, the target element is determined to be abnormal, the abnormal status is recorded, and step S7 is executed.

6. The self-stabilizing method for Web test scripts based on element awareness and layered decoupling according to claim 5, characterized in that, Step S6 is as follows: S6.1 The test case layer of the test script is based on the test steps of the test case and calls the corresponding business action unit stored in the AW layer; the AW layer responds to the call of the test case layer, references the element positioning expression stored in the positioning layer, and calls the basic operation encapsulated in the driver layer to test the current target element and obtain the corresponding test result. S6.2 The driver layer uses the obtained test results to verify whether the basic operation it executes is successful and records it to obtain the basic operation verification result; then, the test result and the basic operation verification result are returned to the AW layer. S6.3 The AW layer verifies whether the business action unit it executes is successfully executed based on the received test results and records it to obtain the business action unit verification result; Then, the test results, basic operation verification results, and business action unit verification results are returned to the test case layer; S6.4 The test case layer compares the received test results with the expected results stored in it using its internal decision rules to determine whether the test case is successful and records the result. S6.

5. Based on the basic operation verification results returned by the driver layer, the business action unit verification results returned by the AW layer, and the judgment results, the use case layer generates a test report for the current target element and executes step S7.

7. The self-stabilizing method for Web test scripts based on element awareness and layered decoupling according to claim 5, characterized in that, In step S5.1, the dynamic loading completion detection mechanism for element identifiers includes: When the initial existence verification of the target element fails, an exponential backoff retry strategy is automatically triggered, dynamically adjusting the interval between subsequent checks.

8. The self-stabilizing method for Web test scripts based on element awareness and layered decoupling according to claim 3, characterized in that: In step S3, the mechanism for dynamically obtaining the address of Web elements includes a gradient positioning mechanism, a dynamic path positioning mechanism, and a fuzzy matching mechanism. The gradient positioning mechanism attempts to obtain the element address of the target element in the order of its ID, CSS, XPath, and Text positioning strategies. The dynamic path location mechanism is used to process dynamic lists or target elements with dynamic indexes. It determines the list length or locates the target element by parameterizing the index variable in the location expression and cyclically incrementing the index value until the target element is detected to be non-existent. The fuzzy matching mechanism is used to locate a target element using multiple attributes simultaneously, and the path expression for locating the element contains a function that performs partial matching on the element class name, text content, or attributes.

9. The self-stabilizing method for Web test scripts based on element awareness and layered decoupling according to claim 1, characterized in that: In step S4, the preset maximum number of attempts is 10; starting from the 3rd attempt, the time interval between each attempt is greater than the time interval between the previous attempts; In step S5, the preset retry limit is 3 times; the time interval of the third retry is greater than the time interval of the second retry.

10. A self-stabilizing Web test script system based on element awareness and hierarchical decoupling, characterized in that, include: Processor and storage device; The storage device is used to store computer programs; When the computer program is executed by a processor, the processor implements the self-stabilizing method for Web test scripts based on element awareness and hierarchical decoupling as described in any one of claims 1-9.

Citation Information

Patent Citations

  • Layered script design method for automated testing of webpages

    CN106354649A