Friday, 21 November 2014

Multimap using Google Guava API

Multimap concept provided by Google Guava API can store multiple 'values' for the same 'key'.

If you want to have multiple values for a same key using java collection api(i.e Map) it is not possible. Since every time you add a value(i.e. using 'put' method call) to the same/single key, the value for that key is replaced with the new value.

Below program-1 demonstrates trying to store multiple values for the same key without using the Multimap concept.

Program-1 :
Without using Multimap concept
import java.util.HashMap;
import java.util.Map;

public class MapExample1 {
    public static void main(String[] args) {
       
        Map<String,String> myLanguages = new HashMap<String, String>();
       
        myLanguages.put("programmingLanguage","C++");
        myLanguages.put("programmingLanguage","C#");
        myLanguages.put("programmingLanguage","Java");
       
        myLanguages.put("scriptingLanguage","VBScript");
        myLanguages.put("scriptingLanguage","JavaScript");
       
        //Prints the values for the key.
        System.out.println("size: "+myLanguages.size());
       
        System.out.println("\n---- Print the values for key 'programmingLanguage' ----");
        System.out.println(myLanguages.get("programmingLanguage"));
       
        System.out.println("\n---- Print the values for key 'scriptingLanguage' ----");
        System.out.println(myLanguages.get("scriptingLanguage"));
    }
}


Output:
Program-1 shows the output having the key with single value. Every time new value is added to the key, old value is replaced with the new value for the key.

Now lets modify the Program-1 (i.e above program) with Multimap concept provided by Google Guava API

Step 1: 
Download the "guava-<<version>>.jar" from the URL: https://code.google.com/p/guava-libraries/

Step 2:
- Copy the  "guava-<<version>>.jar" in to the 'lib' folder of your project.
- Set the java build path for "guava-<<version>>.jar".
 
 
Program-2 :
Using Multimap concept
Modifying the the above program-1 using MultiMap(i.e. Google Guava API).

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
 

public class MultiMapExample {
    public static void main(String[] args) {
      
        Multimap<String, String> myLanguages = ArrayListMultimap.create();
      
        myLanguages.put("programmingLanguage", "C++");
        myLanguages.put("programmingLanguage", "C#");
        myLanguages.put("programmingLanguage", "Java");
      
        myLanguages.put("scriptingLanguage", "VBScript");
        myLanguages.put("scriptingLanguage", "JavaScript");
      
        int size = myLanguages.size();
        System.out.println("myLanguages size : "+size);
        System.out.println("");
              
        //Prints the values for the key.
        System.out.println("Print the values for key 'programmingLanguage'.");
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("");

        System.out.println("Print the values for key 'scriptingLanguage'.");
        System.out.println(myLanguages.get("scriptingLanguage"));
    }
}


Output:
Output for the progam-2 show's multiple values for the same 'key'. Every time new 'value' is added for the same 'key', old 'values' for the 'key' remains and new 'value' is added to the 'key'.

Program-3 :
Above Program-2 uses Multimap for adding the multiple values for the same/single key.  
 
Current Program-3 demonstrates adding the multiple values for the same/single key without using the Multimap API. 
This can be achieved by using the below data structure.
Map<String,List<String>> myLanguages = new HashMap<String,List<String>>();

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapExample2 {
    public static void main(String[] args) {
      
        Map<String,List<String>> myLanguages = new HashMap<String,List<String>>();
      
        myLanguages.put("programmingLanguage", Arrays.asList("C++","C#","Java"));
        myLanguages.put("scriptingLanguage", Arrays.asList("VBScript","JavaScript"));
      
        System.out.println("size: "+myLanguages.size());
      
        System.out.println("\n---- Print the values for key 'programmingLanguage' ----");
        System.out.println(myLanguages.get("programmingLanguage"));
      
        System.out.println("\n---- Print the values for key 'scriptingLanguage' ----");
        System.out.println(myLanguages.get("scriptingLanguage"));
        System.out.println("");
    }
}

Output:
Program-3 stores multiple values in the list, once all the values are added to the list finally list added to the 'key'.

Program-4 :
Using Multimap - Removing the values for the key
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class MultiMapRemove {
    public static void main(String[] args) {
       
        Multimap<String, String> myLanguages = ArrayListMultimap.create();
       
        myLanguages.put("programmingLanguage", "C");
        myLanguages.put("programmingLanguage", "C++");
        myLanguages.put("programmingLanguage", "C#");
        myLanguages.put("programmingLanguage", "Java");
       
        myLanguages.put("scriptingLanguage", "VBScript");
        myLanguages.put("scriptingLanguage", "JavaScript");
       
        System.out.println("myLanguages size : "+myLanguages.size());
        System.out.println("");
               
        //Prints the values for the key.
        System.out.println("---- Print the values for key 'programmingLanguage' ----");
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("");

        System.out.println("---- Print the values for key 'scriptingLanguage' ----");
        System.out.println(myLanguages.get("scriptingLanguage"));
        System.out.println("");
       
        //remove single value
        System.out.println("---- remove single value for key 'programmingLanguage'----");
        myLanguages.remove("programmingLanguage", "C");
        System.out.println("myLanguages size : "+myLanguages.size());
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("");

        //remove all values
        System.out.println("---- remove all values key 'programmingLanguage'----");
        myLanguages.removeAll("programmingLanguage");
        System.out.println("myLanguages size : "+myLanguages.size());
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("");
    }
}


Output:

Program-5 :
Using Multimap - Replacing the values for the key
Below program demonstrates replacing the values for the key.

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class MultiMapReplace {
    public static void main(String[] args) {
      
        Multimap<String, String> myLanguages = ArrayListMultimap.create();
      
        myLanguages.put("programmingLanguage", "C");
        myLanguages.put("programmingLanguage", "C#");
        myLanguages.put("programmingLanguage", "Java");
      
        myLanguages.put("scriptingLanguage", "JavaScript");
      
        //Prints the values for the key.
        System.out.println("size: "+myLanguages.size());
        System.out.println("---- Print the values for key 'programmingLanguage' ----");
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("---- Print the values for key 'scriptingLanguage' ----");
        System.out.println(myLanguages.get("scriptingLanguage"));
        System.out.println("");
      
        //Replace the values for the key.
        Iterable<String> iterable = myLanguages.get("scriptingLanguage");
        myLanguages.replaceValues("programmingLanguage", iterable);
        System.out.println("size: "+myLanguages.size());

        System.out.println("---- Print the values for key 'programmingLanguage' ----");
        System.out.println(myLanguages.get("programmingLanguage"));
        System.out.println("---- Print the values for key 'scriptingLanguage' ----");
        System.out.println(myLanguages.get("scriptingLanguage"));
    }
}
 

Output:
Note:  Above program-5 replaces all the values for the key 'programmingLanguage' with all the values of the key 'scriptingLanguage'.

Environment used for demonstrating the above example programs:
Google Guava - guava-18.0.jar
Java - 1.6

Tuesday, 18 November 2014

WebDriver Element Locator Add-on

WebDriver element locator Add-on makes easy to find the elements XPath.
It is used to find the different XPaths for single web browser element.
Ex: different combination of XPath locators for 'Google Search' button in "http://www.google.com" page.

Easy to distinguish between unique and duplicate locators.
Populates the context menu with XPath based findElement API command for different programming & scripting languages like Java, C# , Python & Ruby

Context menu show XPath with red exclamation and green ticks.
XPath with red exclamation represents duplicate XPath, it alerts automation developers to avoid using the duplicate locators.
XPath with green ticks represents unique XPaths.

Webdriver Element Locator Add-on can be installed from the below URL:
https://addons.mozilla.org/en-US/firefox/addon/element-locator-for-webdriv/


Click on "Add to Firefox" button.

Click on "Allow"


Click "Install Now" button.

Click "Control" + "Shift" + "A" keyboard buttons and check "Webdriver Element Locator" Add-on is added to Firefox browser "Extenstions"


Capturing the XPath locator of the web browser element.
 - Open the url "www.google.com"
 - Mouse over the "Google Search" button.
 - Perform mouse right click
 - Context menu displayed.

Select XPaths in context menu.



Select the Unique XPath locator(i.e. XPath with green ticks) XPath is copied in to the clipboard.

Perform "Control" + "V" for adding the selected XPath locator with findElement API method in the script.

 Pros:
- Easy to find the XPath locators for the web elements.
- Able to distinguish between duplicate and unique XPath Locator.

 Cons:
- Limited only to XPath locators.
- Context menu doesn't show XPath with red exclamation and green ticks for few web elements(i.e. either it shows red exclamation or green ticks but not both for few web elements).