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