How to pass custom headers with Appium mobile tests on LambdaTest?

How to pass custom headers with Appium mobile tests on LambdaTest?

Hey Emma,

You can add custom headers using LambdaTest’s customHeaders capability.

Shown below is a snippet where we are changing the user-agent via customHeaders capability.

customHeaders.put("user-agent", "Mozilla/5.0 (Linux; Android 14.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Mobile Safari/537.36");
ltOptions.put("customHeaders", customHeaders);

This is the complete setup given below for better understanding on how you can add custom header:

DesiredCapabilities capabilities = new DesiredCapabilities();
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("w3c", true);
ltOptions.put("deviceName", "Galaxy.*");
ltOptions.put("platformName", "android");
ltOptions.put("platformVersion", "13");
ltOptions.put("isRealMobile", true);
ltOptions.put("network", true);
ltOptions.put("build", "Java Selenium 4");
ltOptions.put("name", "Sample Test Java");
ltOptions.put("network", true); //PLEASE ADD THIS CAPABILITY AS WELL

Map<String, String> customHeaders = new HashMap<>();
customHeaders.put("user-agent", "Mozilla/5.0 (Linux; Android 14.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Mobile Safari/537.36");
ltOptions.put("customHeaders", customHeaders);

capabilities.setCapability("LT:Options", ltOptions);

driver = new AndroidDriver(new URL("https://username:access_key@mobile-hub.lambdatest.com/wd/hub"), capabilities);

driver.get("https://useragentstring.com/");

For additional details, refer to the LambdaTest Appium Custom Header support documentation.

If you have any questions or need further clarification, feel free to reach out.

Happy Testing! :tada: