CN117950633A - Construction method of python-based selenium page element location library - Google Patents

Construction method of python-based selenium page element location library Download PDF

Info

Publication number
CN117950633A
CN117950633A CN202410094600.2A CN202410094600A CN117950633A CN 117950633 A CN117950633 A CN 117950633A CN 202410094600 A CN202410094600 A CN 202410094600A CN 117950633 A CN117950633 A CN 117950633A
Authority
CN
China
Prior art keywords
positioning
webelement
character string
library
page
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202410094600.2A
Other languages
Chinese (zh)
Inventor
卫宇珩
王舜
高英博
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Dongfeng Communication Technology Co ltd
Original Assignee
Dongfeng Communication Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Dongfeng Communication Technology Co ltd filed Critical Dongfeng Communication Technology Co ltd
Priority to CN202410094600.2A priority Critical patent/CN117950633A/en
Publication of CN117950633A publication Critical patent/CN117950633A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/20Software design
    • G06F8/24Object-oriented
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/38Creation or generation of source code for implementing user interfaces
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/70Software maintenance or management

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Human Computer Interaction (AREA)
  • Stored Programmes (AREA)

Abstract

The invention provides a python-based selenium page element positioning library construction method, which relates to the technical field of element positioning library construction and comprises the following steps: establishing connection with the browser webdriver through a selenium library of python, obtaining the ids and the attributes of all elements in the current page of the browser webdriver in batches, and returning webElement objects corresponding to the elements; positioning the element by utilizing an element positioning mode and the attribute of the element to obtain a positioning character string of the element; testing the positioning character string, and determining a final positioning character string according to a test result; and (3) performing excel export on the final positioning character string and the related fields by interacting with the xlrd library to obtain an element positioning library. The invention constructs a callable class in python for calling other module functions, thereby facilitating the script developer to quickly produce element positioning part codes and accelerating the script completion speed.

Description

Construction method of python-based selenium page element location library
Technical Field
The invention relates to the technical field of element positioning library construction, in particular to a method for constructing a python-based selenium page element positioning library.
Background
Because the digital transformation requirement of most enterprises is large at present, the application system is frequently developed, so that a plurality of application systems are easy to have the problem of difficult screening in the development process, and the problems that the systems are often not responded after certain buttons are pressed after entering into formal production or the control console has errors are solved. The importance of automated test scripts is increasing. The core technology of the automatic script writing comprises the positioning and operation of page elements, the writing and management of test cases and the analysis and reporting of test results.
Currently, an automation script developer needs to face challenges such as page element change, maintainability and stability of a test script, and the like, so as to ensure that the script normally operates in an application program with continuous change, and the main core problems are as follows: 1. instability of page elements: existing automation scripts are often affected by changes in page elements, and if page structures or element attributes change, the scripts may not execute properly, resulting in test failures and maintenance difficulties. 2. The maintainability of the test script is poor: due to page element change and other reasons, the maintainability of the test script is poor, frequent manual adjustment and updating are needed, and the maintenance cost is increased. 3. The test efficiency is low: the existing automation script has efficiency problems in element positioning and operation, and has low test execution speed, especially in large-scale tests. 4. Insufficient handling of the abnormal situation: the existing script generally lacks sufficient processing for abnormal conditions, cannot adapt to complex test scenes, and causes inaccurate test results. 5. Problems caused by the frame itself: some front-end frameworks only provide ids based on the type of element used, rather than using a unique framework id for it, making it difficult for an automation script developer to define an element into the framework at one time, even using an absolute path to locate the element, resulting in a significant reduction in code readability.
Disclosure of Invention
In view of this, the present invention provides a method for constructing a selenium page element location library based on python, which constructs a callable class in python to call other module functions, so as to facilitate a script developer to quickly generate element location part codes, thereby accelerating script completion speed.
The technical scheme of the invention is realized as follows: the invention provides a python-based selenium page element location library construction method, which comprises the following steps:
S1, establishing connection with a browser webdriver through a selenium library of python, acquiring ids and attributes of all elements in a current page of the browser webdriver in batches, and returning webElement objects corresponding to the elements;
S2, positioning the element by utilizing an element positioning mode and the attribute of the element to obtain a positioning character string of the element;
S3, testing the positioning character string, and determining a final positioning character string according to a test result;
S4, performing excel export on the final positioning character string and related fields through interaction with the xlrd library to obtain an element positioning library.
Based on the above technical solution, preferably, step S1 includes:
s11, establishing connection with webdriver of a plurality of browsers by using a selenium library;
S12, acquiring all elements in the current page by using a find_elements method webdriver, and returning to a webElement object list;
s13, traversing webElement the object list, acquiring the id and attribute of each element, and storing the id and attribute as a dictionary;
S14, screening out the corresponding webElement objects according to the element category, and storing the webElement objects into a set, namely webElement objects.
Based on the above technical solution, preferably, step S2 includes:
S21, selecting an element positioning mode according to the parameters, and outputting a positioning logic example of each webElement object as a positioning character string according to the element positioning mode;
S22, returning the positioning character strings of all webElement objects as return values.
Based on the above technical solution, preferably, step S1 further includes:
When traversing webElement the object list, a decision is made for each element, and if no corresponding element is found, an empty set is returned.
Based on the above technical solution, preferably, step S3 includes:
S31, simulating a browser call page through a thread, and acquiring a target webElement object through a find_element function of selenium by using the output positioning character string as input;
S32, retrying the process of acquiring the target webElement object for N times through a retrying retry library, if the position of the element is not changed in the N retries, testing the corresponding positioning character string to be qualified, taking the positioning character string as a final positioning character string, and if the position of the element is changed in the N retries, reselecting an element positioning mode to position, and forming a new positioning character string retest until the test is qualified.
On the basis of the above technical solution, preferably, in step S32, the method for determining whether the position of the element is changed includes:
For a single target webElement object, accessing a corresponding element object through an element attribute of the target webElement object, wherein the element object is a corresponding element in a selenium current page;
And carrying out hash operation on the element object, calling a_hash_method of the element object to obtain a hash value of the element object, and if the hash value is unchanged in N retries, the position of the element is unchanged, otherwise, the position of the element is changed.
On the basis of the above technical solution, preferably, the method further includes:
after the element positioning library is constructed, a user selects a positioning character string, and reliability verification is carried out on the positioning character string through M times of refreshing pages:
if the positioning character string is positioned to the same element in the M times of page refreshing processes, judging that the positioning character string is reliable;
If the same element cannot be located by using the locating character string in the process of refreshing the page for M times, or error is returned in the locating process, the locating character string is judged to be unreliable, the element locating mode is marked as No, and the step S2 is returned for relocation.
Based on the above technical solution, preferably, step S2 further includes:
The webElement objects in selenium and the operation method of selenium are reversely docked, the webElement objects are input by using the send_keys method or the simulated keyboard operation, and the webElement objects are subjected to click () operation by positioning the character strings.
Based on the above technical solution, preferably, the method further includes checking and asserting an element location library:
Performing input content operation on each element by using an element positioning library;
After each content operation is input, carrying out assertion verification, judging whether the content operation is successful or not, and if the assertion fails, returning failure detailed information of the assertion.
Based on the above technical solution, preferably, the process of assertion verification includes:
for the Input box, packaging the send_keys and the keyboard operation methods, and respectively performing the operation of inputting the content;
For the Select box, encapsulating a select_by_one and a select_by_index method, and selecting one of the drop-down boxes according to the requirements;
for a special style Select box, after the send_keys input operation is executed, judging whether corresponding contents exist in the input box;
for the Radio box or Checkbox, it is determined whether the state after clicking and the state before clicking are different.
Compared with the prior art, the method has the following beneficial effects:
(1) The processing tool provided by the invention can be used for rapidly improving the completion speed of the UI automation script with a small page change function. By rapidly acquiring webelement objects of the page elements in batches and automatically identifying a reliable positioning mode, the development and maintenance processes of the UI automation script can be accelerated, the development efficiency is improved, and the maintenance cost is reduced;
(2) The invention can quickly identify whether the current script can be used normally or not and identify which elements are changed. By means of reverse test, page refreshing and the like, the positioning modes of elements in the script can be rapidly identified to be unreliable, so that the script failure problem caused by page variation can be timely found, and a test team can be helped to rapidly make adjustment and repair;
(3) The invention can generate the corresponding locator through a plurality of different locating modes, and can provide a plurality of possible solutions when one locating mode is not good. Through diversified positioning modes, the positioning flexibility of page elements can be increased, the problem of difficulty in positioning under page variation or specific scenes can be solved, and the applicability and flexibility of automatic testing are improved;
(4) The invention supports the function of exporting the locator of one page, and can provide convenience for the construction of a system automation system. The element locating library is exported, so that the locating library of the page element can be conveniently established, convenience is provided for writing and executing subsequent automatic test cases, and the establishment and maintenance of an automatic test system are accelerated;
(5) According to the invention, the webelement objects of the page elements are obtained in batches, and the reverse test of the positioning character strings is performed by utilizing the algorithm logic and the retrying retry library, so that the stability of the automatic test can be improved. The accuracy and the reliability of the positioning mode are ensured, so that positioning failure caused by page variation is reduced, and the stability and the reliability of automatic testing are improved;
(6) The invention can confirm whether the same element can be always positioned by using the same locator by repeatedly testing the page for refreshing, and can quickly identify the reliability of the positioning mode. If the positioning mode cannot pass the verification, the improper positioning mode can be found and repaired in time, and the reliability and the accuracy of the test case are improved.
Drawings
In order to more clearly illustrate the embodiments of the invention or the technical solutions in the prior art, the drawings that are required in the embodiments or the description of the prior art will be briefly described, it being obvious that the drawings in the following description are only some embodiments of the invention, and that other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
FIG. 1 is a flow chart of a method according to an embodiment of the present invention;
FIG. 2 is a schematic diagram showing two different processes for writing scripts according to an embodiment of the present invention.
Detailed Description
The following description of the embodiments of the present invention will clearly and fully describe the technical aspects of the embodiments of the present invention, and it is apparent that the described embodiments are only some embodiments of the present invention, not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the present invention without making any inventive effort, are intended to fall within the scope of the present invention.
As shown in fig. 1, the invention provides a python-based selenium page element location library construction method, which comprises the following steps:
S1, establishing connection with a browser webdriver through a selenium library of python, acquiring ids and attributes of all elements in a current page of the browser webdriver in batches, and returning webElement objects corresponding to the elements;
S2, positioning the element by utilizing an element positioning mode and the attribute of the element to obtain a positioning character string of the element;
S3, testing the positioning character string, and determining a final positioning character string according to a test result;
S4, performing excel export on the final positioning character string and related fields through interaction with the xlrd library to obtain an element positioning library.
Specifically, in an embodiment of the present invention, step S1 includes:
s11, establishing connection with webdriver of a plurality of browsers by using a selenium library;
S12, acquiring all elements in the current page by using a find_elements method webdriver, and returning to a webElement object list;
s13, traversing webElement the object list, acquiring the id and attribute of each element, and storing the id and attribute as a dictionary;
S14, screening out the corresponding webElement objects according to the element category, and storing the webElement objects into a set, namely webElement objects.
Step S1 further includes:
When traversing webElement the object list, a decision is made for each element, and if no corresponding element is found, an empty set is returned.
In one embodiment, step S1 is:
The connection with multiple browsers webdriver supports the individual operation of the browser port of the designated port through the alternate port, and also supports the operation of the current browser state by directly using webdriver; and acquiring the contents of all elements of the current page. And may return its corresponding webElement objects according to the corresponding categories (input/select/radio). The returned object is a set. If the corresponding element is still not found after traversing the full page, the system returns an empty set, facilitating the subsequent direct insertion of data into the set if code is required instead of direct return. Otherwise a new set is needed in the development process. For example:
Using SeleniumWebDriver: seleniumWebDriver is a tool for automated Web application testing that supports a variety of browsers, including Chrome, firefox, edge, etc. Through Selenium WebDriver, connections to multiple browsers webdriver can be made and support individual operations on a designated port browser through alternate ports.
Using Selenium Grid: selenium Grid is part of Selenium that allows multiple tests to be run concurrently on different browsers and operating systems. Through Selenium Grid, different browser instances can be launched and operated individually by designating different ports.
Acquiring the content of all elements of the current page: all elements of the current page may be obtained using the find elements method provided by Selenium WebDriver. An element object can be stored in a set by traversing all elements and returning the corresponding webElement object according to its category (input/select/radio).
The implementation process can be as follows:
First, webDriver connections for multiple browsers are established using Selenium libraries and support individual operations on a designated port browser through an alternate port. This may enable different browser instances, such as Chrome and Firefox, etc., by assigning different port numbers.
Acquiring the content of all elements of the current page, all webElement objects in the current page may be acquired using the find elements method of WebDriver.
Returning its corresponding webElement object according to the corresponding category (input/select/radio), the webElement object list may be traversed, a determination made for each element, and eligible elements stored into a set.
When traversing webElement the object list, if no corresponding element is found, an empty set is returned. This can be done by checking if the set is empty after the traversal is over, and if so, creating an empty set for facilitating subsequent operations.
If the browser with the designated port needs to be operated separately, the browser can be connected to the corresponding browser instance by designating a different port, and then the corresponding operation is carried out.
If the current browser state needs to be operated, webDriver can be directly used for operating the current browser state.
In the embodiment, webDriver connection of a plurality of browsers is realized, independent operation of the designated port browser through the alternative port is supported, and the flexibility and efficiency of testing are improved. And acquiring the contents of all elements of the current page, and providing basic data for subsequent element operation and analysis. And the corresponding webElement objects are screened out according to the element types and stored in a set, so that the classification and management efficiency of the page elements are improved. And under the condition that element positioning fails, returning an empty set, so that subsequent operation on the set is facilitated, and the flexibility and maintainability of codes are improved.
Specifically, in an embodiment of the present invention, step S2 includes:
S21, selecting an element positioning mode according to the parameters, and outputting a positioning logic example of each webElement object as a positioning character string according to the element positioning mode;
S22, returning the positioning character strings of all webElement objects as return values.
In one embodiment, the implementation procedure of step S2 is:
Using SeleniumWebDriver: all webElement objects in the page are obtained using the find elements method of SeleniumWebDriver.
Traversing webElement the object list: traversing the obtained webElement object list, and outputting a positioning logic example of each webElement object as a positioning character string according to the element positioning mode selected by the parameters.
Storing the positioning character string: the locator string for each webElement object is stored in a list.
Returning a locating character string list: and returning the list storing webElement object locating strings as a return value.
For example:
First, the webdriver module and the By class imported into the Selenium library contain various element positioning methods, such as ID, XPATH, CLASS _name, etc.
A function named get_element_ locators is defined that accepts two parameters: web_elements_list represents webElement object list, and locator_type represents location type.
Within the function, each webElement object in the web_elements_list is traversed, and each object is processed according to the imported locator_type parameter.
If locator_type is "ID", then the ID attribute of webElement objects is obtained using element_get_attribute ("ID") and formatted as a string of "driver_find_element (by ID, '{ }'") which is stored in locators list.
If locator_type is "XPATH", then the element_attribute ("XPATH") is used to obtain webElement the XPATH attribute of the object, and it is formatted as a string of "driver_find_element (by. XPATH, '{ }'") and stored in locators list.
The processing of other positioning modes can be expanded according to specific requirements.
Finally, the list locators storing webElement object localization strings is returned as a return value.
In the main program, the get_elements_ locators function is called, and the get_elements_list and the positioning mode parameter "ID" of the webElement object list web_elements_list are input, and the returned positioning logic example list element_ locators is printed out.
In this embodiment, a flexible manner is provided, and corresponding positioning logic examples can be output by selecting different element positioning manners according to parameters, for example, using conventional element positioning manners such as ID, XPATH, and the like. The returned positioning character string list can help the developer to better understand the positioning logic of each webElement object, so that the subsequent script writing and debugging are facilitated.
Specifically, in an embodiment of the present invention, step S2 further includes:
The webElement objects in selenium and the operation method of selenium are reversely docked, the webElement objects are input by using the send_keys method or the simulated keyboard operation, and the webElement objects are subjected to click () operation by positioning the character strings.
For webElement objects, the input is made using the send_keys method or simulated keyboard operations. This may be accomplished by calling the send_keys method of webElement objects, which sends text content to an input box or other inputtable element. Or may be implemented using the ActionChains classes if a simulated keyboard operation is required. Click () operation is performed on webElement objects by locating a string. A list of locator strings for webElement objects has been generated in step S2, from which a click operation can be performed on webElement objects.
In this embodiment, reverse butt joint of the webElement objects and the Selenium operation method is implemented, so that operations can be directly performed after the webElement objects are acquired, and readability and maintainability of codes are improved. The input is performed through the send_keys method or the simulated keyboard operation, so that the operation of an input box and other inputtable elements is facilitated, and the flexibility of an automatic test script is improved.
Specifically, in an embodiment of the present invention, step S3 includes:
S31, simulating a browser call page through a thread, and acquiring a target webElement object through a find_element function of selenium by using the output positioning character string as input;
S32, retrying the process of acquiring the target webElement object for N times through a retrying retry library, if the position of the element is not changed in the N retries, testing the corresponding positioning character string to be qualified, taking the positioning character string as a final positioning character string, and if the position of the element is changed in the N retries, reselecting an element positioning mode to position, and forming a new positioning character string retest until the test is qualified.
The method for judging whether the position of the element is changed or not comprises the following steps:
For a single target webElement object, accessing a corresponding element object through an element attribute of the target webElement object, wherein the element object is a corresponding element in a selenium current page;
And carrying out hash operation on the element object, calling a_hash_method of the element object to obtain a hash value of the element object, and if the hash value is unchanged in N retries, the position of the element is unchanged, otherwise, the position of the element is changed.
The following description describes a specific embodiment:
First, a thread is created to simulate a browser call page, and the target webElement object is obtained through the find_element function of Selenium using the output positioning string as input.
The threading module and webdriver modules in selenium are imported.
A function named browser_thread is then defined that accepts a parameter locator representing the element's locator string.
In the function, a Chrome browser object is instantiated first, and then an example web page is opened. The incoming locating string is then used to locate the page elements and perform some element location change determination and test operations.
Next, a thread object named t is created by using the threading class, an objective function to be executed is specified as browser_thread, and a locating string of a parameter "you_locator_string" is imported as an element.
Finally, the start method of the thread object is invoked to start the thread, so that the browser operation is executed in a single thread.
And performing N retries on the process of acquiring the target webElement object through the retrying retry library, and judging whether the position of the element is changed or not. This can be implemented using the retrying library of Python, which provides the functionality to retry the execution of the function.
The retry of the process of acquiring the target webElement object is performed using the retry decorator in the retrying library.
The get_element function is first decorated by the @ retry (stop_max_ attempt _number=3) decorator as a retriable function, with a maximum number of retries of 3.
When the get_element function is called, if the webElement objects were not successfully acquired in the first attempt, the retry decorator would automatically retry, 3 times at most.
If webElement objects are not successfully acquired after 3 retries, an exception may be thrown and the failed retry condition may be handled in the accept block.
The method for judging whether the position of the element is changed is that aiming at a single target webElement object, the corresponding element object is accessed through the element attribute of the target webElement object, the hash operation is carried out on the element object to obtain the hash value, if the hash value is unchanged in N retries, the position of the element is not changed, otherwise, the position of the element is changed.
# Acquisition target webElement object
element=driver.find_element_by_xpath("your_locator_string")
# Obtain hash value of element object
hash_value=hash(element)
In the embodiment, the page is called by the multithreading simulation browser, so that the test of the element positioning character string and the judgment of element position change are realized, and the concurrency and efficiency of the test are improved. N retries of the process of acquiring the target webElement object are performed by using retrying libraries, so that the robustness and stability of codes are enhanced. The hash value is used for judging the position change of the element, so that the change of the element position can be more accurately determined, the possibility of misjudgment is reduced, and the accuracy and the reliability of the test are improved.
Specifically, in an embodiment of the present invention, step S4 includes:
The xlrd library was used to read and manipulate Excel files. The existing Excel file may be read by xlrd library or a new Excel file may be created and written using xlwt library. When reading an existing Excel file, the Excel table may be traversed and the locating strings and associated fields therein obtained and then stored in a data structure. The contents of these data structures can then be written to a new Excel file to form the element locator library.
An existing Excel file named 'exist_file.xls' is first opened using xlrd library and the locating string and related field data of the first worksheet therein are obtained. A new Excel file is then created using xlwt libraries and a worksheet named 'element_ locators' is created therein. The locating string and related field data are then written to the new worksheet using the for loop, and finally the new Excel file is saved as a file named' element_locations.
In the embodiment, the locating character string and the related fields are exported to the Excel file to form an element locating library, so that the management and maintenance of the locating character string are facilitated. And an Excel file is used as an element positioning library, so that the positioning character strings and related fields are more convenient to find and use, and the maintainability of the test case is improved.
After the element positioning library is obtained, the invention also comprises the step of verifying the reliability of the positioning character string, wherein the specific process is as follows:
after the element positioning library is constructed, a user selects a positioning character string, and reliability verification is carried out on the positioning character string through M times of refreshing pages:
if the positioning character string is positioned to the same element in the M times of page refreshing processes, judging that the positioning character string is reliable;
If the same element cannot be located by using the locating character string in the process of refreshing the page for M times, or error is returned in the locating process, the locating character string is judged to be unreliable, the element locating mode is marked as No, and the step S2 is returned for relocation.
In one particular embodiment, the following description is provided:
and simulating a user to select a positioning character string, refreshing the page for M times through Selenium, and then judging whether the positioning character string is reliable or not. This can be done by cycling through M page refreshes and using the locating string to find the element, determining if the same element can be located.
The webdriver modules in time and selenium are first introduced. Then define a function named validate _locator that accepts two parameters: the locator represents the locating string of the element, and M represents the number of times verification is required.
In validate _locator function, a Chrome browser object is first instantiated, and then the page is refreshed M times through a loop. After each page refresh, the incoming locating string is used to locate the page element and some judgment and verification operations are performed.
Finally, the locating character string is verified for 5 times of page refreshing by calling validate _locator function and introducing 'you_locator_string' and 5 as parameters.
In the embodiment, the reliability verification is performed on the positioning character string, so that the accuracy and the reliability of the positioning character string can be improved, and the possibility of positioning failure caused by page change is reduced. The user can confirm whether the locator generated by the tool class can locate in the same form through the verification process, so that the correctness of the function of leading out the locator is confirmed, and the reliability and the stability of the test case are enhanced.
The invention also comprises the steps of verifying and asserting the element positioning library, wherein the specific process is as follows:
Performing input content operation on each element by using an element positioning library;
After each content operation is input, carrying out assertion verification, judging whether the content operation is successful or not, and if the assertion fails, returning failure detailed information of the assertion.
The process of assertion verification includes:
for the Input box, packaging the send_keys and the keyboard operation methods, and respectively performing the operation of inputting the content;
For the Select box, encapsulating a select_by_one and a select_by_index method, and selecting one of the drop-down boxes according to the requirements;
for a special style Select box, after the send_keys input operation is executed, judging whether corresponding contents exist in the input box;
for the Radio box or Checkbox, it is determined whether the state after clicking and the state before clicking are different.
And performing input content operation on each element by using the positioning character strings in the element positioning library, performing assertion verification after each input content operation, and judging whether the input content operation is successful or not. This can be achieved by encapsulating various Input operations, such as operating on an Input box, a Select box, a Radio box, or a Checkbox, and performing assertion verification.
The specific implementation process is as follows:
input_content function: two parameters are accepted, element represents the page element to be operated on, content represents the content to be entered. The function inputs content to the specified page element using the send_keys method.
Select_option_by_text function: accepting two parameters, select_element represents a drop-down list element, option_text represents the option text to be selected. The function uses the Select class to Select the options in the drop down list, matching the option text through the visible_text.
Select_option_by_index function: accepting two parameters, select_element represents a drop-down list element, index represents an index of the option to be selected. The function uses the Select class to Select the options in the drop down list, matching the index of the options through index.
Validate _element function: accepting two parameters, the element represents the page element to be verified, and expected_content represents the desired content. The function first obtains the values of the page elements and then uses the assert statement to assert whether the actual content matches the desired content, and if not, throws the exception.
Finally, each element in the element location library is operated and verified through one cycle. The method comprises the steps of locating page elements by using a driver_element_by_xpath method, then calling an input_content function to input 'test content' to the elements, and then calling a validate _element function to verify the input content, so that the input content is ensured to be consistent with the expected content.
In the embodiment, by verifying and asserting the element positioning library, the page element can be ensured to normally receive the input content, the accuracy of the input content is verified, and the reliability and the accuracy of the automatic test are improved. A daemon process is started to capture the control desk errors in the browser and output error information, so that developers can be helped to find errors in pages in time, and the pages are subjected to error processing and repairing, so that the stability and reliability of test cases are improved.
After the verification and the verification of the element positioning library are passed, the corresponding set is output to excel by utilizing the linkage of the xlrd library, so that the testers can directly multiplex the test signals. In particular, developers can copy and paste these locale content into their UI test scripts, avoiding manual inspection of browser element content and writing locale logic. In this way, the efficiency of script development can be improved, the possibility of errors can be reduced, and the accuracy of the script can be ensured.
For example, assume that a developer is writing a UI test script that requires automated testing of a form page. Through the previous code, they can obtain the location string and associated field name for each form field. They can then apply these positioning strings directly into their UI test scripts without manually checking the browser element content. Therefore, the construction of the page script can be completed rapidly, and the development efficiency is improved.
The process of writing a script using a conventional method and writing a script using the element localization library provided by the present invention is shown in fig. 2.
The foregoing description of the preferred embodiments of the invention is not intended to be limiting, but rather is intended to cover all modifications, equivalents, alternatives, and improvements that fall within the spirit and scope of the invention.

Claims (10)

1. The method for constructing the selenium page element location library based on python is characterized by comprising the following steps of:
S1, establishing connection with a browser webdriver through a selenium library of python, acquiring ids and attributes of all elements in a current page of the browser webdriver in batches, and returning webElement objects corresponding to the elements;
S2, positioning the element by utilizing an element positioning mode and the attribute of the element to obtain a positioning character string of the element;
S3, testing the positioning character string, and determining a final positioning character string according to a test result;
S4, performing excel export on the final positioning character string and related fields through interaction with the xlrd library to obtain an element positioning library.
2. The method for constructing a python-based selenium page element location library of claim 1, wherein step S1 comprises:
s11, establishing connection with webdriver of a plurality of browsers by using a selenium library;
S12, acquiring all elements in the current page by using a find_elements method webdriver, and returning to a webElement object list;
s13, traversing webElement the object list, acquiring the id and attribute of each element, and storing the id and attribute as a dictionary;
S14, screening out the corresponding webElement objects according to the element category, and storing the webElement objects into a set, namely webElement objects.
3. The method for constructing a python-based selenium page element location library as claimed in claim 2, wherein step S2 comprises:
S21, selecting an element positioning mode according to the parameters, and outputting a positioning logic example of each webElement object as a positioning character string according to the element positioning mode;
S22, returning the positioning character strings of all webElement objects as return values.
4. The method for constructing a python-based selenium page element location library of claim 2, wherein step S1 further comprises:
When traversing webElement the object list, a decision is made for each element, and if no corresponding element is found, an empty set is returned.
5. The method for constructing a python-based selenium page element location library of claim 1, wherein step S3 comprises:
S31, simulating a browser call page through a thread, and acquiring a target webElement object through a find_element function of selenium by using the output positioning character string as input;
S32, retrying the process of acquiring the target webElement object for N times through a retrying retry library, if the position of the element is not changed in the N retries, testing the corresponding positioning character string to be qualified, taking the positioning character string as a final positioning character string, and if the position of the element is changed in the N retries, reselecting an element positioning mode to position, and forming a new positioning character string retest until the test is qualified.
6. The method for constructing a python-based selenium page element location library as claimed in claim 5, wherein in step S32, the method for determining whether the location of the element is changed comprises:
For a single target webElement object, accessing a corresponding element object through an element attribute of the target webElement object, wherein the element object is a corresponding element in a selenium current page;
And carrying out hash operation on the element object, calling a_hash_method of the element object to obtain a hash value of the element object, and if the hash value is unchanged in N retries, the position of the element is unchanged, otherwise, the position of the element is changed.
7. The method for constructing a python-based selenium page element location library of claim 6, further comprising:
after the element positioning library is constructed, a user selects a positioning character string, and reliability verification is carried out on the positioning character string through M times of refreshing pages:
if the positioning character string is positioned to the same element in the M times of page refreshing processes, judging that the positioning character string is reliable;
If the same element cannot be located by using the locating character string in the process of refreshing the page for M times, or error is returned in the locating process, the locating character string is judged to be unreliable, the element locating mode is marked as No, and the step S2 is returned for relocation.
8. The method for constructing a python-based selenium page element location library of claim 1, wherein step S2 further comprises:
The webElement objects in selenium and the operation method of selenium are reversely docked, the webElement objects are input by using the send_keys method or the simulated keyboard operation, and the webElement objects are subjected to click () operation by positioning the character strings.
9. The method for constructing a python-based selenium page element locator according to claim 8, wherein said method further comprises checking and asserting the element locator:
Performing input content operation on each element by using an element positioning library;
After each content operation is input, carrying out assertion verification, judging whether the content operation is successful or not, and if the assertion fails, returning failure detailed information of the assertion.
10. The method for constructing a python-based selenium page element location library of claim 9, wherein the process of assertion verification comprises:
for the Input box, packaging the send_keys and the keyboard operation methods, and respectively performing the operation of inputting the content;
For the Select box, encapsulating a select_by_one and a select_by_index method, and selecting one of the drop-down boxes according to the requirements;
for a special style Select box, after the send_keys input operation is executed, judging whether corresponding contents exist in the input box;
for the Radio box or Checkbox, it is determined whether the state after clicking and the state before clicking are different.
CN202410094600.2A 2024-01-23 2024-01-23 Construction method of python-based selenium page element location library Pending CN117950633A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202410094600.2A CN117950633A (en) 2024-01-23 2024-01-23 Construction method of python-based selenium page element location library

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202410094600.2A CN117950633A (en) 2024-01-23 2024-01-23 Construction method of python-based selenium page element location library

Publications (1)

Publication Number Publication Date
CN117950633A true CN117950633A (en) 2024-04-30

Family

ID=90799327

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202410094600.2A Pending CN117950633A (en) 2024-01-23 2024-01-23 Construction method of python-based selenium page element location library

Country Status (1)

Country Link
CN (1) CN117950633A (en)

Similar Documents

Publication Publication Date Title
CN109189684B (en) Python-based automatic interface testing method
US7752501B2 (en) Dynamic generation and implementation of globalization verification testing for user interface controls
US7917895B2 (en) Automated software testing and validation system
CN108763076A (en) A kind of Software Automatic Testing Method, device, equipment and medium
CN109189479B (en) Parallel automatic verification method for processor instruction set
US7895575B2 (en) Apparatus and method for generating test driver
US20100064178A1 (en) World-Readiness and Globalization Testing Assemblies
CN113626326B (en) Dragging type zero code front end automatic test system based on image recognition
Tierno et al. Open issues for the automotive software testing
CN112181854A (en) Method, device, equipment and storage medium for generating flow automation script
CN113742215A (en) Method and system for automatically configuring and calling test tool to perform test analysis
CN109189688B (en) Test case script generation method and device and electronic equipment
CN103186459A (en) JAVA graphical user interface automatic test method based on scripts
CN110286882B (en) Foreground system design and verification method based on model detection
CN113434385A (en) Method and system for automatically generating test case for software model inspection tool
CN116820996A (en) Automatic generation method and device for integrated test cases based on artificial intelligence
CN117950633A (en) Construction method of python-based selenium page element location library
CN113378502B (en) Test method, device, medium and equipment for verifying signal trend code matching
CN113238940B (en) Interface test result comparison method, device, equipment and storage medium
CN112181809A (en) Automatic graphical user interface testing method based on multiple positioning methods
CN112015638A (en) Automatic testing method and system
CN112306767A (en) Automatic testing method for chip signal connection relation
CN118377726B (en) Web application automatic test method and computer device
CN109800155B (en) Method and device for testing QTE interlocking application software based on Probe
CN118550836A (en) Black box testing method and system without expected result injection

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination