[appium-device-farm-15] — Step-by-Step Guide to Connect a ‘Real Device’ with “Appium Device Farm”

Get a step-by-step walkthrough in the video below!
Following Steps will be covered in the Article:
— — — — — — — — — — — — — — — — — — — — -
To connect Real Device(Physical Device):
— — — — — — — — — — — — — — — — — — — — —
Step-1: Connect the Real Device with vysor(optional Step).

Step-2: Will Go through the required setting in Developer Options.

Step-3: Mirror the Real Device with Android Studio

Launch Appium Server:
— — — — — — — — — — — — -
Step-4: Launch Appium Server with Device-Farm

Step-5: Try to connect the Real Device.

Step-6: check the error messages(if any)

Steps to Resolve issue: (Very Important)
— — — — — — — — — — — — — — — — — — — —
Step-7: Install device-farm.apk

Step-8: Install stream.apk

Step-9: Try to reconnect with Real Device

Step-10: Execute Our First Script on the Real Device:
package com.appiumguide.devicefarm;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import java.net.*;
import java.time.Duration;
/**
* Script Details - "appium-device-farm”- Step-by-Step Guide to Connect a ‘Real Device’ with “Appium Device Farm”
*
* appium-java-client version: 9.5.0
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/
public class devicefarm_FirstScriptOnRealDeviceDemo {
private AndroidDriver driver;
WebElement dragelement,dropelement;
@BeforeClass
public void setup() throws MalformedURLException {
UiAutomator2Options cap=new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setUdid("E6S8MJPFYHEUZDU8"); //UDID of Real Device
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@Test
public void test1() throws InterruptedException {
driver.activateApp("com.wdiodemoapp");
Thread.sleep(5000);
driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().text(\"Drag\")")).click();
dragelement=driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(\"android.widget.ImageView\").instance(4)"));
dropelement=driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(\"android.view.ViewGroup\").instance(10)"));
int eX=(int)(dropelement.getRect().getX());
int eY=(int)(dropelement.getRect().getY());
//Perform Drag gesture based on element id, endX & endY co-ordinates
System.out.println("Perform Drag gesture based on element id, endX & endY co-ordinates");
((JavascriptExecutor) driver).executeScript("mobile: dragGesture",ImmutableMap.of("elementId",((RemoteWebElement) dragelement).getId(),
"endX",eX,
"endY",eY));
Thread.sleep(3000);
dragelement=driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(\"android.widget.ImageView\").instance(7)"));
int sX=(int)(dragelement.getRect().getX());
int sY=(int)(dragelement.getRect().getY());
dropelement=driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(\"android.view.ViewGroup\").instance(8)"));
int eX1=(int)(dropelement.getRect().getX());
int eY1=(int)(dropelement.getRect().getY());
//Perform Drag gesture based on element id, startX,startY,endX & endY co-ordinates
System.out.println("Perform Drag gesture based on element id, startX,startY,endX & endY co-ordinates");
((JavascriptExecutor) driver).executeScript("mobile: dragGesture",ImmutableMap.of(
"startX",sX,
"startY",sY,
"endX",eX1,
"endY",eY1));
Thread.sleep(3000);
////Perform Drag gesture based on element id,endX,endY co-ordinates and Speed
dragelement=driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(\"android.widget.ImageView\").instance(1)"));
dropelement=driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().className(\"android.view.ViewGroup\").instance(10)"));
int eX2=(int)(dropelement.getRect().getX());
int eY2=(int)(dropelement.getRect().getY());
int speedvalue=(int)(100*2.625);
System.out.println("Perform Drag gesture based on element id,endX,endY co-ordinates and Speed");
((JavascriptExecutor) driver).executeScript("mobile: dragGesture",ImmutableMap.of("elementId",((RemoteWebElement) dragelement).getId(),
"endX",eX2,
"endY",eY2,
"speed",speedvalue));
Thread.sleep(3000);
}
@AfterClass
public void teardown() {
if(driver!=null) {
driver.quit();
System.out.println("Test Execution Completed");
}
}
}
Observe that user able to connect Real Device with Device-Farm

GitHub Link:
🎬 Explore More! Watch My Latest Videos on YouTube!
Visit My Official Blog:
No comments:
Post a Comment