Hi All,
I am also facing same issue to Switch back to Native App<.apk file> from WebView.
Below is the my Scenario.
1. I Need to login to my App and do some operations and logout
2. Launch google.com and search some text
3. Again login to my App
Below is the code I have written
1. Installed .apk file through DesiredCapabilities
2. Perfomed some operations after login to app and logout
3. Again Through DesiredCapabilities launching google.com and performing some operations
But again while Switching back to Native app getting some error.
Appium Version: 1.46.1
OS: Windows 7
Device : Android Tab
This is the code.
public class Switchbtnapp {
public static AppiumDriver driver;
public static void main(String arg[]) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
File appDir = new File("C:\\Testing\\Library\\");
File app = new File(appDir, "Fluency_Flex_New.apk");
capabilities.setCapability("deviceName","41004d5d8e236000");
capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("autoWebview","true");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
capabilities.setCapability("newCommandTimeout", "180");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appActivity", "com.mmodal.fluencyflex.gui.startup.MhaLoginActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
driver.get("www.google.com");
Thread.sleep(15000);
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains("WEBVIEW_1")){
driver.context(contextName);
System.out.println(contextName);
Thread.sleep(15000);
driver.findElement(By.id("lst-ib")).sendKeys("Automation");
//driver.context("NATIVE_APP_com.mmodal.fluencyflex.gui.startup.MhaLoginActivity");
driver.context("NATIVE_APP");
System.out.println(driver.getContext());
Thread.sleep(20000);
driver.findElementById("com.mmodal.fluencyflex.qa:id/userid").sendKeys("automationuser2");
}
}
}
}
Error:
NATIVE_APP
WEBVIEW_1
WEBVIEW_1
NATIVE_APP
Exception in thread "main" io.appium.java_client.NoSuchContextException: No such context found. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 19 milliseconds
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:16:47'
System info: host: 'LAP89-QA-BNG', ip: '10.216.11.236', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_91'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=true, version=50.0.2661.89, platform=ANDROID, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 37113c6f19761ff109fcbe1b523a9cbb
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:180)
at io.appium.java_client.AppiumDriver.context(AppiumDriver.java:536)
at Switchbtnapp.main(Switchbtnapp.java:56)
Can anyone know the solution Please let me know.