简体   繁体   中英

Read api calls of app with Appium

we are experimenting with Appium for our mobile testing. I couldn't find how to read the API calls our app does towards our backend. 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?

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. 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.

2) Another way is through browsermob proxy. 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:

    <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

 server.newHar("2.har");

In your @AfterSuite add below:

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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