-
Notifications
You must be signed in to change notification settings - Fork 670
Description
Hi, I try to use browsermob-proxy for get the API calls in Android device, using Java maven with Appiun, selenium and Junit.
this is my pom.xml file dependencies:
net.lightbody.bmp
browsermob-core
2.1.5
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.9.0</version> <!-- Update to the latest stable version -->
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>8.3.0</version> <!-- Update to the latest stable version -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version> <!-- Update to the latest stable version -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.0</version> <!-- Update to the latest stable version -->
<scope>test</scope>
</dependency>
</dependencies>
I tried a lotssss, this is my current code:
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
public class AppiumWithBrowserMobProxy {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
// Start BrowserMob Proxy on a random port
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true); // Trust all SSL certificates
proxy.start(0); // 0 means choose any free port
// Set up Selenium proxy
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// Enable more detailed HAR capture
proxy.enableHarCaptureTypes(
net.lightbody.bmp.proxy.CaptureType.REQUEST_CONTENT,
net.lightbody.bmp.proxy.CaptureType.RESPONSE_CONTENT);
// Start capturing network traffic before launching the app - because API calls are generated when the application loads
proxy.newHar("test");
// Set up DesiredCapabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "device_name");
capabilities.setCapability("appPackage", "appPackage");
capabilities.setCapability("appActivity", "appActivity");
capabilities.setCapability(MobileCapabilityType.PROXY, seleniumProxy);
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);
// Start Appium session
AndroidDriver driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
// Initialize the WebDriverWait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
// Wait for the element to be visible and then click it
WebElement showButton = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(@resource-id, 'show_button')]"))
);
showButton.click();
// Wait for API calls to complete
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Stop capturing network traffic after API calls are made
Har har = proxy.getHar();
// Save HAR data to a JSON file
String harFilePath = "path_to\\har_appium_output.har";
saveHarToJsonFile(har, harFilePath);
// Stop BrowserMob Proxy
proxy.stop();
// Quit Appium session
driver.quit();
}
private static void saveHarToJsonFile(Har har, String filePath) {
try (FileWriter fileWriter = new FileWriter(filePath)) {
har.writeTo(fileWriter);
System.out.println("HAR data saved to: " + filePath);
} catch (IOException e) {
System.err.println("Error saving HAR data: " + e.getMessage());
}
}
}
but after the run finish successfully, I see the har file created but this is the only content in the output har file:
{"log":{"version":"1.2","creator":{"name":"BrowserMob Proxy","version":"2.1.5","comment":""},"pages":[{"id":"test","startedDateTime":"2024-07-01T08:19:35.365Z","title":"test","pageTimings":{"comment":""},"comment":""}],"entries":[],"comment":""}}
no API calls are there, but the application sure generated API calls when the application loads.
the Browsermod works me well in web, but with mobile I have this issue - not see the API calls in the output har file.
can help me Pls?
thanks.