Automate device with Samsung Internet as default browser

Our hybrid app is using OAuth2 as authentication scheme. The login button in our app forwards our customers to an external website, which is provided by an identity server and which is shown via in-app browser. On this website the customers enter their login credentials. The identity server verifies the credentials, generates a user access token and issues a callback to the app. Once the app receives the callback, it brings its UI in foreground again and switches to the logged-in state.

I managed to automate this login process with Appium. My code works for iOS. My code also works for Android when Chrome is set as default browser on the device. However, it does not work when Samsung Internet is set as default browser on the device. It fails when it tries to switch from the webview context of our app to the webview context of the browser which shows the login page.

My code for the context switch looks as follows:

public static String setDriverContext(AppiumDriver<MobileElement> driver, boolean bChromeView)
{
	
    String sWebViewContext =  "";
 
    Set<String> contextNames = driver.getContextHandles();
	TestUtils.log(Level.INFO, logger, false, "Available contexts: " + contextNames.toString());

	ArrayList<String> list = new ArrayList<String>(contextNames);
	//TestUtils.log(Level.INFO, logger, false, "Available contexts in list " + list.toString());
    
	if(bChromeView){
	    //switch to browser webview (e.g. Chrome webview)
		for(String varContext : contextNames) 
			if (varContext.contains("chrome"))     // Chrome case
				sWebViewContext = varContext;
		if(sWebViewContext == "")                     // other cases, e.g. Safari
   			for(String varContext : contextNames) 
				if (varContext.contains("WEBVIEW") && !varContext.equals(webviewContext))     //webviewContext is a static variable in which the webview of our app is stored
					sWebViewContext = varContext;
	}
	else{
	   //switch to App webview
		 for(String varContext : contextNames)
			if (varContext.contains("nameofourapp"))  // Chrome case
				sWebViewContext = varContext;
		 if(sWebViewContext == "")                    // other cases, e.g. Safari
	   		for(String varContext : contextNames) 
   				if (varContext.contains("WEBVIEW") && varContext.equals(webviewContext))     
   					sWebViewContext = varContext;
	}
	 	
	TestUtils.log(Level.INFO, logger, false, "Switching to context: " + sWebViewContext);
	driver.context(sWebViewContext);
	return sWebViewContext;
}

If I execute this code with an Android device where Chrome is set as default browser, then the entire login process works. In particular, both context switches work correctly, that is, first from App webview to Chrome webview and then, after completing the login form on the external website, from Chrome webview back to app webview. This is the output to console for the first context switch from App webview to Chrome webview:

2021-08-13 16:13:57.271 [main] INFO  utils.TestUtils - Available contexts: [NATIVE_APP, WEBVIEW_chrome, WEBVIEW_com.package.name.of.our.app]
2021-08-13 16:13:57.271 [main] INFO  utils.TestUtils - Switching to context: WEBVIEW_chrome
2021-08-13 16:14:01.531 [main] INFO  SideMenu - Changed context to: WEBVIEW_chrome

When I execute the same code with an Android device where Samsung Internet is set as default browser, then it fails at the first context switch from App webview to browser webview. This is what I see in the console:

2021-08-13 16:18:27.270 [main] INFO  utils.TestUtils - Available contexts: [NATIVE_APP, WEBVIEW_Terrace, WEBVIEW_com.package.name.of.our.app]
2021-08-13 16:18:27.271 [main] INFO  utils.TestUtils - Switching to context: WEBVIEW_Terrace
2021-08-13 16:18:32.732 [main] ERROR utils.TestNGListener - test step1_assertCannotCreateReservation failed
io.appium.java_client.NoSuchContextException: An unknown server-side error occurred while processing the command. Original error: An unknown server-side error occurred while processing the command. Original error: unknown error: Failed to get PID for the following process: Terrace

What do I need to do to get the code working for the Samsung Internet case as well?

Hi @simlina did you find any solution for this?

Unfortunately there is no known way to make Samsung browser working with chromedriver. I assume it needs to have its own driver, but Samsung does not provide one

ohh Thanks @mykola-mokhnach :slight_smile:

Try continue without contex switching. In many cases for simple actions it is not needed.