简体   繁体   English

用Appium阅读app的api调用

[英]Read api calls of app with Appium

we are experimenting with Appium for our mobile testing. 我们正在尝试使用Appium进行移动测试。 I couldn't find how to read the API calls our app does towards our backend. 我找不到如何阅读我们的应用程序对我们后端的API调用。 Is it possible to 'listen' to the network calls of the app and for example read the JSON body that is returned from our backend? 是否可以“监听”应用程序的网络调用,例如读取从后端返回的JSON正文?

Thanks 谢谢

There are two possible ways of recording the network calls: 录制网络呼叫有两种可能的方法:

1) Set up proxy in your mobile via any of the proxy tool you use like charles, wireshark. 1)通过您使用的任何代理工具(如charles,wireshark)在您的手机中设置代理。 What I mean is manually open whatever GUI tool you have and route your traffic via the tool as when you want to listen to network traffic. 我的意思是手动打开您拥有的任何GUI工具,并通过该工具路由您的流量,就像您想要收听网络流量一样。

2) Another way is through browsermob proxy. 2)另一种方法是通过browsermob代理。 This would generate a HAR file of all the network calls that were made(would give the headers of the response and not JSON data).Maven Dependency is: 这将生成所有网络调用的HAR文件(将给出响应的头部而不是JSON数据).Maven Dependency是:

    <dependency>
        <groupId>net.lightbody.bmp</groupId>
        <artifactId>browsermob-core-littleproxy</artifactId>
        <version>2.1.0-beta-3</version>
    </dependency>

Add this where you create your environment: 在创建环境的地方添加:

// Starting server BrowserMobProxy

    server= new BrowserMobProxyServer();
    server.setConnectTimeout(10, TimeUnit.SECONDS);
    server.start(8897);
    Proxy proxy = ClientUtil.createSeleniumProxy(server);

Set capabilities: 设置功能:

 capabilities.setCapability(CapabilityType.PROXY, proxy);

After the driver is set up, create a HAR file 设置驱动程序后,创建一个HAR文件

 server.newHar("2.har");

In your @AfterSuite add below: 在你的@AfterSuite中添加如下:

 if(server.getHar()==null){
        System.out.println("No Har capture");
    }
    Har har = server.getHar();
    if(har==null){
        System.out.println("Har is NULL");
    }

    FileOutputStream fos = new FileOutputStream(FILE_OUTPUT_HAR+"fos"+".har");
    har.writeTo(fos);



    HarLog log = har.getLog();
    if(log==null){
        System.out.println("Harlog is null");
    }
    List<HarEntry> entries = new CopyOnWriteArrayList<HarEntry>(log.getEntries());
            System.out.println("entries"+entries);
            for (HarEntry entry : entries){
                System.out.println("entry="+entry.getRequest().getUrl());
            }

    File harFile = new File(HAR_FILE_PATH+"2"+".har");
    File("/Users/yourpath/"+"2"+".har");        
    har.writeTo(harFile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM