1. Home
  2. AstroFarm
  3. How to use Appium with AstroFarm using APIs?

How to use Appium with AstroFarm using APIs?

KB ID: 42G2108656
Total Views: 107
Last Updated: August 2021

Appium is a cross-platform, automation tool used for automating applications on test devices.
AstroFarm users can now simulate the automation on the devices remotely. The device configuration can be done with the help of API exposed by AstroFarm to configure the remote device.

Please follow the below-mentioned steps to achieve this. 

  1. Log into the AstroFarm console.  
  2. Go to Settings on the AstroFarm console and navigate to the Keys tab. Click on the ‘+’ option to generate an access token in the Access Token tab.  

3. Once done, copy and save the Access Token.
4. Further, we will need to fetch the Serial Number of the device. We can use the below API for this. Please replace the below API with the URL and the Access Token generated in Step 2.

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url('https://myfarm-astrofarm-us.42gears.com/api/v1/devices') 
  .get()
  .addHeader('authorization', 'Bearer YOUR_ACCESS_TOKEN')
  .build();

Response response = client.newCall(request).execute();

5. To proceed and use the device, please use the below API. Ensure to add the Access Token and Device Serial Number from Step 2 and Step 4. Also, replace the URL

OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse('application/json');
RequestBody body = RequestBody.create(mediaType, '{serial':'DEVICE_SERIAL'}');
Request request = new Request.Builder()
  .url('https://myfarm-astrofarm-us.42gears.com/api/v1/user/devices')
  .post(body)
  .addHeader('authorization', 'Bearer YOUR_ACCESS_TOKEN')
  .addHeader('content-type', 'application/json')
  .build();

Response response = client.newCall(request).execute();

6. The next step will be to retrieve the Remote Debug URL. Below API will be useful. Ensure to add the Access Token and Device Serial Number from Step 2 and Step 4. Also, replace the URL

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url('https://myfarm-astrofarm-us.42gears.com/api/v1/user/devices/DEVICE_SERIAL/remoteConnect')
  .post(null)
  .addHeader('authorization', 'Bearer YOUR_ACCESS_TOKEN')
  .build();

Response response = client.newCall(request).execute();

ResponseBody bodytext = response.body();

Note: The user needs to add an ADB key to AstroFarm before using the above API, as the device might be in an unauthorized state after connecting to it for the first time. Users can add the ADB key from the Settings section of AstroFarm.

7. To connect the device to ADB, use the Remote debug URL generated in Step 6 below command. 

Runtime.getRuntime().exec("adb connect remotedebugURL”)

8. After connecting the device through ADB, the user will need to fetch the Device name from the ADB device list using the below command. And then the user has to pass that device name in Appium desired capabilities.

Runtime.getRuntime().exec("adb devices”)

9. Use the Device Name generated in Step 8 in the below command for capabilities required for Appium configuration. 

DesiredCapabilities caps = new DesiredCapabilities();
  caps.setCapability("deviceName", "DeviceName from adb device list");
  caps.setCapability("platformName", "Android");
  caps.setCapability("appPackage", appPackage);
  caps.setCapability("appActivity", activity);
  caps.setCapability("adbExecTimeout",100000);
  caps.setCapability("uiautomator2ServerInstallTimeout",20000);
	  caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT,20000);
  caps.setCapability("testdroid_testTimeout", 20000);
  caps.setCapability("noReset", “true”);
  caps.setCapability("fullReset", “false”);
 driverAppium=new AndroidDriver<WebElement>(new URL(“http://127.0.0.1:4723/wd/hub”),caps);

10. After configuring the Appium method, users can run the automation on devices using AstroFarm

11. Further, if the user wants to free up the device, the below command will be helpful. Ensure to add the Access Token and Device Serial Number from Step 2 and Step 4. Also, replace the URL. 

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url('https://myfarm-astrofarm-us.42gears.com/api/v1/user/devices/DEVICE_SERIAL')
  .delete(null)
  .addHeader('authorization', 'Bearer YOUR_ACCESS_TOKEN')
  .build();

Response response = client.newCall(request).execute();

12. Users can disconnect the remote debug session using the below command. Ensure to add the Access Token and Device Serial Number from Step 2 and Step 4. Also, replace the URL. 

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url('https://myfarm-astrofarm-us.42gears.com/api/v1/user/devices/DEVICE_SERIAL/remoteConnect')
  .delete(null)
  .addHeader('authorization', 'Bearer YOUR_ACCESS_TOKEN')
  .build();

Response response = client.newCall(request).execute();

For more details on AstroFarm, click here


Share:

Was this article helpful?

Related Articles