Configuration management of element positioning for entry-level Web automated testing

 

We have talked about the Selenium usage tutorial before. In this article, we will learn the configuration management of element positioning.

Purpose

Web automation testing, as a "sweet pastry" that cannot be bypassed in the field of software automation testing, is usually the preferred learning object for the majority of testing practitioners. Compared with the automation of C/S architecture, B/S has its inability Many advantages that are neglected, from the development trend of the industry, the characteristics of the R&D model, and the support of testing tools, its overall complete ecology has far exceeded the testing value of the C/S architecture.

Next, on the basis of the last time, we will supplement and optimize the automation code that has already taken shape, and use the characteristics of Python to design the management method of positioning elements and explain the ideas and points of attention.

management method

Generally speaking, the common information management methods of interface elements include configuration files, persistence, special platforms and tools, etc. Everyone has different opinions on how to choose, but since we use Python as a language, we must make good use of its advantages to minimize management costs (manpower, time-consuming, etc.).

Compared with general text, we can use configuration files in ini format to manage the elements with known specific information in a unified manner, effectively separate business codes from interface elements, and reduce a large amount of maintenance work that occurs due to changes in requirements in the later stage.

Through the methods mentioned above, we can enter most of the commonly used element information into the configuration file, even if the later product or project changes, we can respond flexibly.

For example, if the logic and function of the product or project are changed, then we only need to modify the business code; if the UI changes, we only need to modify the corresponding configuration file. Only such a low-coupling automation framework can effectively improve the daily work efficiency of the test team.

In addition, in view of the existence of multiple testing roles in most teams, at the beginning of defining the configuration file, discussions within the team should also be carried out effectively, and some general factors of the configuration file (writing and naming conventions, storage path etc.) for full cognitive unification and integration. In case of low-level errors such as element names that cannot be found due to different element names when used later.

configuration method

Next, let's take a look at what content needs to be written in the general configuration file. The following figure shows the positioning information of the relevant elements on the login page. Let me tell you here. If the scale of the system is not too large, it is recommended to put all The element information of the system is managed in a configuration file, which is not suitable for decentralized processing of multiple paths and multiple files.

The format of the file is basically the configuration format of ini. The content is composed of multiple sections (in square brackets). There can be multiple configuration items in each section. Each configuration item is composed of configuration item name, positioning method, and positioning value.

The name of the configuration item does not need to be explained. It will be used directly in the code. It can be simply understood as a variable name. The id behind it is the positioning method. It is not limited to the id here. If you want to use other positioning methods, change it to the corresponding one. method, the specific positioning method can refer to the get_element method in the previous article.

The colon in it is just to facilitate the division of values ​​in the business code later, the colon is not mandatory, and other symbols are acceptable.

The last is the positioning value, that is, the attribute value of the corresponding attribute of various elements in the development interface of the developer. It should be noted here that the value must correspond to the method you specified earlier, and it must not be mistaken.

Here is a simple example. For example, there is a get_element method in the LoginPageElement class. Then we convert the elements in the original business code into configuration files:

log_pg_ele = LoginPageElement('chrome')
log_pg_ele.get_element('id', 'transaction_log_treaty')

Is it very simple, because in the original get_element method, we have defined the two parameters of the relevant positioning method and positioning value, so according to the composition of the configuration items in the configuration file, seamless conversion can be performed without additional operations .

method implementation

With the corresponding configuration file, we can use python to design and implement related configuration parsing and element calling methods. Here we first create the corresponding ini file in the directory specified by each project, and then repeat it again. If it is shared by the team, the file name and storage path need to be unified.

Suppose my configuration file is called FundManSys.ini, which is stored in the conf folder of the project. Before we use Python to parse the configuration file, we need to install the corresponding functional modules.

Here we use the configparser library, which can read and parse our general ini class configuration files.

Whether it is python interpreter or pip install, we will start the overall function design and implementation after the installation is complete.

First, let's implement the function of reading configuration files.

Here I created a class, but did not write it out. We will directly show the methods in it. You can freely use the name. In the constructor, the node name that needs to be specified will be brought in. If the name is not specified, a specific value will be brought in. .

The second piece of code reads and parses the configuration file by calling the method ConfigParser, fills in the path and file name of the configuration file in read(), sets variables here, and finally returns the entire object.

def __init__(self, section=None):
    if section:
        self.section = section
    else:
        self.section = 'business_log_v2'

def load_ini(self):
    cf = configparser.ConfigParser()
    cf.read(ini_file)
    return cf

Then how do we call the information inside after obtaining the object, and then we need to perform further processing on it.

The chain writing method is used here. Otherwise, you can define a variable in the constructor to receive the object returned in the load_ini method. Needless to say, the two parameters section and key correspond to each other clearly.

def get_data(self, key):
    data = self.load_ini().get(self.section, key)
    return data

The basic configuration file parsing function is designed and encapsulated, is it very simple? Then how do we actually combine the desired data into the existing element operation code, and then we can start to connect the business code with the actual configuration function.

We optimize the previously designed get_element method and add the operation of obtaining and processing data.

The load_ini instantiated here will use the business_log_v2 node name by default because no node name is specified.

Here, the original method parameters have been changed. You can see that the changed parameters are reduced. The original by and ele parameters are also replaced by the positioning method and positioning value in the configuration item. This is what we want to achieve the goal of.

For specific changes, you can compare the get_element method in the previous article.

def get_element(self, key):
    element = None
    load_ini = EleConfiguration()
    data = load_ini.get_data(key)
    by, ele = data.split(':')
    try:
        if by == 'id':
            element = self.driver.find_element(By.ID, ele)
        elif by == 'name':
            element = self.driver.find_element(By.NAME, ele)
        elif by == 'css':
            element = self.driver.find_element(By.CSS_SELECTOR, ele)
        elif by == 'class':
            element = self.driver.find_element(By.CLASS_NAME, ele)
        else:
            element = self.driver.find_element(By.XPATH, ele)
    except:
        session.add(ele_err_msg)
        session.commit()
    return element

So far, our overall configuration management design has been completed. In the actual use process, we only need to write the code of the page business logic according to our actual scene. No matter what kind of interface business operation is, we can directly instantiate and call the get_element method to Element positioning is carried out, and due to the separation of real element information and business code, the overall readability and maintainability are also greatly improved.

Points to note

1. The name of the configparser module in Python 2 and 3 is different, and there are uppercase and lowercase points. Note that 2 is ConfigParser and 3 is configparser;

2. If the key-value pair in the configuration item uses a colon for the connection symbol, pay attention to the status of Chinese and English, so as to avoid accidental error reporting due to the use of a Chinese colon;

3. Do not set too many section names in the configuration file. Too many names will easily confuse the content of the configuration file and will not be maintained;

4. Rather than paying attention, it is better to say that it is a specification. All kinds of names in the configuration file should be defined in English as much as possible, which is similar to the naming meaning of writing code. Pinyin should not appear in this type of configuration file.

Finally: The complete software testing video tutorial below has been sorted out and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/weixin_50829653/article/details/132305852