[Software Testing Foundation] The core concept of automated testing framework design-keyword driven

Write in front

Many people will hear the concept of keyword driving when they come into contact with automated testing. However, when researching, they are not sure how to design and implement this driving model.

Keyword-driven, as a design form of automated testing framework, has been mentioned at a very early time. Its basic principle or idea is to separate the original automation code from code and data, and then separate the separated code twice to form the so-called behavior code and test code, and then drive the behavior code through the data content to generate the test code. . This makes it easier to manage automated test code and improve the reusability of automation, making it easier for users to understand and use automation (after all, there are still many testers who don't know anything about the code.


Maybe this explanation sounds a bit complicated, so let me give you a very simple example to let you understand the keyword-driven form:

1. I am eating.

2. You are eating.

3. He is eating.

In this group of content, you can see that no matter who it is, everyone is doing the act of eating, and the focus is on the role , which is the so-called "you, me and him", then we can extract the behavior of "eating" , As a public content, only need to provide a role data, you can achieve such a thing for anyone to eat. The process of extracting the behavior as public content is the so-called keyword encapsulation. The role data is used to drive such a behavior to form this thing. This is the so-called "keyword-driven + data-driven" automated test design mode Up.


So what should we do if we want to achieve keyword driving in automated testing?

The first is the preparation of the environment:

1. The generation of Selenium+WebDriver regular components: (For this part, please refer to the Selenium environment building I wrote before)

Import the Selenium module in Pycharm

Add the driver file under the Python installation root path

2. If you want to combine data drive, select the corresponding component installation according to the data file format:

Excel can pass xlrd

yaml can pass pyyaml

There are many other examples.

3. If you want the whole framework to be more complete, you can add a lot of other content, such as logging logging, Requests interface automation, Appium/uiautomator2 mobile UI automation, etc., etc., here are added according to personal preferences and needs. can.

After preparing the environment, it is the key keyword encapsulation and extraction. Here we take Selenium as an example:

First look at the basic engineering structure:

Define a most basic keyword structure and create a package specifically for managing various keyword objects, which can be WebUI, AppUI, and interface keyword objects. As a keyword library, used to store and be called. Define another package, which is specifically used to call the content of the keyword library to achieve automated execution. Of course, other forms can also be used, and many methods can be applied here.

Then let's take a look at how the keyword class is implemented:

In the keyword class, the normal element operation behavior is extracted from the Selenium object, and then encapsulated twice to generate your own keyword library. This library can be understood as a tool library, when it needs to be applied to any In the method, you can directly instantiate the library and then call it in the library. In this class, exceptions may occur during execution, such as element search failures, timeouts, etc., so it is best to add an exception handling mechanism when keyword encapsulation to avoid exceptions at runtime and fail to capture specific the reason.

Keyword debugging:

Because there is no data-driven class, we directly write the test code in Pycharm to debug the generated keyword class. It can be found by running that the whole process can be implemented normally, which means that the keyword object library is not too Big problem, and what we need to do in the future is to keep adding the required keywords in this object library. When combined with data drivers, such as Excel, yaml, etc., these data files can also be used to drive our object library.

SO.

Through keyword-driven implementation, the operation behavior of conventional automation code can be well separated from the test code, and an automated test pattern with high reusability can be generated. And combined with data-driven, it can be applied to many different types of business systems to achieve automated testing. So as to improve our work efficiency and personal automated testing capabilities. Come here to discuss and exchange, click on the secret code [CSDN]


Pay attention, don't get lost! If this article is helpful to you, don't forget to like and support!

Guess you like

Origin blog.csdn.net/shuaigezhou10086/article/details/108626133