Common skills for interface testing: Jmeter operates database

01. Configure JDBC Request

1. Add the required driver jar package

To use different databases, we need to introduce different jar packages.

Method 1: Copy the jar package directly to the lib directory of jmeter

mysql database:

Download mysql-connector-java-5.1.28.jar and put it in the lib directory of the jmeter root directory

sql server database:

Download sqljdbc4.jar and put it in the lib directory of the jmeter root directory

oracle database:

Put \product\10.2.0\db_1\jdbc\lib\ojdbc14.jar under the oracle data installation directory into the lib directory under the jmeter root directory

Method 2: Through Test Plan

If we don't use the method of copying the jar to the lib directory of jmeter, we can also use Jmeter's Test Plan to import the corresponding jar package, such as the jar package for importing oracle data below
insert image description here

2. Description of important parameters

Variable Name:

The name of the database connection pool, we can have multiple jdbc connection configurations, each of which can have a different name, and the appropriate connection pool can be selected for use in the jdbc request.

Database URL:

Database url, jdbc:mysql://host ip or machine name:mysql listening port number/database name, such as: jdbc:mysql://localhost:3306/test

JDBC Driver class:

JDBC driver

username:

Username for database login

password:

Password for database login

For the specific filling methods of different databases, you can refer to the following form:
insert image description here
after filling, it is roughly as follows:
insert image description here

Important parameter description:

Variable Name:

The name of the database connection pool needs to be consistent with the name of the Variable Name Bound Pool of JDBC Connection Configuration

Query:

Do not add ";" at the end of the filled sql statement

Parameter pain:

parameter value

Parameter types:

Parameter type, please refer to: Javadoc for java.sql.Types

Variable names:

The name of the variable that saves the result returned by the sql statement

Result variable name:

Create an object variable to hold all returned results

Query timeout:

query timeout

Handle result set:

Define how to process the results returned by callable statements
insert image description here
3. Execution results:
insert image description here

02. JDBC Request parameterization

method one)

Define variables and use variables in sql query:

1. Define a variable in Test Plan

(Of course, you can also use parameterization: Jmeter parameterization):
insert image description here
2. Use ${variable name} in sql query to reference:

select * from test where name='${name}'

insert image description here
Method Two)

Use "?" as a placeholder in the sql query, and pass the parameter value and parameter type, as shown in the following figure:

1. The passed parameter value is a constant, as shown in the figure, two variables are passed, and multiple variables are separated by ", ". Here, if you have data of type int, you should also mark it as type varchar in Parameter types, otherwise it will not work.
insert image description here
2. The passed parameter value is a variable, use ${variable name}
insert image description here 

03. Variables names parameter

1. How to use:

The explanation given by the jmeter official website is: If a value is set for this parameter, it will save the data returned by the sql statement and the total number of rows of the returned data. Suppose, the sql statement returns 2 rows, 3 columns, and the variables names are set to A, C

Then the following variables would be set to:

A_#=2 (total number of lines)

A_1=column 1, row 1

A_2=column 1, row 2

C_#=2 (total number of rows)

C_1=column 3, row 1

C_2=column 3, row 2

If the return result is 0, then A_# and C_# will be set to 0, and other variables will not be set.

If 6 rows of data are returned for the first time, and only 3 rows of data are returned for the second time, then the 3 more rows of data variables for the first time will be cleared.

You can use KaTeX parse error: Expected group after '_' at position 3: {A_̲#}, {A_1}... to get the corresponding value

Example:

We still use the above database to find out all the data. The test table has 3 fields and 5 records (if you forget, you can go back to the first step to check)

1. Add a jdbc request called "parameter 4", add a "Debug Sampler" to view the output results, and set the variables name to column1, column2, column3: 2. Execution results
insert image description here
:
insert image description here

Parse:

column1 represents all the data in the first column, and column1_# can get the number of rows in the first column

column1_n: Get the data of the nth row of the first column.

The functions of column2 and column3 are similar. If we only need the data of the first column and the third column, it can be written as column1, column3, and the "," in the middle cannot be omitted.

04. Result variable name parameter

1. How to use:

If you set a value for this parameter, it will create an object variable, save all the returned results, and get the specific value method:

columnValue = vars.getObject("resultObject").get(0).get("Column Name")

insert image description here

2. Execution result:
insert image description here


              [The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled]


1. From entry to mastery of Python programming

2. Interface automation project actual combat 

3. Actual Combat of Web Automation Project


4. Actual Combat of App Automation Project 

5. Resume of first-tier manufacturers


6. Test and develop DevOps system 

7. Commonly used automated testing tools

Eight, JMeter performance test 

9. Summary (little surprise at the end)

life is long so add oil. Every effort will not be let down, as long as you persevere, there will be rewards in the end. Cherish your time and pursue your dreams. Don't forget the original intention, forge ahead. Your future is in your hands!

Life is short, time is precious, we cannot predict what will happen in the future, but we can grasp the present moment. Cherish every day and work hard to make yourself stronger and better. Firm belief, persistent pursuit, success will eventually belong to you!

Only by constantly challenging yourself can you constantly surpass yourself. Persist in pursuing your dreams and move forward bravely, and you will find that the process of struggle is so beautiful and worthwhile. Believe in yourself, you can do it! 

                                    

Guess you like

Origin blog.csdn.net/nhb687095/article/details/132082388