Challenges in Appium Server 2.x Service Instantiation

     We know that most of them are in the migration stage of Appium 1.x to Appium 2.x. There are some changes in the Appium server instantiation logic when we use the Selenium 4.x and Appium Server 2.x. Starting the Appium server’s service using AppiumDriverLocalService and we discussed different driver services in the previous article. In this article, I would like to share the problems and solutions which I faced while instantiating the Appium Server 2.x programmatically.

Problem 1:

     I have upgraded Appium Server 1.x to 2.x, updated Selenium 3.x to 4.x and java-client is 7.6.0. The first problem is No route found for /wd/hub/session start appium 2 server programmatically.

Solution:

     Because Appium 2.x does not have /wd/hub. We can run Appium 2.x with flags -pa /wd/hub or -base-path or provide http://127.0.0.1:4723/ instead of http://127.0.0.1:4723/wd/hub in the client code. Here I have added a flag like withArgument(() -> “-pa”, “/wd/hub”). Below is the complete code snippet:

AppiumDriverLocalService service = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder().usingPort(getPort()).withArgument(() -> "-pa", "/wd/hub")
.withLogFile(new File(System.getProperty("user.dir") + "\Logs\Appium_logs\appiumLogs_"+getCurrentDateAndTime()+".txt")));
service.start();
Problem 2:

     Once the server problem was solved; got another error when using Appium Server 2.x, Selenium 4.x and java-client 7.6.0. The error saying io.appium.java_client.service.local.AppiumServiceBuilder does not define or inherit an implementation of the resolved method ‘abstract java.util.List createArgs()’.

Solution:

     We can use Selenium version < 4.x (most recommended 3.x version) or java-client version > 7.6.0 that is upgrade java-client to 8.x. I have upgraded the java-client version to 8.x and the error are gone.

     Below is the complete code snippet to work with Appium Server 2.x, Selenium 4.x and java-client 8.x. I have used one of the demo Android applications:

AndroidDriver driver = null;
		
AppiumDriverLocalService service = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder().usingPort(getPort()).withArgument(() -> "-pa", "/wd/hub")
.withLogFile(new File(System.getProperty("user.dir") +"\\Logs\\Appium_logs\\appiumLogs_"
+ getCurrentDateAndTime() + ".txt")));
service.start();

DesiredCapabilities capabilities = new DesiredCapabilities();
if (service.isRunning()) {
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "One Plus");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "11");
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);	    capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, true);
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 180);
capabilities.setCapability(MobileCapabilityType.UDID, "your_device_udid");
capabilities.setCapability(MobileCapabilityType.APP, "your_application_path");
capabilities.setCapability(MobileCapabilityType.NO_RESET, true);
driver = new AndroidDriver(service.getUrl(), capabilities);
}

//To get available port dynamically that used at usingPort()
private static int getPort() throws Exception {
 int port = 0;
 try {
  ServerSocket socket = new ServerSocket(0);
  socket.setReuseAddress(true);
  port = socket.getLocalPort();
  socket.close();
 } catch (Exception e) {
  e.printStackTrace();
 }
 return port;
}

//To get the timestamp that suffix to the appium log file
private static String getCurrentDateAndTime() {
 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 DateFormat timeFormat = new SimpleDateFormat("HH-mm-ss");
 Date date = new Date();
 String currdate = dateFormat.format(date);
 String currtime = timeFormat.format(date);
 return currdate + "_" + currtime;
}

     I hope you really enjoyed reading this article and got some ideas for your mobile application automation using Appium 2.x. Try to implement the above logic in your traditional automation script or in custom automation frameworks.

make it perfect!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: