A mobile UI automation testing method based on appium+python
Patent Information
- Application Number
- CN202111599406.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-24
- Publication Date
- 2024-11-05
- Estimated Expiration
- 2041-12-24
AI Technical Summary
[0003]本发明的目的在于提供一种基于appium+python的移动端ui自动化测试方法,以期解决当前自动化测试需要执行大量的测试用例和测试数据,导致替换测试数据或修改测试用例逻辑容易出错,甚至造成脚本瘫痪的问题
[0021] The beneficial effects that the mobile terminal UI automated testing method based on appium+python disclosed in this application may bring include but are not limited to:
Smart Images

Figure CN114238142B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Internet technology, and in particular to a mobile terminal UI automation testing method based on Appium+Python. Background Art
[0002] In the prior art, mobile UI automation testing mainly executes test scripts to send requests to Appium to execute test instructions. Each test case requires a large number of scripts to control a large amount of test data. However, there is no good management of the case scripts and test data, resulting in code and data confusion. Therefore, when the case function logic and test data change, a large amount of modification work will be generated, and it is easy to cause script and data errors, affecting the test accuracy, and even causing the script to be paralyzed and unable to execute; this also leads to an increase in workload. Summary of the invention
[0003] The purpose of the present invention is to provide a mobile UI automation testing method based on Appium+Python, in order to solve the problem that the current automated testing needs to execute a large number of test cases and test data, which leads to easy errors in replacing test data or modifying test case logic, and even causes script paralysis.
[0004] In order to achieve the above object, the present invention adopts the following technical solutions:
[0005] A mobile UI automation testing method based on Appium+Python, including the following steps:
[0006] Build a Python project, including directories: common, excel_data, result, testcase, runner;
[0007] In the directory common, use Python's related functions to encapsulate the mobile UI element search method, operation method, Excel data reading method, screenshot method, and driver of Appium;
[0008] In the runner directory, use the Python unit test framework unittest to encapsulate the test suite acquisition method;
[0009] In the testcase directory, use Python to write test case scripts for the functions of the app under test;
[0010] Create a new Excel form in the directory excel_data to manage test data and tested UI element information;
[0011] Execute the test case, start the driver to call the appium service to connect to the mobile device and enable the app and perform specified operations on the app according to the test case.
[0012] In some embodiments, the use of Python related functions in the directory common to encapsulate mobile UI search, operation methods, Excel data reading methods, screenshot methods, and driver for Appium includes:
[0013] In the directory common, use the methods in Python's WebElement related library to encapsulate the mobile UI element search method and operation method; use the related methods in Python's xlrd library to encapsulate the Excel data reading method; use Python's get_screenshot_as_file function to encapsulate the screenshot method; encapsulate the configuration of the appium service to connect to the mobile device and start the driver of the application under test.
[0014] In some embodiments, an excel document is used in the excel_data directory to manage test data; the result directory is used as a save path for test reports and screenshot attachments; the testcace directory is used to write test cases for the app under test, with one script for each test case; and the runner directory is used to edit methods for obtaining test suites.
[0015] In some embodiments, the methods in the WebElement related library of Python are used in the directory common to encapsulate the mobile UI element search method and operation method, including searching, clicking, inputting, and dragging elements according to element ID, name, xpath, etc.; the element unique identification identifier is parameterized during encapsulation.
[0016] In some embodiments, the related methods in the xlrd library of Python are used to encapsulate the Excel data reading method; including reading all the data in the specified form in sequence, and reading a row of data in the specified form as required.
[0017] In some embodiments, the screenshot method is encapsulated using Python's get_screenshot_as_file function, including: when a problem is encountered during the test and the test case cannot be passed, the screenshot is automatically saved and stored in the result directory, and the picture naming rule is "%Y-%m-%d-%H-%M-%S+test case name", which is used to take screenshots and save them immediately after a bug is found during the automatic test process, and locate the bug later according to the time and test case name.
[0018] In some embodiments, the package configures the appium service to connect to the mobile device and start the driver of the application under test, including that the driver connects to the corresponding mobile device through the mobile device name and version according to the appium server address and port number, and starts the app under test on the mobile device through the app package name and Activity under test.
[0019] In some embodiments, Python's unit testing framework unittest is used to obtain test cases according to the case storage path and the case name to form a test suite; each test case obtains the mobile UI element information and test data stored in Excel by calling the Excel data reading method.
[0020] In some embodiments, the search type of the mobile terminal UI element information obtained by calling the element operation method is searched for the element, and then the operation is automatically performed on the related elements according to the operation type.
[0021] The beneficial effects that may be brought about by the mobile UI automation testing method based on Appium+Python disclosed in this application include but are not limited to:
[0022] This patent manages test data and element information through Excel documents, encapsulates test case scenarios and operation element methods in Python, and encapsulates Appium services in Python to connect to mobile devices and start the app. Then, the test data and element information stored in Excel are used to automatically select the operation type for the specified element to complete the test case scenario test. This forms a set of code and data separation, which can achieve only code modification for logic changes without affecting test data, and only data modification for test data changes without affecting code; and a mobile UI automation test system with reusable code. This improves code writing efficiency and test accuracy, and reduces workload. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] Figure 1 It is a flow chart of the present invention. DETAILED DESCRIPTION
[0024] In order to make the purpose, technical solution and advantages of the present application more clear, the present application is further described in detail below in conjunction with the embodiments. It should be understood that the specific embodiments described here are only used to explain the present application and are not used to limit the present application.
[0025] On the contrary, the present application covers any substitution, modification, equivalent method and scheme made on the essence and scope of the present application as defined by the claims. Further, in order to make the public have a better understanding of the present application, some specific details are described in detail in the detailed description of the present application below. Those skilled in the art can fully understand the present application without the description of these details.
[0026] The following is a detailed description of a mobile UI automation testing method based on Appium+Python involved in the embodiments of the present application. It is worth noting that the following embodiments are only used to explain the present application and do not constitute a limitation of the present application.
[0027] like Figure 1 As shown, a mobile UI automation testing method based on Appium+Python includes the following steps:
[0028] Step 1: Create a new Python project, including the directories: common, excel_data, result, testcase, and runner.
[0029] Step 2: In the directory common, use the methods in Python's WebElement and other related libraries to encapsulate the mobile UI element search method and operation method; use the related methods in Python's xlrd library to encapsulate the Excel data reading method; use Python's get_screenshot_as_file function to encapsulate the screenshot method; encapsulate the configuration of the appium service to connect to the mobile device and start the driver of the application under test.
[0030] Step 3: Use the Python unit test framework unittest to encapsulate the test suite acquisition method in the runner directory.
[0031] Step 4: Use Python to write a test case script for the mobile UI under test in the testcase directory.
[0032] Step 5: Create a new Excel form in the directory excel_data to manage test data and the UI element information of the tested mobile terminal.
[0033] In order to better realize the present invention, a further technical solution is:
[0034] According to an embodiment of the present invention, the common directory described in step 1 encapsulates the mobile terminal UI element search method, operation method, Excel reading method, screenshot method, and appium driver startup; the excel_data directory uses an Excel document to manage test data; the result directory is used as the save path for test reports and screenshot attachments; the testcace directory writes test cases for the tested app, with one script for each case. The runner directory edits the method for obtaining the test suite.
[0035] Further technical solution: In step 2, by calling the methods in Python's WebElement and other related libraries, the search elements and operation elements are encapsulated again according to their own needs, including searching, clicking, inputting, and dragging elements according to element id, name, xpath, etc. When encapsulating, the element's unique identification (id, name, xpath, etc.) is parameterized.
[0036] Further technical solution: Step 2 encapsulates the Excel data reading method by calling the relevant method in Python's xlrd library, including reading all the data in the specified form in sequence and reading a row of data in the specified form as required (passing in the Excel storage path, shell name, and the row number of the form to be read as parameters).
[0037] Further technical solution: Step 2 encapsulates the screenshot method by calling Python's get_screenshot_as_file function, which is used to automatically take screenshots and save them when problems are encountered during the test and the test case cannot be passed, and store them in the result directory. The image naming rule is "%Y-%m-%d-%H-%M-%S+test case name". It is used to take screenshots and save them immediately after a bug is found during the automatic test process. The bug can be located later according to the time and test case name.
[0038] Further technical solution: The driver described in step 2 connects to the corresponding mobile device through the mobile device name and version according to the appium server address and port number, and starts the tested app on the mobile device through the tested app package name and Activity.
[0039] Further technical solution: In step three, use Python's unit testing framework unittest to obtain the test cases in step four according to the test case storage path and the test case name to form a test suite.
[0040] Further technical solution: Each test case in step 4 obtains the mobile UI element information and test data stored in Excel in step 5 by calling the Excel data reading method in step 2;
[0041] A further technical solution: call the element operation method in step 2 to find the element according to the search type (such as by_xpath, by_name, etc.) of the mobile UI element information obtained in the above solution, and then automatically perform operations on related elements according to the operation type (such as send_keys, click, etc.).
[0042] Further technical solution: When an error occurs in the operation of an element in the above solution, the screenshot method in step 2 is called to take a screenshot of the current operation element page and save it, so as to facilitate tracking and finding the test problem later;
[0043] Further technical solution: Steps 1 to 5 complete the construction of the appium+python app automation testing framework; when the project function changes, it can be modified by changing the test case scenario in step 4; when the test data changes, it can be modified by modifying the test data in Excel in step 5.
[0044] The present invention manages test data and element information through Excel documents, encapsulates test case scenarios and operation element methods in Python, encapsulates Appium services in Python to connect to mobile devices and start apps, and then automatically selects the operation type for a specified element through the test data and element information stored in Excel to complete the test case scenario test. A set of code and data separation is formed, and only code modification for logic changes will not affect test data, and only data modification for test data changes will not affect code; and the code is reusable in a mobile UI automation test system. The code writing efficiency and test accuracy are improved, and the workload is reduced.
[0045] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A mobile UI automation testing method based on Appium+Python, characterized in that: The following steps are involved: Build a Python project, including directories: common, excel_data, result, testcase, runner; In the directory common, use Python's related functions to encapsulate the mobile UI element search method, operation method, Excel data reading method, screenshot method, and driver of Appium; In the runner directory, use the Python unit test framework unittest to encapsulate the test suite acquisition method; In the testcase directory, use Python to write test case scripts for the functions of the app under test; Create a new Excel form in the directory excel_data to manage test data and tested UI element information; Execute the test case, start the driver to call the appium service to connect to the mobile device and enable the app and perform specified operations on the app according to the test case; The excel_data directory uses excel documents to manage test data; The result directory is used as the storage path for test reports and screenshot attachments. The testcace directory is used to write test cases for the app under test, with one script for each test case. Edit the method to get the test suite in the runner directory; The method in the WebElement related library of Python is used in the directory common to encapsulate the mobile UI element search method and operation method, including searching, clicking, inputting, and dragging elements according to the element ID, name, and xpath; the element unique identification mark is parameterized during encapsulation; The related functions of Python are used in the directory common to encapsulate the mobile UI search, operation method, Excel data reading method, screenshot method, and driver of Appium, including: In the directory common, use the methods in Python's WebElement related library to encapsulate the mobile UI element search method and operation method; use the related methods in Python's xlrd library to encapsulate the Excel data reading method; use Python's get_screenshot_as_file function to encapsulate the screenshot method; encapsulate the configuration of the appium service to connect to the mobile device and start the driver of the application under test; Use Python's unit testing framework unittest to obtain test cases according to the case storage path and case name to form a test suite; each test case obtains the mobile UI element information and test data stored in Excel by calling the Excel data reading method.
2. According to a mobile terminal UI automation testing method based on appium+python according to claim 1, it is characterized in that: The method of using the relevant methods in the xlrd library of Python to encapsulate the Excel data reading method includes reading all the data in the specified form in sequence and reading a row of data in the specified form as required.
3. According to a mobile terminal UI automation testing method based on appium+python according to claim 1, it is characterized in that: The method of encapsulating the screenshot using the get_screenshot_as_file function of Python includes: automatically taking screenshots and saving them in the result directory when a problem is encountered during the test and the test case cannot be passed, and immediately taking screenshots and saving them after a bug is found during the automatic test, and locating the bug later according to the time and the test case name.
4. According to a mobile terminal UI automation testing method based on appium+python according to claim 1, it is characterized in that: The package configuration appium service connects to the mobile device and starts the driver of the tested application, including that the driver connects to the corresponding mobile device through the mobile device name and version according to the appium server address and port number and starts the tested app on the mobile device through the tested app package name and Activity.
5. According to a mobile terminal UI automation testing method based on appium+python according to claim 1, it is characterized in that: Call the element operation method to obtain the mobile UI element information, find the element by the search type, and then automatically perform operations on related elements according to the operation type.
