tldr; for all the geeks;
AWS Device Farm expects to not have certain device specific
desired capabilitiesin the testcases, however we would like to have them for our local test cases. For those who hate changing code every time while switching between local and AWS, we can use the Environment variable trick to let the code figure out the environment and add/remove configs appropriately.
The same utility can be used to change code behaviour, directory to capture screen shots etc.
AWS Device farm is a cloud infrastructure mobile testing platform, that eases up our multi device app testing by running our testcases over Amazon’s physical devices and providing us the results, screen shots, logs and performance metrics of our testcases. It certainly cuts up the dev cost on building an in-house test stack.
AWS Device farm test cases expect us to remove the device specific details from the testcases, because AWS would be injecting the details for the devices that we choose from the cloud devices. Check this comment in AWS code sample:
So we should have these values set for our local dev code, but not set for the AWS Device farm code.
Creating a check for environment variable
The core of the trick is this small code snippet that figures out the environment based on if a particular environment variable is set. We will set this variable only for our local dev and it won’t be present on the AWS stack, hence is a good candidate for using it as a distinguisher.
Now all our test cases can start using this utility function to check if its a loca dev env, and set the desired capabilities only if its a local dev setup like this:
How to set the environment variable
If you’re running the testcases from terminal you could setup the environment variable by the command:
$ EXPORT LOCAL_DEV_ENV=1
If you’re on an IDE, you can set the Env variable via the `Edit Run Configurations` as below:
That’s all we would need to change today. Below is the entire test case that should run fine on both local dev and AWS Device farm with absolutely no code changes.
- I have hard coded the device details here in test case, but ideally you would be reading it from a config file
- I have imported all the utility functions in the testcase itself. They should all be in an external re-usable utility script.
That’s for this small post. The same utility function can be used to:
- add and remove device configs differently
- change test behaviour / actions
- change the directory for capturing app screen shots, etc
Hope this small trick is helpful, and saves few minutes of fellow dev and QA mates. Cheers.
Checkout my portfolio here: https://confusedcoders.com/nikita-sharma-greenhorn-data-science-student
I am a greenhorn Data Science student with interest in finding patterns in data. My language of choice is Python and I am starting to get my hands dirty with R.
I blog on Medium.com  and ConfusedCoders.com . I share my code on Github.com .