I have a use case where I need to copy a link from an app installed in emulator 1, close the app, switch to emulator 2 and then launch browser on emulator 2 and paste the link copied from app in emulator 1. How can I achieve this? The code I wrote invokes the browser in emulator 1 itself and pastes the link.
File app = new File("abcd.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "100");
cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
/*
String link = code to get the link
*/
driver.closeApp();
driver.quit();
DesiredCapabilities cap1 = new DesiredCapabilities();
cap1.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5556");
cap1.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
cap1.setCapability(MobileCapabilityType.BROWSER_NAME, "chrome");
cap1.setCapability(MobileCapabilityType.VERSION, "7.0");
WebDriver webdriver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap1); // chrome mobile
webdriver.get(link);
For that you need to you need to put Device udid in the DesiredCapabilities. To find the device udid of your emulator, use can use adb devices in the command prompt. It will show the device udid.
Then you can copy the device udid and add it in the DesiredCapabilities.
//Other desired capabilities
cap.setCapability("udid","your emulator 1 device udid");
AppiumDriver driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
It will launch your app in the emulator 1. Now you can copy the link from the device one using
String myLink= driver.findElementById("elements id here").getText();
Then add udid of the emulator2 in the DesiredCapabilities cap1
//other desired capabilities
cap1.setCapability("udid", "emulator 2 udid");
AppiumDriver driver2 = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
//open URL in chrome browser
driver.get(myLink);