Selenium and Java

Wednesday, 5 August 2015

Java - Set collection interfaces

Set collection interface:

A Set is a Collection that cannot contain duplicate elements. The Java platform contains three general-purpose Set implementations: HashSet, TreeSet, and LinkedHashSet

HashSet: This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not
guarantee that the order will remain constant over time. This class permits the null element.

   HashSet<String> hs =new HashSet<String>();
   hs.add("Name 1");
   hs.add("Name 2");
   hs.add("Name 3");

      int sizehashset = hs.size();
      System.out.println(sizehashset);
      Iterator<String> itr=hs.iterator();

    while(itr.hasNext()){
         System.out.println(itr.next()); }
   hs.contains(Object) ;
   hs.clear();

TreeSet : TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in sorted, ascending order. The TreeSet class implements NavigableSet interface that extends the SortedSet interface. Access and retrieval times are quite fast.

LinkedHashSet :  LinkedHashSet is between HashSet and TreeSet. It is implemented as a hash table with a linked list running through it, so it provides the order of insertion.
LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted. This allows insertion-order iteration over the set.

Java - Collections

Collections

A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers).

The core collection interfaces encapsulate different types of collections, which are shown in the figure below. These interfaces allow collections to be manipulated independently of the details of their representation. Core collection interfaces are the foundation of the Java Collections Framework. As you can see in the following figure, the core collection interfaces form a hierarchy.




A Set is a special kind of Collection, a SortedSet is a special kind of Set, and so forth. Note also that the hierarchy consists of two distinct trees — a Map is not a true Collection.

The following list describes the core collection interfaces:


Collection — the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such as Set and List. Also see The Collection Interface section.

Set — a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine. See also The Set Interface section.
List — an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position). If you've used Vector, you're familiar with the general flavor of List. Also see The List Interface section.
Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.

Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements' natural ordering. Whatever the ordering used, the head of the queue is the element that would be removed by a call to remove or poll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties. Also see The Queue Interface section.
Deque — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Deque provides additional insertion, extraction, and inspection operations.

Deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out). In a deque all new elements can be inserted, retrieved and removed at both ends. Also see The Deque Interface section.
Map — an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. If you've used Hashtable, you're already familiar with the basics of Map. Also see The Map Interface section.

The last two core collection interfaces are merely sorted versions of Set and Map:
SortedSet — a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls. Also see The SortedSet Interface section.
SortedMap — a Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories. Also see The SortedMap Interface section.

Methods of Collection interface

There are many methods declared in the Collection interface. They are as follows:

No.MethodDescription
1public boolean add(Object element)is used to insert an element in this collection.
2public boolean addAll(collection c)is used to insert the specified collection elements in the invoking collection.
3public boolean remove(Object element)is used to delete an element from this collection.
4public boolean removeAll(Collection c)is used to delete all the elements of specified collection from the invoking collection.
5public boolean retainAll(Collection c)is used to delete all the elements of invoking collection except the specified collection.
6public int size()return the total number of elements in the collection.
7public void clear()removes the total no of element from the collection.
8public boolean contains(object element)is used to search an element.
9public boolean containsAll(Collection c)is used to search the specified collection in this collection.
10public Iterator iterator()returns an iterator.
11public Object[] toArray()converts collection into array.
12public boolean isEmpty()checks if collection is empty.
13public boolean equals(Object element)matches two collection.
14public int hashCode()returns the hashcode number for collection.

Iterator interface

Iterator interface provides the facility of iterating the elements in forward direction only.

Methods of Iterator interface

There are only three methods in the Iterator interface. They are:
  1. public boolean hasNext(): it returns true if iterator has more elements.
  2. public object next() : it returns the element and moves the cursor pointer to the next element.
  3. public void remove(): it removes the last elements returned by the iterator. It is rarely used.

Tuesday, 4 August 2015

Selenium - Handling multiple iFrames in Selenium Webdriver

An inline frame is used to embed another document within the current HTML document. It means iframe is actually a webpage within the webpage which have its own DOM for every iframe on the page.

Once the frame is selected or navigated , all subsequent calls on the WebDriver interface are made to that frame. i.e the driver focus will be now on the frame. What ever operations we try to perform on pages will not work and throws element not found as we navigated / switched to Frame.
We can select an iFrame using the below metthods:

1. by index: frame(index):  We can use index of the frame to get focus on the frame. index start with 0. 

   driver.switchTo().frame(0) ;// focus on 0th frame

   Parameters: Index - (zero-based) index
   Returns: driver focused on the given frame (current frame)
   Throws: NoSuchFrameException - If the frame is not found.

2. by name : frame(Name of Frame) : We can use name of the frame instead of index to get frame.

      driver.switchTo().frame(frameName) ;// focus on frame with name frameName

    Parameters: name Or Id - the name of the frame or the id of the frame element.
    Returns: driver focused on the given frame (current frame)
   Throws: NoSuchFrameException - If the frame is not found

3. by Id: frame(Id of the frame): We can also use Id of the frame as below

  driver.switchTo().frame(frameID) ;// focus on frame with ID as frameId

    Parameters: name Or Id - the name of the frame or the id of the frame element.
    Returns: driver focused on the given frame (current frame)
   Throws: NoSuchFrameException - If the frame is not found

4. by locating frame like other element: frame(WebElement frameElement)

  First locate frame with any web element locator and use webelement.

 Webelement el = driver.findelement(by.xpath(".//iframe[@title='ABC']"));
 driver.switchTo().frame(el);

    Parameters: frameElement - The frame element to switch to.
    Returns: driver focused on the given frame (current frame).
    Throws: NoSuchFrameException - If the given element is neither an iframe nor a frame element. And StaleElementReferenceException - If the WebElement has gone stale.

5. switching outside frame : defaultContent()

  driver.switchTo().defaultContent();

6. switch to multiple frame : 

driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);