前言

我喜欢周末是因为,可以做一些我自己喜欢的事。

比如我插上耳机,写点东西就能坐上一天,这也许算是属于我自己的一份静谧吧。

想系统学习请参考:Playwright+Java入门

使用Playwright进行API测试

1、总体感受

和其他API的依赖比起来,感觉使用起来不是很舒服,而且感觉繁琐呢,肯定是我研究的不够深入,不熟引起的。

2、初始化配置

这一部分相当于httpclient的初始化客户端操作,示例代码如下:

@BeforeClass
public void beforeClass() {
  playwright = Playwright.create();
  request = playwright.request().newContext(new APIRequest.NewContextOptions()
          .setBaseURL("http://localhost:8090"));
}

销毁操作,示例代码如下:

@AfterClass
public void afterClass() {
    if (request != null) {
        request.dispose();
        request = null;
    }
    if (playwright != null) {
        playwright.close();
        playwright = null;
    }
}

3、编写API测试

效果如下:

微软出品自动化神器【Playwright+Java】系列(八) 之 使用 Playwright进行API接口测试-小白菜博客
微软出品自动化神器【Playwright+Java】系列(八) 之 使用 Playwright进行API接口测试-小白菜博客

4、完整代码

这里我仅用查询(GET)和新增接口(POST)进行演示,完整示例代码如下:

package com.playwight.test;

import com.microsoft.playwright.APIRequest;
import com.microsoft.playwright.APIRequestContext;
import com.microsoft.playwright.APIResponse;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.RequestOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;

import static org.testng.Assert.assertTrue;

public class TestGitHubAPI {

    private Playwright playwright;
    private APIRequestContext request;

    @BeforeClass
    public void beforeClass() {
        playwright = Playwright.create();
    }

    /**
     * get请求
     */
    @Test
    public void testGetAPi() {
        request = playwright.request().newContext(new APIRequest.NewContextOptions()
                .setBaseURL("http://localhost:8090"));
        APIResponse getAPIResponse = request.get("/students");
        assertTrue(getAPIResponse.ok());
        System.out.println(getAPIResponse.text());
    }
    
   /**
     * post请求
     */
    @Test
    public void testPostApi() {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        request = playwright.request().newContext(new APIRequest.NewContextOptions()
                .setBaseURL("http://localhost:8090")
                .setExtraHTTPHeaders(headers));
        Map<String, String> data = new HashMap<>();
        data.put("className", "className");
        data.put("courseName", "english");
        data.put("email", "xiaoqiang@qq.com");
        data.put("name", "xiaoqiang");
        data.put("score", "90");
        data.put("sex", "boy");
        data.put("studentId", "00099");
        APIResponse postAPIResponse = request.post("/studentAdd",
                RequestOptions.create().setData(data));
        assertTrue(postAPIResponse.ok());
        System.out.println(postAPIResponse.text());
    }


    @AfterClass
    public void afterClass() {
        if (request != null) {
            request.dispose();
            request = null;
        }
        if (playwright != null) {
            playwright.close();
            playwright = null;
        }
    }

}

写在最后

感觉还是写API测试简单,而且好上手,难道是我错觉吗?有兴趣的同学可以自行尝试!