RangerRick
RangerRick

Reputation: 296

Can Xamarin apps use App Live Local Testing on BrowserStack

I've developed a cross platform mobile app with Xamarin.Forms. I have local services that my app hits. My Xamarin app defines the HTTPClient as shown below. When testing on BrowserStack's App Live product I can not hit my local services from my app using the BrowserStack Local app (I receive a "No Such Host Is Known" response) . I can hit my services using the device's default browser.

HTTPClient setup in Xamarin App:

public static readonly HttpClient client = new HttpClient()

My local services have internal domain names (it's not simply 'localhost:443' but it actually has a domain name like 'customservice.com')

Is it possible for a Xamarin App to use the BrowserStack App Live product while using Local Testing? If so, how?

Upvotes: 1

Views: 1126

Answers (2)

RangerRick
RangerRick

Reputation: 296

fine, I'll do it myself

I found a solution that works for Android and should work for iOS (but doesn't). Maybe with some tinkering it could work for iOS too, but I thought I'd share what I found:

Step 1: Create a Dependency Service

This step is loosely based on this article. The article is incomplete because it does not create a dependency service. So that's fun

  1. In the Shared Project create a IProxyInfoProvider interface as shown:
public interface IProxyInfoProvider
{
   WebProxy GetProxySettings();
}
  1. Create the platform implementations: (Don't forget to add the assembly tag above your namespace to export the Dependency Service!)

iOS

public class ProxyInfoProvider : IProxyInfoProvider
{
    public WebProxy GetProxySettings()
    {
        var systemProxySettings = CFNetwork.GetSystemProxySettings();

        var proxyPort = systemProxySettings.HTTPPort;
        var proxyHost = systemProxySettings.HTTPProxy;
        Console.WriteLine("Proxy Port: " + proxyPort.ToString());
        Console.WriteLine("Proxy Host: " + Convert.ToInt64(proxyHost));

        return !string.IsNullOrEmpty(proxyHost) && proxyPort != 0
            ? new WebProxy(proxyHost, proxyPort)
            : null;
    }
}

Android

public class ProxyInfoProvider : IProxyInfoProvider
{
    public WebProxy GetProxySettings()
    {
        var proxyHost = JavaSystem.GetProperty("http.proxyHost");
        var proxyPort = JavaSystem.GetProperty("http.proxyPort");

        Console.WriteLine("Proxy Host: " + proxyHost);
        Console.WriteLine("Proxy Port: " + proxyPort);

        return !string.IsNullOrEmpty(proxyHost) && !string.IsNullOrEmpty(proxyPort)
            ? new WebProxy($"{proxyHost}:{proxyPort}")
            : null;
    }
}

Step 2: Update your HttpClientHandler

  1. We want to consume the WebProxy that is now being returned from the dependency service. Update your HttpClient handler so it looks something like this:
var _handler = new HttpClientHandler();
_handler.Proxy = DependencyService.Get<IProxyInfoProvider>().GetProxySettings();

Ensure that your HttpClient is consuming this Handler in it's constructor like: new HttpClient(_handler.Value)

Step 3: Upload your app to BrowserStack

  1. Boom. Then Android works with BrowserStack local testing! Why doesn't it work on iOS you ask? Good question. I'm still trying to figure that part out...
  2. What is also weird is that if you use a proxy to debug your app in your local environment, this solution works! But it stops working when you put your app in BrowserStack.

Upvotes: 1

singh_mehar
singh_mehar

Reputation: 1

Since you’re able to access your internal domain via their device browser, it seems that your application is not able to route traffic via BrowserStack’s Local Testing infrastructure.

If that is the case, you can reach out to the BrowserStack Support team for further assistance.

Upvotes: 0

Related Questions