Jmeter如何测试需要登录的接口

首先说明本系统的登录采用的是session,但是无论是什么登录步骤都是大差不差的

1.首先要构造用户测试的数据保存到数据库

2.构造请求获取请求后的请求头(如果需要代码参照请看最后)
3.解析请求头获取需要的cookie(sessionId是放在cookie中)或token。如果对header的结果无法把控就打印一个看一下结构再解析
4.保存所有的cookie或token到一个txt文件中

5.接下来就是jmter的测试环节,最基本得有这几个

选择刚刚生成的cookie.txt文件

使用${}获取即可

接下来测试即可

所有代码

bash 复制代码
    /**
     * 登录接口示例
     */
    @PostMapping("/login")
    @ResponseBody
    public ResponseDTO userLogin(@Valid @RequestBody UserLoginReq req, HttpSession session, HttpServletRequest request) 
bash 复制代码
    @Test
    public void testCreateUserGetCookie() throws IOException {
        //1.创建测试用户集合信息,插入user表和student表
        List<TUserEntity> userEntityList = new ArrayList<>();
        List<TStudentEntity> studentEntityList = new ArrayList<>();
        //2.保存用户信息
        for (int i = 1; i <= 500; i++) {
            UUID uuid = UUID.randomUUID();
            String userId = uuid.toString().replaceAll("-", "");
            //user表
            TUserEntity userEntity = new TUserEntity();
            userEntity.setId(userId);
            userEntity.setRealName("user"+i);
            userEntity.setGender(1);
            userEntity.setUsername("123123"+i);
            userEntity.setPhoneNumber("130"+String.format("%0" + (11 - "130".length()) + "d", i));
            userEntity.setEmail("2080067852"+"@qq.com");
            userEntity.setPassword( CommonUtils.encodeByMd5(defaultPassword+salt));
            userEntity.setSchoolId("001");
            userEntity.setCampusId("0011");
            userEntity.setIcon("school.png");
            userEntity.setStatus(1);
            userEntity.setRecordStatus(1);
            userEntity.setKind(0);
            userEntity.setCreateTime(new Date());
            userEntityList.add(userEntity);
            //student表
            TStudentEntity tStudentEntity = new TStudentEntity();
            tStudentEntity.setSchoolId("001");
            tStudentEntity.setUserId(userId);
            tStudentEntity.setStudentNum("2015201805"+i);
            tStudentEntity.setName("张"+i+"憨");
            tStudentEntity.setGender(1);
            tStudentEntity.setCampus("test");
            tStudentEntity.setMajor("test");
            tStudentEntity.setClassNum("311631");
            tStudentEntity.setCreateTime(new Date());
            studentEntityList.add(tStudentEntity);
        }
        //3.批量保存
        studentService.saveBatch(studentEntityList);
        userService.saveBatch(userEntityList);
        //4.批量发起请求
        for (int i = 0; i < userEntityList.size(); i++) {
            //拼装参数发起请求,根据你自己的登录接口
            UserLoginReq userLoginReq = new UserLoginReq();
            userLoginReq.setUsername("123123"+(i+1));
            userLoginReq.setPassword("123456");
            userLoginReq.setSchoolId("001");
            userLoginReq.setCode("da513");
            String url="http://localhost:8080/api/v1/public/login";
            //发起请求,保存tokenn.txt文件
            sendHttpGetCookie(userLoginReq, url);
        }
    }
bash 复制代码
    private void sendHttpGetCookie(UserLoginReq userLoginReq, String url) throws IOException {
        // 2.创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建Http Post请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        //3.封装需要的数据
        String json = JSON.toJSONString(userLoginReq); // 将对象转换为 JSON 字符串
        StringEntity requestEntity = new StringEntity(json, StandardCharsets.UTF_8);
        httpPost.setEntity(requestEntity);
//        response = httpClient.execute(httpPost);
        //4.解析结果
        CloseableHttpResponse execute = httpClient.execute(httpPost);
        Header[] headers = execute.getAllHeaders();
        for (Header header : headers) {
            System.out.println(header.getName()+":"+header.getValue());
        }
        String value = headers[3].getValue();
        String cookie = value.split(";")[0];

        //2.写入文件
        File filePath=new File("D:/cookies.txt");
        FileWriter fileWriter = new FileWriter(filePath, true); // 使用 true 表示追加写入
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(cookie);
        bufferedWriter.newLine(); // 换行
        // 关闭文件写入流
        bufferedWriter.close();
        System.out.println("cookieRes: "+cookie);
    }
bash 复制代码
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.13</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpmime</artifactId>
                <version>4.5.13</version>
                <scope>runtime</scope>
            </dependency>
相关推荐
测试杂货铺2 天前
Jmeter(六):json断言元件,jmeter参数化实现
jmeter·json
Moshow郑锴13 天前
JMeter API 并发性能测试计划JMX文件解析
jmeter
qq_49244844614 天前
java项目打包成jar包,并给jmeter使用
java·jmeter·jar
kanyun12314 天前
Jmeter使用过程中的一些总结
jmeter
無_爲14 天前
JavaEE:使用JMeter进行接口并发测试
java·jmeter·java-ee
cllsse16 天前
jmeter学习
jmeter
明月与玄武16 天前
JMeter 高阶玩法:分布式压测的技术核心技术要点
jmeter·分布式压测
会又不会16 天前
JMeter-SSE响应数据自动化3.0
jmeter·自动化
软件测试大叔17 天前
Jmeter ServerAgent在arm环境启动报错no libsigar-aarch64-linux.so in java.library.path
jmeter
心灵宝贝17 天前
Apache JMeter 2.9使用教程:压力测试入门步骤详解
jmeter·apache·压力测试