Jenkins: how do you get deviceId using the Android Emulator Plugin

Now I have a Test Suites running on Jenkins, how do I get deviceId?

Thinks.

Hi @roy.zheng,

For me, the Android emulator device id, you can run the command adb devices, which will return you a list of the running emulators.

The device id seems to be emulator- plus the port number the emulator is running on. For me, it’s always running on port 5554, so my device id is always emulator-5554.

Hope this helps,

Chris

1 Like

thank you @Chris_Trevarthen .
I try, but it prompts me “No mobile device with name ‘emulator-5786’ available”.

In addition, I also want to know how to make the emulator port always 5786

Hi @roy.zheng,

That does seem weird that the device is created but then can’t be found. Could you share the Android Emulator Plugin settings you’re using?

As for using a specific port, the Android Emulator Plugin docs say that it will be a random port every time. However, you might be able to use the Jenkins Port Allocator Plugin to set a value for the port:

https://wiki.jenkins.io/display/JENKINS/Port+Allocator+Plugin

Hope this helps,

Chris

thank you @Chris_Trevarthen

Hi @roy.zheng,

Are you able to turn on the “Show emulator window” setting? I’m wondering if Katalon running on CI is having a problem because there is no graphical representation of the device.

– Chris

thank you @Chris_Trevarthen ,
I turn on the Show emulator window, but the emulator does not work properly.

Hi @roy.zheng,

That makes sense if there’s no display hooked up to the build node.

I’ve also seen similar issues to what you’re seeing when I didn’t run the startApplication() command at the beginning of the test. Could you share the code of the first test in your test suite?

– Chris

@Chris_Trevarthen Of course,
StartApplication () has the wrong appFile and I know it.

Mobile.startApplication('C:\\Users\\User\\Desktop\\debug0410.apk', false)
def timeout = GlobalVariable.g_Mobile_TimeOut
' need to log in ?'
if(Mobile.waitForElementPresent(findTestObject('android_V1/Login/Button_Sign in'), GlobalVariable.g_waitForElementTime)){
	def g_ServerAddress = GlobalVariable.g_ServerAddress

	'server is correct? or Switch servers'
	if (Mobile.getAttribute(findTestObject('android_V1/Login/Text_ServerName'), 'text', timeout) != g_ServerAddress) {
	    Mobile.tap(findTestObject('android_V1/Login/Text_ServerName'), timeout)
		def serverAddress = GlobalVariable.g_ServerAddress
	
	    Mobile.tap(findTestObject('android_V1/Login/CheckedText_serverAddress',[('serverAddress'):serverAddress]), timeout)
	
	    Mobile.tap(findTestObject('android_V1/Login/button_ok'), timeout)
	}
		
	Mobile.setText(findTestObject('android_V1/Login/EditText_Account'), GlobalVariable.g_username, timeout)
	
	Mobile.setText(findTestObject('android_V1/Login/EditText_Password'), GlobalVariable.g_password, timeout)
	
	Mobile.tap(findTestObject('android_V1/Login/Button_Sign in'), timeout)

}

Hi @roy.zheng,

Ah yes, if you’re not running startApplication() at all or running it with a non-existent .apk file, I’m pretty sure that you get the exact error you’re seeing. It definitely isn’t very obvious about what’s really going on.

So, if you point to an actual .apk located on the Jenkins build node, in the startApplication() call, I bet you’ll get further in your test.

Also check out this post that can help you avoid hardcoding the .apk file path in your scripts so it’ll be easier to run on Jenkins:

Hope this helps,

Chris

thank you @Chris_Trevarthen
I think reading your article later will be very helpful to me, but I want to solve the problem first.
I tried to make the.apk file exist, but the emulator still doesn’t work

Hi @roy.zheng,

That startApplication code in your test looks right. It’s odd that it’s still failing. I assume that you turned off the checkbox for “Show emulator window” again?

The only other thing that comes to mind is that you might need to move the “startApplication” into the Test Suite itself. So taking it out of the Test Case file and having it as the setup step of the Test Suite script.

—Chris

thank you @Chris_Trevarthen,
I turned on the “Show emulator window”.
Move the “startApplication" in the test suite itself?How to?

Hi @roy.zheng,

First, let’t try turning off the “Show emulator window” again, because I don’t think your Jenkins node has a display on it and that could cause the emulator not to start. After you’ve done that, try running the tests again, making sure you have the startApplication() function using the correct path to the file: /data/jenkins/workspace/Mobile_V1/debug0410.apk.

If that doesn’t work, my suggestion to put the startApplication() function in the Test Suite would mean:

  • Open the Test Suites/mobile_test file in Katalon
  • Switch to the “Script” view tab at the bottom
  • In the function for @SetUp, change it to the following:
    @SetUp(skipped = false) // Please change skipped to be false to activate this method.
    def setUp() {
        Mobile.startApplication('/data/jenkins/workspace/Mobile_V1/debug0410.apk', false)
    }
    
  • This will start the mobile app once before the whole suite starts, not for each test
  • Remove the startApplication() line from your Test Case file
  • Rerun your test on Jenkins

This will mean that you can’t run the individual Test Cases any more - you’ll need to run them through a Test Suite. There are ways around this, but for right now, we should just see if we can get the Test Suite working.

Hope this helps,

Chris

hi @Chris_Trevarthen or @roy.zheng , can you help to how give the static value of emulaor port number?
for example emulator-5556 will be the name for every build.

I already explore about port allocatiot but still not find the solusion about that.
since I need to parsing the emulator name to deviceID value in katalon CMD execution.

really approciate if you can help :pray:

I’m now using Remote devices (like BrowserStack) to execute Android test cases

Hi @rswitch,

If you’re looking to specify the emulator port when you’re starting the emulator, you might be able to follow these steps:

If you already have an emulator running, you should be able to get the emulator name using this command on the command line:

adb devices

Hope this helps,

Chris

1 Like

I am facing the same issue as @roy.zheng and still couldn’t find a way to work with the emulator.
Any leads would be really helpful?
My emulator always starts on a different port and the port plugin you mentioned here is descoped and there is no way i could pass a device id in this case which matches the random port.