Xamarin: how can I provide the backend URL to my app during UI tests?

Hello,

So I have this application made with Xamarin which is actually a client for a backend application. During UI tests I need to provide the backend URL to the application that is installed in the Android/iOS device to make it works.

Fortunately the URL is deterministic so I can make -for example- a script step that would calculate it for me.

My question is: how do I set this information in the code?

Currently the application is using a hardcoded string constant that has this URL. I thought about using environment variable and a static property but that means it will get the value at execution time and I don’t think that’s what I want. What I think I need is to be able to build the application using the URL I will calculate somewhere in the workflow.

Is it the correct approach? How can I achieve this?

Best

Hi, in our xamarin ios uitest sample project, we refer to env vars exposed during build time like this: https://github.com/bitrise-samples/sample-apps-xamarin-ios/blob/master/CreditCardValidator.iOS.UITests/Tests.cs#L13

If your backend url is always the same for tests, just define the URL as a secret or App env var in the workflow editor.

If you calculate the URL during the build, use a script step to calculate the URL, and expose the URL, using envman: https://github.com/bitrise-io/envman#usage-example-simple-bash-example, to be accessible for subsequent steps.

Sorry I didn’t express myself properly.

During build time the tests are ran so it’s logical that the environment variable is accessible in the tests.

My question is about the application that is installed on the virtual device itself. Because that’s the one that is going to use the URL to the backend.

We have a Config.cs file with some constants for the application. I’ve seen that we’re currently replacing the URL in place in that file (modifying the code). I don’t know if it’s a good practice. It seems a bit hacky in my opinion.

Do you have the prod one hardcoded there?

Always depends on the project of course but in general the solution you mentioned, to include it in a code file should be a reasonably simple to maintain one.

Environment Variables which you use on the device when the app is running is trickier, as you have to define that for the “emulator/simulator” (the device). The env vars that are defined on the host (macOS in this case) are NOT available to the emulator/simulator unless you set them on the device before running the app. Definitely possible but I’d say significantly more complex than injecting into the code and possibly harder to debug.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.