13

Is there any possibility to switch from one application to another application at run time using Appium.

Thanks

Sravan
  • 591
  • 1
  • 4
  • 16

5 Answers5

17

Finally I found accurate answer, May it will be usefull for some one

source https://www.linkedin.com/grp/post/6669152-6027319885992841219?trk=groups-post-b-title

 // App1 capabilities
 String calculatorAppPackageName="com.android.calculator2";
 String calculatorAppActivityName="com.android.calculator2.Calculator";

// App2 capabilities
 String settingsAppPackageName="com.android.settings";
 String settingsAppActivityName="com.android.settings.Settings";

 @Before
 public void setUp() throws MalformedURLException
 {
        DesiredCapabilities capabilities = DesiredCapabilities.android();
        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "192.168.215.101:5555");
        capabilities.setCapability(MobileCapabilityType.APP_PACKAGE, calculatorAppPackageName);
        capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, calculatorAppActivityName);
        driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 }

 @Test
    public void testApp() throws InterruptedException, MalformedURLException
    {
        //Perform calculation in calculator
        driver.findElement(By.name("4")).click();
        driver.findElement(By.name("×")).click();
        driver.findElement(By.name("3")).click();
        driver.findElement(By.name("=")).click();

        //launch settings App
        driver.startActivity(settingsAppPackageName, settingsAppActivityName);

        //Switch OFF WIFI
        driver.findElement(By.id("com.android.settings:id/switchWidget")).click();

        //Re launch calculator App
        driver.startActivity(calculatorAppPackageName, calculatorAppActivityName);

        //Validate results
        String result = driver.findElement(By.className("android.widget.EditText")).getText();
        System.out.println("Result : " + result);
        Assert.assertEquals("Incorrect Result", "12", result);
    }
Sravan
  • 591
  • 1
  • 4
  • 16
  • 3
    Is there a way to achieve this in iOS? – anavarroma Aug 05 '15 at 14:34
  • 2
    Just an update on the command - driver.startActivity(settingsAppPackageName, settingsAppActivityName); It has changed now, the new way of performing startActivity is: driver.startActivity(new Activity(settingsAppPackageName, settingsAppActivityName)); – Sam Jul 17 '19 at 09:17
6

You can change applications by re-instantiating the webdriver with the new application's attributes.

driver = webdriver.Remote(appiumUrl,dcapabilityApp1)
[Your tests]
driver = webdriver.Remote(appiumUrl,dcapabilityApp2)
[New app tests]

As long as you don't close/disconnect the emulator/simulator/device then your user data will be maintained.

jkbz
  • 101
  • 1
  • 4
  • HI jkbz, Thanks for the response :) I am working with JAVA API, do u have any idea java equivalent webdriver.Remote do we need to change the appium port number for new instance of driver = webdriver.Remote(appiumUrl,dcapabilityApp2 [New app tests] – Sravan Jun 25 '14 at 05:09
  • You shouldn't have to change the port; _appiumUrl_ (above) is inclusive of both IP and port. – jkbz Jun 25 '14 at 19:34
  • Thanks jkbz for the answer. As a part of second step can I launch which is already installed on device, I mean to say I do not have .app or .ipa file for that app, but it is installed. thanks in advance – Changdeo Jadhav Jun 30 '14 at 07:23
  • @ChangdeoJadhav - You can use appium to access any applications (pre-installed or native) as long as you have the application identifiers (package ID, app activity), etc. Personally I only have appium install the applications if I have to (iOS, Selendroid, etc.) and use ADB instead. – jkbz Jul 01 '14 at 13:45
  • @jkbz I tried above for iOS and I am getting following exception java.lang.AssertionError: Execution Failed due toA new session could not be created. (Original error: Requested a new session but one was in progress) (WARNING: The server did not provide any stacktrace information). Do I need to clear previous session? Can you point me on how to do this? – Changdeo Jadhav Jul 01 '14 at 13:54
  • I would recommend starting a separate thread for this issue and provide specifics to your code. Thx. – jkbz Jul 01 '14 at 19:36
  • Thanks here is new qustion http://stackoverflow.com/questions/24530783/ios-app-gets-reset-on-creation-of-new-appium-session – Changdeo Jadhav Jul 02 '14 at 12:14
0

You can use:

driver.startActivity(settingsAppPackageName, settingsAppActivityName);

to invoke another app withing the same code.

MJH
  • 2,301
  • 7
  • 18
  • 20
0

Going through question , i have an assumption that it might break your driver current session.and if the driver command failed there is no fall back for it. Can't it been done with adb command . One can use above solution or might use abd command

adb shell am start -d <YOUR_ACTIVITY_NAME>

And this will open directly appActivity without fail.

-1

driver.startActivity() method can be used to switch between apps. For more details how it works you can check below video.

Watch "Appium Tutorial- Switching between apps (Contact and SMS)" on YouTube https://youtu.be/sH1bHeDDj8U