Use context in Selenium tests

When you us Perfecto in your Selenium tests, you operate in something called a context. By default, Perfecto is started with a WEBVIEW context. In this context, you examine DOM elements.

If the tested application is a native or a hybrid one, you may want to examine native UI elements. To do so, you will need to switch the context to NATIVE_APP.

Perfecto provides a third VISUAL context. In this context, elements are analyzed by applying optical character recognition technology (OCR) to the application screenshot. At the moment, Selenium clients provide partial support for the context switching, so we need to add some code to allow it.

For the Java client, things are quite simple. Add the following 3 methods to your test code, assuming _driver is the RemoteWebDriver used in your test:

Copy
private void switchToContext(String context) {
        RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(_driver);
        Map<String,String> params = new HashMap<>();
        params.put("name", context);
        executeMethod.execute(DriverCommand.SWITCH_TO_CONTEXT, params);
    }
private String getCurrentContextHandle() {          
        RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(_driver);
        String context =  (String) executeMethod.execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE, null);
        return context;
    }
private List<String> getContextHandles() {          
        RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(_driver);
        List<String> contexts =  (List<String>) executeMethod.execute(DriverCommand.GET_CONTEXT_HANDLES, null);
        return contexts;
    }

These methods are already included in the Perfecto templates for RemoteWebDriver projects in all plugins.

To obtain the current context in a Java client, use the following:

Copy
String context = getCurrentContextHandle();

To obtain a list of all Perfecto contexts in a Java client, use the following:

Copy
List contexts = getContextHandles();

To switch to another context in a Java client, use the following:

Copy
switchToContext(CONTEXT_NAME); // "WEBVIEW", "NATIVE_APP" or "VISUAL"

In a Selenium .NET client, context switching requires a bit more work, supported by the Perfecto Visual Studio plug-in (deprecated). It is based on using the following classes:

Copy
public string Context
        {
            get
            {
                var commandResponse = Execute(GET_CONTEXT, null);
                return commandResponse.Value as string;
            }
            set
            {
                var parameters = new Dictionary<string, object>();
                parameters.Add(NAME, value);
                Execute(SET_CONTEXT, parameters);
            }
        }
public ReadOnlyCollection<string> Contexts
        {
            get
            {
                var commandResponse = Execute(CONTEXTS, null);
                var contexts = new List<string>();
                var objects = commandResponse.Value as object[];
                if (null != objects && 0 < objects.Length)
                {
                    contexts.AddRange(objects.Select(o => o.ToString()));
                }
                return contexts.AsReadOnly();
            }
        }

To obtain the current context in a .NET test, use the following:

Copy
string context = driver.Context;

To obtain a list of all Perfecto contexts in a .NET test, use the following:

Copy
ReadOnlyCollection contexts = driver.Contexts;

To switch to another context in a .NET test, use the following:

Copy
driver.Context = CONTEXT_NAME; // "WEBVIEW", "NATIVE_APP" or "VISUAL"