The app that I have started working on is having different appPackage name for different environments (QA, staging, prod etc). I want to fetch that information from the apk file (during the execution) and then use the respective appPackage name in all my find element calls (URLs).
Could you please shed some light on how can I achieve it.
Try the getCapabilities() method available in Appiumâs drivers. You can then check if the returned Capabilities object contains an âappPackageâ key.
Thanks for response but for me this returns ânullâ for appPackage as well as appActivity. Though I am able to get platformName using same approach. Here is what I am trying:
I think I see two problems here â if Iâm wrong, please forgive my misinterpretation
You need to set the appPackage capability when you start Appium. From the description above, you may not be setting that capability â that would describe the return of ânullâ from capabilities
Get the appPackage for whatever is running on the device at that moment.
Alternatively, if you just want to get the app package name of a running application, you can do this with calls to adb. You can try using appium-adb (https://github.com/appium/appium-adb), but I found it difficult to use. We created our own adb interface which works because we are only testing locally and not against selenium grids or the like.
To get the app package, you need to run the command âadb shell dumpsys window windowsâ We specifically look for the two lines which contain either âmCurrentFocusâ or âmFocusedAppâ because the donât always agree. Some heuristics are required to decide which one is correct.
I think my question is little confusing (Sorry about that) but I do not want to set the appPackage in the capablities. That was the whole point; I want to fetch that information from the APK file while executing my scripts and then use it in my find element calls as prefix for resource ID.
Actually I was assuming that we can get that information from the APK file but it looks like I can only fetch the capability which I define initially under appium capabilities.
I am aware about |grep package command but that is also a manual task unless I create a bash file (I am on mac) to execute it and then somehow fetch the information from there.
Is there a reason you canât execute a system call to aapt? Iâm assuming you know which apk file is being used. Given your description, thereâs no reason to fetch this info from the capabilities.
Be sure to add null checks as needed (e.g. check packageCap. I wanted to get the point across without making the code too messy)
There is no intermediate âdesiredâ map capability (or at least there shouldnât be). You can verify this by using a debugger to set a breakpoint right after retrieving the driverâs capabilities, and exploring the values stored in the Capabilities object (or, simply iterate through the available keys in caps and just print out the values).