How to get app package of running android app?

Hello,

I am able to launch my android app on emulator and would like to get the app package to use it as a variable.
How can I get this information programatically?

Thanks

For your info, in case someone needs it, I couldn’t find other solution than looking in the page source and using a global variable to use it anywhere:

def getPackage(){
   AppiumDriver driver = MobileDriverFactory.getDriver()
   AndroidDriver androidDriver = (AndroidDriver) driver
   def pageSource = androidDriver.getPageSource()
   String pattern = "package=\"(\\S*)\" "
   Pattern r = Pattern.compile(pattern)
   Matcher m = r.matcher(pageSource)
   if (m.find()){
      GlobalVariable.G_Package = m.group(1)
   }
}

Hi quentin,

Can I ask how do you use that function in your test script?

Regards

Hello,

I have defined it in a groovy file on Keyword part, with @Keyword tag.
Then on my Test case I call it with: (script view)

if (MobileDriverFactory.getDeviceOS().equals("Android")){
	CustomKeywords.'packagename.getPackage'()
}

Hi quentin,

Thanks for your information. Just curious how can you use it in ‘Start Application’ keyword because you need to start an application on android emulator, right?

Regards

I am using it just to find test object in mobile android because I run my tests on multiple enivronment and IDs are the same but packages are different. As we cannot use id tag but resource-id, I need to know the package