Use visual analysis with RemoteWebDriver

When testing a mobile website or application, it is recommended to use visual analysis for user checkpoints because only visual analysis acts like a real user, "seeing" real-life bugs like corrupted or cut text, something which you might not find when using only native or web driver. To use the visual driver, we need to switch context by using the switchToContext function (part of the Perfecto template) and then call the findElement(By.linkText) command.

Copy
    switchToContext(driver, "VISUAL");
    remoteWebDriver.findElement(By.linkText(needle));   // needle - is the text searching for, preset in the variable

If the text we are looking for can be found only by scrolling the screen or the default threshold of 60% is not enough for our needs, we need to use the executeScript function that Selenium provides, sending the relevant parameters to the proprietary Perfecto function. The example below shows how to call this function.

  1. Create a string with the proprietary command. 
  2. Create a hashMap and put all the needed parameters in it. 
  3. Call the executeScript function and pass the command String and hashMap as parameters.

    Copy
        String needle = "productX";
        String findCommand = "mobile:text:find"; // The proprietary function call
        Map<String, Object> params = new HashMap<>();
        params.put("content", needle);      // The text we're looking for
        params.put("scrolling", "scroll");  // Add the scroll and search
        params.put("next", "SWIPE_UP");     // Next is mandatory if using scroll and search
                                            // Can also use customized swipe like:
                                            // "SWIPE=(50%,80%),(50%,60%);WAIT=1000"    params.put("maxscroll", 3);       // Not mandatory, default is 5
        params.put("threshold", 100);     // Adding threshold
        remoteWebdriver.executeScript(findCommand, params); // Calling the script
        // Clarification: The "threshold" params 
        // is not related to the "scroll and search" param,
        // and can be called or not called regardless of the "scroll and search"

Some short notes about using these advanced visual analysis parameters:

  • Use the screen.top, screen.heightand screen.width parameters to limit the area of the screen searched and speed up the visual analysis operation.
  • Use the index parameter if the string that you are searching for appears multiple times on the screen and you need to identify (for example) the second or third appearance.
  • Some visual analysis commands, for example mobile:test:select, throw an exception if the text is not found.
  • Use the timeout parameter to wait for the text to appear on the screen (similar to Selenium wait functions).