Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Sep 22, 2019 17:35

When testing a mobile website or application, it is always recommended to use visual analysis for user checkpoints. The reason for that is because only visual analysis acts as 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.

So how do we use the visual driver? 
The simple answer is very easy: We need to switch context, by using the switchToContext function, which is part of the Perfecto template, then just call the findElement(By.linkText) command.

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

But what if the text we're looking for can be found only by scrolling the screen? or the default threshold of 60% is not enough for our need? 
Then we need to use the executeScript function which 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 executeScript function and pass the command String and hashMap as parameters.

	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 visual analysis advanced parameters:

  • Use the, screen.height, and 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.
  • The timeout parameter can be used to wait for the text to appear on the screen. Similar to Selenium wait functions.