How to detect & switch Contexts in iOS and Android Hybrid apps in C# using Appium?
If one of the below instance variables are declared in test script:
public AppiumDriver<IOSElement> iOSappDriver;
public AppiumDriver<AndroidElement> androidDriver;
Then, all the below commands shall work based on the below mentioned situations.
public AppiumDriver<IOSElement> iOSappDriver;
public AppiumDriver<AndroidElement> androidDriver;
Then, all the below commands shall work based on the below mentioned situations.
1. To detect the presence of Contexts, the below command is to be passed in test scripts:
Command: iOSappDriver.Contexts;
Output: Count = 2
[0]: "NATIVE_APP"
[1]: "WEBVIEW_123.4"
2. To find out the Current Context in the android or iOS app:
Command: iOSappDriver.Context;
Output: "NATIVE_APP"
Once user knows the number of contexts present in an android or iOS app, user can set the current context based on need:
3. If user needs to Set the Current Context to Native, then the below test command can be used in scripts:
Command: iOSappDriver.Context = iOSappDriver.Contexts[0];
Output: "NATIVE_APP"
4. If user needs to Set the Current Context to WebView, then the below test command can be used in scripts:
Command: iOSappDriver.Context = iOSappDriver.Contexts[1];
Output: "WEBVIEW_123.4"
Note:
Tested with Appium v1.8.1 in Mac & Windows machines
About the author of this post:
I am a Software Test Engineer, who is quite passionate about testing, automating and finding bugs. I am from Bangalore, India.

