'How to Switching between two native apps using appium
I would like to switch between two apps without loosing the previous state of the application. How can we achieve this using appium.
Scenario: 1. Launch any application which requires OTP to login ( Ex:filpkart) 2. Launch SMS application and read the OTP 3. Close SMS application and Switch back to first app and enter OTP which we read it from SMS application.
Could some one help me on this.
Regards, Shiva Oleti
Solution 1:[1]
Well if you want to send an app in background then simply use driver.CloseApp() function and relaunch it by driver.OpenApp()
You can also use press keycode method Below are the codes
Home Menu Button - 82
Back Button - 4
Recent app - 187
and then perform the getOTP activity from message then switch back to main app
Solution 2:[2]
Step1:- Launch app normally passing all the desired capabilities(For e.g Flipkart app)
Step2):- perform action to get OTP
Step3):- Once OTP received try to pass th AppPacakge and AppActivity of the messaging app where you get otp to below method(you get app pacakage and activity by adb shell command)
public String startNewAndroidActivity(String AppPacakge, String AppActivity) throws
Exception{
String actvty = null;
Activity activity = new Activity(AppPacakge, AppActivity);
activity.setAppWaitPackage(AppPacakge);
activity.setAppWaitActivity(AppActivity);
activity.setStopApp(false);
try {
((AndroidDriver<MobileElement>) driver).startActivity(activity);
Thread.sleep(1000);
actvty = ((StartsActivity) driver).currentActivity();
System.out.println(actvty);
}
catch (Exception e) {
System.out.println("Error occured while starting new Activity
"+e.getMessage());
e.printStackTrace();
}
return actvty;
}
Step4). Once message(OTP) is read close the message application by below method.
public boolean closeApplication() throws Exception {
boolean flag = false;
if(driver!=null) {
driver.closeApp();
flag = true;
}
return flag;
}
Step5). Once this is close find the Xpath of the field where you need to copy the otp. Paste it and go ahead.
P.S:- This work well with Android app.
Solution 3:[3]
Updated
For that you need to define two AppiumServer with different port, two AppiumDriver (one for sms app and another for another app).
Start appium driver with 2 different port eg: 4723 and 4724. Define 2 drivers:
public static AppiumDriver<MobileElement> driver1;
public static AppiumDriver<MobileElement> smsDriver;
Define DesiredCapabilities for both app and initialize both driver.
First open and do Login.
public void startApp1(){
DesiredCapabilities cap1 = new DesiredCapabilities();
cap1.setCapability(MobileCapabilityType.NO_RESET, true);
cap1.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 600);
cap1.setCapability(MobileCapabilityType.DEVICE_NAME, "android device");
cap1.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
cap1.setCapability("appPackage", "your app1 package name");
cap1.setCapability("appActivity", "your app1 package name");
cap1.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
driver1 = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps);
}
After that start sms and perform the action
public void startApp2(){
DesiredCapabilities cap2 = new DesiredCapabilities();
cap2.setCapability(MobileCapabilityType.NO_RESET, true);
cap2.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 600);
cap2.setCapability(MobileCapabilityType.DEVICE_NAME, "android device");
cap2.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
cap2.setCapability("appPackage", "your app1 package name");
cap2.setCapability("appActivity", "your app1 package name");
cap2.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
smsDriver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4724/wd/hub"), caps);
}
Now to switch from app1 to app2 you can use call startApp2 method. Now if you want to switch back again to switch to app1. Use:
driver1.activateApp(appPackage of app1);
now if you again want to switch to app2 use: driver2.activateApp(appPackage of 2nd app);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | akshay patil |
| Solution 2 | Rahii |
| Solution 3 |
