赞
踩
目录
3.bmp:browsermob-proxy(可利用har文件自动生成测试用例)
1.embedded mode(嵌入式,适合自己开发工具使用)
1.embedded模式下自动生成har文件,并生成测试用例的实现
3.启动服务:browsermob-proxy -port 9000(此端口只是bmp服务的端口,非监听端口)
7.请求操作完毕后访问:http://127.0.0.1:9000/proxy/8081/har
常用mock工具:charles,fiddler,moco,wiremock,browsermod-proxy,mitmproxy.
GitHub - dreamhead/moco: Easy Setup Stub Server
- [
- {
- "response" :
- {
- "text" : "Hello, Moco"
- }
- },
-
- {
- "request":
- {
- "uri" : "/foo"
- },
- "response":
- {
- "text" : "chuntian_tester的测试!"
- }
- }
- ]
java -jar .\moco-runner-1.2.0-standalone.jar http -p 12306 -c .\moco.json
_____________________________________________________________________________
https://github.com/wiremock/wiremock-->Getting Started - WireMock
- <dependency>
- <groupId>com.github.tomakehurst</groupId>
- <artifactId>wiremock-jre8</artifactId>
- <version>2.31.0</version>
- <scope>test</scope>
- </dependency>
- package ServiceTest;
-
- import com.github.tomakehurst.wiremock.WireMockServer;
- import org.junit.jupiter.api.BeforeAll;
- import org.junit.jupiter.api.Test;
-
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
-
- import static com.github.tomakehurst.wiremock.client.WireMock.*;
- import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
-
- public class StubTest {
-
- private static WireMockServer wireMockServer;
-
- @BeforeAll
- static void beforeAll() {
- //beforeall中先启动wiremockserver,服务占用8089端口
- wireMockServer = new WireMockServer(wireMockConfig().port(8089));
- //启动服务
- wireMockServer.start();
- //配置端口,上面已配置,此处可省略......(此处可有可无)
- configureFor("localhost", 8089);
- }
-
-
- @Test
- void stub() throws InterruptedException {
- //stubFor这个API:需要一个stub服务,发送请求时如果url中匹配到包含"/user/d",则给我返回我下面特定的内容,
- stubFor(get(urlEqualTo("/user/d"))
- // .withHeader("Accept", equalTo("text/xml"))
- //返回特定的aResponse().
- .willReturn(aResponse()
- .withStatus(200)
- .withHeader("Content-Type", "text/xml")
- .withBody("<response>d info</response>")));
-
- //todo: use
- Thread.sleep(300000);
- }
- }
运行用例stub,会启动wiremock服务
浏览器访问该服务,匹配到规则时就能返回想要的内容.
curl命令发起网络请求:也会返回response
可返回多个数据设置:
服务开启后的前10s内访问返回:"mock on stub"
服务开启后的前10s内访问返回:"exception"
代码实现:
- //代理服务
- public class StubTest {
- private static WireMockServer wireMockServer;
- @BeforeAll
- static void beforeAll() {
- //beforeall中先启动wiremockserver,服务占用8089端口
- wireMockServer = new WireMockServer(wireMockConfig().port(8089));
- //启动服务
- wireMockServer.start();
- //配置端口,上面已配置,此处可省略......(此处可有可无)
- configureFor("localhost", 8089);
- }
-
- //设置代理服务,类似charles中的map_remote技术.BeforeAll中的服务启动起来后会占用8089端口,任何访问这个端口的请求都会被代理到另一个地址.
- @Test
- void proxy() throws InterruptedException {
- stubFor(
- //请求任何地址都会代理到proxiedFrom中的地址.
- get(urlMatching(".*"))
- .atPriority(10)
- .willReturn(aResponse().proxiedFrom("https://www.baidu.com")));
- Thread.sleep(100000);
- }
- }
启动服务后访问服务所在主机的8089端口,被代理到了百度
_____________________________________________________________________________
类似python的mitmproxy.
- <dependency>
- <groupId>net.lightbody.bmp</groupId>
- <artifactId>browsermob-core</artifactId>
- <version>2.1.5</version>
- <scope>test</scope>
- </dependency>
- public class mockBmpOnProxyTest {
-
-
- private BrowserMobProxy proxy;
-
- @BeforeAll
- void beforeall(){
- //代理服务的初始化
- //根据官方文档介绍引用官方代码
- BrowserMobProxy proxy = new BrowserMobProxyServer();
- //bmp 服务开启监听8083端口
- proxy.start(8083);
- }
-
- @BeforeEach
- void beforeeach(){
- //每条用例操作执行前创建har文件
- //与每个操作绑定.如下面的click测试
- proxy.newHar("click");
- }
-
- @AfterEach
- void aftereach(){
- //每条用例操作执行完后生成har文件中的数据
- proxy.endHar();
- }
-
- @Test
- void click(){
- //自动化测试的各个操作,对各对象的操作将记录下对接口的请求,存在har文件中.
- }
- }
1.代码实现:
- public class mockBmpOnProxyTest {
-
- //修改response
- @Test
- void mockOnProxy() throws InterruptedException {
- // //根据官方文档介绍引用官方代码
- BrowserMobProxy proxy = new BrowserMobProxyServer();
- //bmp 服务开启监听8083端口
- proxy.start(8083);
- proxy.addResponseFilter(new ResponseFilter() {
- @Override
- public void filterResponse(HttpResponse httpResponse, HttpMessageContents httpMessageContents, HttpMessageInfo httpMessageInfo) {
- httpMessageContents.setTextContents(httpMessageContents.getTextContents().replace("DMS后台管理系统", "DMS后管mock"));
- }
- });
- Thread.sleep(100000);
- }
- }
2.启动服务后指定代理访问
3.电脑指定代理后访问也会相应修改
[~]$ curl -X PUT -d 'initialPageRef=Foo' http://localhost:9000/proxy/8081/har
或者
[~]$ curl http://localhost:9000/proxy/8081/har
有了以上har文件就可以生成测试用例了.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。