ngrinder groovy 参数化登录账号和密码(以mysql数据库为例)
2023-09-14 08:59:26 时间
import static net.grinder.script.Grinder.grinder import static org.junit.Assert.* import static org.hamcrest.Matchers.* import net.grinder.plugin.http.HTTPRequest import net.grinder.plugin.http.HTTPPluginControl import net.grinder.script.GTest import net.grinder.script.Grinder import net.grinder.scriptengine.groovy.junit.GrinderRunner import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread // import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3 import org.junit.Before import org.junit.BeforeClass import org.junit.Test import org.junit.runner.RunWith import java.util.Date import java.util.List import java.util.ArrayList import HTTPClient.Cookie import HTTPClient.CookieModule import HTTPClient.HTTPResponse import HTTPClient.NVPair //导入数据库连接依赖的包 import groovy.sql.Sql @RunWith(GrinderRunner) class TestRunner { public static GTest test public static HTTPRequest request public static NVPair[] headers = [] public static NVPair[] params = [] public static Cookie[] cookies = [] // 定义两个字符串列表,接收 账号和密码 public static String[] sql_nameList, sql_pwdList // 定义sql连接数据库的对象 public static Sql sql = Sql.newInstance("jdbc:mysql://IP:port/databaseName", "user", "password", "com.mysql.jdbc.Driver") @BeforeProcess public static void beforeProcess() { HTTPPluginControl.getConnectionDefaults().timeout = 6000 test = new GTest(1, "10.248.12.10") request = new HTTPRequest() // 设置请求头header List<NVPair> headerList = new ArrayList<NVPair>() headerList.add(new NVPair("Content-Type", "application/json")) headers = headerList.toArray() // 定义查询语句的执行结果, LineList 是一个map def LineList = sql.rows("SELECT user_name,password FROM `user` where user_name like 'test%';") grinder.logger.info("sqlresult={}",LineList) // 获取map中 name 和 pwd 的值 sql_nameList = LineList.user_name sql_pwdList = LineList.password grinder.logger.info("sql_nameList={},sql_pwdList={}",sql_nameList,sql_pwdList); grinder.logger.info("before process."); } @BeforeThread public void beforeThread() { test.record(this, "test") grinder.statistics.delayReports=true; grinder.logger.info("before thread."); } @Before public void before() { request.setHeaders(headers) cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) } grinder.logger.info("before thread. init headers and cookies"); } @Test public void test(){ int totalAgents=grinder.getProperties().getInt("grinder.agents",1); //总代理数 int total_processes=grinder.getProperties().getInt("grinder.processes",1); //总进程数 int total_threads=grinder.getProperties().getInt("grinder.threads",1); //总线程数 int Listindex=grinder.agentNumber*total_processes*total_threads + grinder.processNumber*total_threads + grinder.threadNumber + totalAgents * total_processes * total_threads * grinder.runNumber grinder.logger.info("runNumber={},processNumber={},threadNumber={},name={},pwd={}",grinder.runNumber,grinder.processNumber,grinder.threadNumber,sql_nameList[Listindex],sql_pwdList[Listindex]) // 重新生成body String body = "{\"userName\":\"${sql_nameList[Listindex]}\",\"password\":\"${sql_pwdList[Listindex]}\"}" grinder.logger.info("body={}",body) HTTPResponse result = request.POST("http://10.248.12.10:3462/login", body.getBytes())//发起请求 grinder.logger.info("响应报文={}",result.getText()) //设置断言 if(assertThat(result.getText(), containsString("\"code\":200"))){ grinder.logger.info("响应失败,响应报文={}",result.getText()) } } }
相关文章
- linux mysql root密码忘记后重置
- 【C/C++学院】(23)Mysql数据库编程--C语言编程实现mysql客户端
- 使用phpMyAdmin修改MySQL数据库root用户密码
- 【MySQL】批量删除mysql中数据库中的表
- 【MySql】mysql 数据库数据订正
- MySQL按照汉字的拼音排序,mysql汉字排序
- MySQL忘记密码的正确解决方法
- mysql分享一:运维角度浅谈MySQL数据库优化
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
- 【问题解决方案】MySQL安装后无法启动-net start mysql服务名无效
- MySQL--执行mysql脚本及其脚本编写
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)--续
- ngrinder groovy 参数化登录账号和密码(以mysql数据库为例)
- MySQL索引覆盖扫描(覆盖索引)
- MYsql 数据库密码忘记(Window)-2(mysql 5.7)
- linux -安装mysql,配置密码,开启远程访问
- MYSQL导入数据报错|MYSQL导入超大文件报错|MYSQL导入大数据库报错:2006 - MySQL server has gone away
- MySQL Study之--Mysql无法启动“mysql.host”
- Mysql的mysqldump详解 mysql数据库备份和导入
- Mysql数据库的mysql Schema 究竟有哪些东西& 手工注入的基础要领
- Mysql 中Left join ...on 之后 and 与where的区别
- Mysql之mysql工具
- 【高可用MySQL解决方案】centos7配置mysql主从复制
- MySQL报错Column count of mysql.user is wrong. Expected 43, found 42. Created with MySQL 50568, now run