Switch App to browser and back to app

I’ve a Question. In my test script i’ve to open a link in safari, verify URL link and switch back to my app. How can i do this?

2 Likes

Can’t be done! Apple Instruments does not allow us to send commands to more than one app per session. So you can automate your app, or safari, but not both :frowning:

4 Likes

@jonahss So it is possible in Android?

Yes it’s possible on android :smile:

Just switch to the NATIVE context, then tap elements on the screen to launch your app and stuff.

2 Likes

Hi @jonahss,

Right, it is possible for Android. But I am facing a problem while doing it. Here it is:

I am automating an app say App-1 and switching to App-2 which contributes to App-1 flow. Then I am leaving App-2 and Starting App-1 again. And here the problem comes. App-1 is starting properly on switch but does not perform any operation. When I am checking Appium logs, these are showing that operation has been performed( Click a button), but it is not reflecting on the app. and app is trying to perform the next operation which it can not perform, so app gets hanged on it and ultimately time out occurs.

Can you please help me in resolving this issue?

Hm, what version of Android are you doing this on?
Sometimes this functionality is buggy because UIAutomator was really designed for testing a single app. Is your App-1 starting completely new? Or is it in the background and you are foregrounding it.

Hi @jonahss, Thanks for the reply.

I am using Android 5.1.1 at device side and Android 4.4(API Level-19) at appium side(as Capability).

For the first time, App-1 starts completely New, but for the second time(App1->App2->App1), App-1 is starting where I left it which I actually want.

Please check the Code Below:

App-1:

@BeforeClass
public void setup() throws MalformedURLException {

	capabilities
			.setCapability(
					"app",
					"C:\\Users\\dhiraj01\\Downloads\\smartdriver-android-1.6-SNAPSHOT[staging644].apk");
	capabilities.setCapability("appPackage", "com.tower.smartdriver");
	capabilities.setCapability("appActivity", ".activity.WelcomeActivity");
	capabilities.setCapability("deviceName", "0cbbdc0a032f9efa");
	capabilities.setCapability("platformName", "Android");
	capabilities.setCapability("appWaitActivity",
			".activity.WelcomeActivity");

	driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
			capabilities);// new RemoteWebDriver is Selenium Specific Class.
}

   @Test
   Test_code{
       --------
       --------
       --------
       driver.quit();
   Lockito start_Lockito = new Lockito(); //Switching to App-2
   start_Lockito.setup();

  //Come Back to App-1

DesiredCapabilities capabilities_control = new DesiredCapabilities();

capabilities_control.setCapability(MobileCapabilityType.APP_PACKAGE,
			"com.tower.smartdriver");
capabilities_control.setCapability(MobileCapabilityType.APP_ACTIVITY,
			".activity.WelcomeActivity");
capabilities_control.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities_control.setCapability(MobileCapabilityType.DEVICE_NAME, "0cbbdc0a032f9efa");
capabilities_control.setCapability(MobileCapabilityType.APP_WAIT_ACTIVITY,
			".activity.MainActivity");

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
			capabilities_control);

     wait.until(ExpectedConditions.elementToBeClickable(By
			.id("com.tower.smartdriver:id/start_trip_btn")));// Appium showing Success for this.
	
	driver1.findElement(By.id("com.tower.smartdriver:id/start_trip_btn")).click(); //Appium is 
                                                                                                                 //showing success for it.
                                                                                                                
	wait2.until(ExpectedConditions.textToBePresentInElement(
		driver.findElement(By.id("com.tower.smartdriver:id/log_trip_distance_display")), "2.2"));

           ------------
           ------------
           ------------
     }

App-2:

    @BeforeClass
public void setup() throws MalformedURLException {

	DesiredCapabilities capabilities1 = new DesiredCapabilities();
	
	capabilities1.setCapability("appPackage", "fr.dvilleneuve.lockito");
	capabilities1.setCapability("appActivity", ".ui.SplashscreenActivity_");
	capabilities1.setCapability("deviceName", "0cbbdc0a032f9efa");
	capabilities1.setCapability("platformName", "Android");
	capabilities1.setCapability("appWaitActivity",
			".ui.ItinerariesActivity_");

	driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
			capabilities1);

	List<WebElement> Trip_Selection = driver.findElements(By
			.className("android.widget.RelativeLayout"));
	Trip_Selection.get(2).click();

	WebElement Play_Trip = driver.findElement(By
			.id("fr.dvilleneuve.lockito:id/menuPlay"));
	Play_Trip.click();
	driver.quit();// going to App-1
}

Hope this code help you figuring out the error.

Ohhhhh. um. So you have two separate drivers? I don’t think thats going to work. I bet android UIAutomator can only run one instance on a device.

Instead, Hm. If you first run App1, then tap a link to open the browser. Then you can automate the browser, then tap a button on the browser to return to your app. All with just one driver object (so just one appium session).

2 Likes

Hey guys, @jonahss @DhirajAggarwal @jagadeeshkotha
I have a similar scenario here
Scenario:
I have a Hybrid app.
I login to the app and hit a link url which opens up the link in a browser.(Safari/Chrome)

Now i need to switch back to my app,for further testing.
How can i perform this particular action of switching to my App under test.?
Is there a command available in Appium to do so?
Any thoughts?

You could click on a link which brings you back to your app, or add a Back button. Kind of like how facebook login will redirect back to the app that launched it.

1 Like

Hi Jonahss,

I am launching the App in android when i click on the button it will open the browser with loaded URL.
Now i have to switch to browser and perform the action and return back to android app.

After loading browser page i am not able to switch the driver and not able to perform any action.
Please share if you have working sample code.

Thanks
Naresh

Hi @jonahss

This is working spot on Android, swtich to browser from app and perform opeartion and using driver.naviaget().back() is obsoletely working fine,

But for IOS I tired below things to go back to App…
//Using xpath -
driver.findElement(By.xpath("//UIAButton[@name =‘back to app’]"));

//for this I’m getting instruments crash error and some time null pointer excpetions

//Using BackButton

driver.navigate().back()
//this is doing nothing…

Any suggestions for IOS to go back to app

I have a separate admin link which i want to execute in between my app code to delete some entry from admin and again want to back to the app …
Please guide me …

Apps are automated using Intruments. Once you leave the app, instruments crashes and doesn’t let you automate anymore.

We automate Safari and Chrome using their separate debug ports (not Intruments) so even though Instruments dies, we can continue testing on the browser.

But you can’t go back to your app once you leave, Instruments is done automating. This is how Apple’s testing framework works currently.

It is possible that Apple could improve this and allow the operation in their new XCUITest framework. They have said they are very open to feedback, so go ahead and submit a report to Apple asking for the ability to test with XCUITest, and allow testers to leave the app and return to it.

2 Likes

did you find any solution for this? I have something very urgent to do and m stuck … wasted almost 3 days now. Can you provide solution if it worked for u?

Thank you.

I am also having the same issue on iOS. Works seamlessly on Android. I am not able to do a sendKeys or setValue on an element on a webpage opened in Safari (from my iOS native App).

1 Like

@krishan
Can you share the Appium code, you are using for android as I am also facing the same issue.

Please share code for android app(Java) . I am facing same issue fro past 1 month

Can you please share your code, I am facing same issue?

@Aru1 check Driver is starting a browser activity but switches back to app automatically. Hence, i am unable to perform any operation in the browser