Tuesday 9 March 2021

How to kill Appium server if device is not connected | How to find connected Android device

Subscribe to my youtube channel for more videos here:

Problem:

If the Android device was not connected during Appium Automation, it was starting the Appium server but was not able to quit it.
So, once running the script again after connecting the device , we will get error

Could not start Appium server. It's showing [Error Starting appium server: listen EADDRINUSE 0.0.0.0:4723]

Below is the script which I created to handle the situation and check if the device is already connected.
If it is not connected just Quit the Appium Server.

Solution:

if (loadPropertyFileAndroid.startsWith("Android_")) {

try {
Process process = Runtime.getRuntime().exec("adb devices");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;

Pattern pattern = Pattern.compile("^([a-zA-Z0-9\\-]+)(\\s+)(device)");
Matcher matcher;

while ((line = in.readLine()) != null) {
if (line.matches(pattern.pattern())) {
matcher = pattern.matcher(line);
if (matcher.find()) {
System.out.println("android device connected");

AppiumServer.start();
CommonUtils.loadAndriodConfProp(loadPropertyFileAndroid);
log.info(loadPropertyFileAndroid + " properties file loaded !!!");
CommonUtils.setAndroidCapabilities();
driver = CommonUtils.getAndroidDriver();
String connectedDeviceName = driver.getSessionDetail("deviceName").toString();
System.out.print("Saif "+connectedDeviceName);

log.info("Appium server started");
}else{
AppiumServer.stop();

log.info("Appium server not started");
}

}
}
} catch (IOException e) {
e.printStackTrace();
}
Read about automation coding standards here: Avoid Mobile Automation Coding Pitfalls : Automation coding standards

No comments:

Post a Comment

Interview Experience with AMAZON for the role of Quality Assurance Tester

This role was for Digital/ IoT/Mobile Application based testing :)  Amazon is a dream company and everyone wants to work for that company a...

Popular Posts