How to open one app first and another app next in the same test

Hi,

In the same test, I would like to:

Step 1) Test my app, then
Step 2) Open another app (a browser) and test it
Step 3) Test my app again.

Is this possible?
Could someone please help post a code snippet?

We’ve implemented this by adding an adb module for our appium project. Assuming you have 1 already covered, you could issue this command through the shell

adb shell am start -n <app>

Then, to get back to your app

activity_hash = {app_package: <app_package>, app_activity: <app_activity>}
appium_driver.driver.start_activity

That hash contains two values that you sent as capabilities to appium when you started it up.

1 Like

Thank you. I was able to open the 2nd app, which is a browser, but I cannot interact with it. Is it because I have not updated the driver capabilities during Step 2 below?

Step 1) Test my app, then
Step 2) Open another app (a browser app).
Step 3) Enter a text in a textfield. (Failure – Element not found)
Step 4) Click on a button (Failure – Element not found)

I haven’t had to work significantly with a browser, but I haven’t had any problems with it. How are you trying to locate the objects? Do they show up that way when you dump elements (using get_source) or when you pull up uiautomatorviewer? For example, when we type a URL into the browser, we first swipe ‘down’ so we can guarantee we are at the top, then we locate one of two URL fields to type data into by resource id: ‘com.android.chrome:id/search_box_text’, ‘com.android.chrome:id/url_bar’

Alternately, you can look into the work on the Chrome driver. I’ve seen several references to it on this site.

Thanks Will. I can open the 2nd app, but I cannot interact with it. Any idea why?

	//Hide my app first in background
	driver.runAppInBackground(1);
	
        //Open the 2nd app (browser) in forground
	String adbPath = "/Users/mayukataoka/Library/Android/sdk/platform-tools/adb";
	Runtime.getRuntime().exec(adbPath + " shell am start com.android.browser");
	
        //This line prints out the page source from the first app I put in background somehow incorrectly! 
	System.out.println(driver.getPageSource());

        //Throws an error.  Not finding the element.  I tied the id without "com.android.browser:id/" also
	driver.findElement(By.id("com.android.browser:id/Passwd")).sendKeys("test");		
        //Throws an error.  Not finding the element. 
	driver.findElement(By.id("com.android.browser:id/signIn")).click();

Just some thoughts

driver.runAppInBackground(1);
    // WO> Why do you need to send the app to the background?  When you start the browser
    // the app will go into the background automatically

    //Open the 2nd app (browser) in forground
String adbPath = "/Users/mayukataoka/Library/Android/sdk/platform-tools/adb";
Runtime.getRuntime().exec(adbPath + " shell am start com.android.browser");

    //This line prints out the page source from the first app I put in background somehow incorrectly! 
System.out.println(driver.getPageSource());
    // WO> Before you grab the page source, you should confirm that the browser is up and running
    // first.  First, you can check that the activity is running.  driver.current_activity should return ".BrowserActivity"
    // Then you can check for the existence of one of the browser bar by resource ids.  here's one id that's used
    // on some devices"com.android.browser:id/url".  The point is, you have to wait for the id to be present before 
    // trying to use it


    //Throws an error.  Not finding the element.  I tied the id without "com.android.browser:id/" also
driver.findElement(By.id("com.android.browser:id/Passwd")).sendKeys("test");		
    //Throws an error.  Not finding the element. 
driver.findElement(By.id("com.android.browser:id/signIn")).click();

Thank you, Will. I fixed the problems you pointed out in my code and my test is working properly now. Happy for now. :slight_smile:

hi can you guide me how to Switch between two apps

Step 1) Open my app, and click get OTP
Step 2) Open Chrome app and open some website and there it should Read OTP
Step 3) The OTP should pass in my First app