Thursday, 14 December 2017

How to validate xPath (whether the written xpath exist in page or name)

How to validate xPath (whether the written xpath exist in page or name)
Steps:
1) Load the URL in chrome browser
2) Right click on mouse and select inspect option
3) Select inspect option in tool bar (first option (has arrow symbol))
4) Now select which filed locator you want
5) It highlights html code with blue color
6) Right click on high lighted line
7) select Copy
8) select copy xpath
9) Go to console tab
xpath validation - $x ($x("//*[@id='pass']"))
css validation - $ ($("#loginbutton"))

How to find xPath in Chrome browser

How to find xPath in Chrome browser
Steps:
1) Load the URL in chrome browser
2) Right click on mouse and select inspect option
3) Select inspect option in tool bar (first option (has arrow symbol))
4) Now select which filed locator you want
5) It highlights html code with blue color
6) Right click on high lighted line
7) select Copy
8) select copy xpath
9) paste in your selenium code
10) Works fine
ex- facebook email filed (//*[@id="email"])

Thursday, 27 October 2016

How to get uniq value from array

package java_practice;

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class Uniqvaluesfromarray {

public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] numbers = {1, 1, 2, 1, 3, 4, 5};
Set<Integer> uniqKeys = new TreeSet<Integer>();
uniqKeys.addAll(Arrays.asList(numbers));
System.out.println("uniqKeys: " + uniqKeys);
}

}


Output: uniqKeys: [1, 2, 3, 4, 5]

Thursday, 16 June 2016

Collections in java

Java Collections:
Collection: Collection represents a single unit of object/group.
Java Collections framework is collection of interfaces and classes which helps in storing and procesing data efficiently.

                        Collection(interface)
Set(interface)                        List(interface)                Queue(interface)
Hashset,LinkedHashSet,Treeset(sortedSet)        ArrayList,Vector,LinkedList        LinkedList,PriorityQueue

1) List:
List is an ordered collection(sequence).
List may contains duplicate elements
List index is starts with Zero

1a) ArrayList:
ArrayList is resizable implemented from List interface
Java ArrayList class can contain duplicate elements.
ArrayList class maintain insertion order.
ArrayList class is non synchronized.

Methods of ArrayList class:

add("String")
add(index, "String")
remove("String")
set(int index,Object)
indexOf(Object)
get(int index)
size()
contains("String")
clear()



Sample code using ArrayList:


package collections;

import java.util.ArrayList;

public class ArrayList_Example {

    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList<String>();
        arr.add("padi");
        arr.add("pravanjan");
        arr.add("reddy");
        System.out.println("printing arrayList: " + arr);
        System.out.println("Printing size of array:" + arr.size());
        System.out.println("Element at index 0 :" + arr.get(0));
        System.out.println("Does arrayList contains padi: "+arr.contains("padi"));
        arr.add(2, "REDDDY");
        System.out.println("printing arrayList: " + arr);
        System.out.println("Index of PERL is "+arr.indexOf("reddy"));
        System.out.println("Printing size of array:" + arr.size());
       
    }
}

Output:
printing arrayList: [padi, pravanjan, reddy]
Printing size of array:3
Element at index 0 :padi
Does arrayList contains padi: true
printing arrayList: [padi, pravanjan, REDDDY, reddy]
Index of PERL is 3
Printing size of array:4

1b) LinkedList:
Java v class uses doublly linked list to store the elements
LinkedList class can contain duplicate elements
LinkedList class maintains insertion order
LinkedList is non synchronized

Sample Code:
package collections;

import java.util.LinkedList;
import java.util.*;

public class LinkedList_Example {
   
    public static void main(String[] args) {
       
   
    LinkedList<String> ll=new LinkedList<String>();
    ll.add("pabbu");
    ll.add("reddy");
    ll.add("aaaaaaaaaa");
    System.out.println("Printin all linked list values : " + ll);
    Iterator<String> itr=ll.iterator();
    while (itr.hasNext()) {
    System.out.println("printing values using iterator :" + itr.next());
       
    }
    }
}

Output:
Printin all linked list values : [pabbu, reddy, aaaaaaaaaa]
printing values using iterator :pabbu
printing values using iterator :reddy
printing values using iterator :aaaaaaaaaa

+++++++++++++++++ Hash Set Class++++++++++++++
HashSet class uses hashtable to store the elements, It extends AbstartctSet class implements Set interface
HashSet contains only unique elemets.
HashSet contains only Values
 Difference between List and Set:
 - List can contains duplicate elements whereas Set contains unique elemenets only.

++++++++++ Sample code using HashSet class +++++++++
package collections;

import java.util.HashSet;
import java.util.Iterator;

public class HashSet_Example {
public static void main(String[] args) {
    HashSet<String> hset=new HashSet<String>();
    hset.add("pabbu");
    hset.add("pabbu");
    hset.add("reddy");
    hset.add("abcd");
    System.out.println("Printin all values from HaseSet: " + hset);
    Iterator<String> itr=hset.iterator();
    while (itr.hasNext()) {
    System.out.println("printin vaue using iterator : " + itr.next());
       
    }
   
}
}

Output :
Printin all values from HaseSet: [pabbu, reddy, abcd]
printin vaue using iterator : pabbu
printin vaue using iterator : reddy
printin vaue using iterator : abcd

++++++++++++++++ Tree Set Class ++++++++++
- Contains uniqe elements like HashSet
 - Maintains ascending order

Sample code for TreeSet :

package collections;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSet_Example {
public static void main(String[] args) {
    TreeSet<String> ts=new TreeSet<String>();
    ts.add("pabbu");
    ts.add("abcd");
    ts.add("reddy");
    ts.add("reddy");
    System.out.println(" Printin all values from TreeSet: " + ts);
    Iterator<String> itr=ts.iterator();
    while(itr.hasNext())
    {
        System.out.println("printin all values using iterator :" + itr.next());
    }
   
   
}
}

Output :
 Printin all values from TreeSet: [abcd, pabbu, reddy]
printin all values using iterator :abcd
printin all values using iterator :pabbu
printin all values using iterator :reddy

+++++++++++++    HashMap               +++++++++++
 - HashMap contains values based on the key. It implements the Map interface and extends the AbstractMap class.
 - It contains only Uniq elements
 - It may have one null key and multiple null values.
 - It maintains no order.

Sample code for HashMap:
package collections;
import java.util.*;
import java.util.HashMap;
import java.util.Map;


public class HashMapTest {
    public static void main(String[] args) {
       
   
HashMap<Integer,String> hm=new HashMap<Integer,String>();
hm.put(100,"pabbu");
hm.put(200, "reddy");
hm.put(200, "reddy");

    for(HashMap.Entry m:hm.entrySet())
    {
        System.out.println(m.getKey() + " "+m.getValue());
       
    }
   
    }
}


Output:

100 pabbu
200 reddy

+++++++++++++++
What is difference between HashSet and HashMap?

HashSet contains only values whereas HashMap contains entry(key and value).

++++++    Hash Table    ++++++++++++++
- HashTable is an array of list. Each list is know as bucket. The position of bucket is identified by calling the hascode() method.
- HashTable contains values based on the Key.
- it contains only Uniq elements.
- It may not have any null key or value.
- It is Synchronized

Sample Code for HashTable:
package collections;

import java.util.Hashtable;
import java.util.Map;

public class HashTable_Test {
    public static void main(String[] args) {
        Hashtable<Integer,String> ht=new Hashtable<Integer,String>();
        ht.put(100,"pabbu");
        ht.put(200, "reddy");
        ht.put(300,"pabbu");
        ht.put(100,"abcd");
        ht.put(400," ");
       
       
        for(Map.Entry m:ht.entrySet())
                {
            System.out.println(m.getKey()+" "+m.getValue());
                }
    }

}

Output:
400 
300 pabbu
200 reddy
100 abcd


++++++++++     Diffrence between HashMap and Hashtable     ++++

    HashMap                                Hashtable
1) HashMap is non synchronized. It is not thread safe.        1) Hashtable is synchronized.It is thread safe and can be shaed with manu threads.
2) HashMap allows one null key and multiple null values        2) Hashtable doesnt allow any null key or value
3) HashMap is a new class introduced in JDK 1.2            3) Hashtable is legacy class
4) HashMap is fast                        4) hASHTABLE is slow
5) We can make HashMap as synchronized by calling
   Map m=Collections.synchronizedMap(hashMap)            5) Hashtable is internally syncronized






Sorting in assending order using java

package java_interview;

public class Sort_AssendingOrder {
    public static void main(String[] args) {

        int[] arr={22,33,11,31,34};
        int t=0;
        for(int i=0;i<arr.length;i++){
        for(int j=i+1;j<arr.length;j++){
        if(arr[i]>arr[j]){
        t=arr[i];
        arr[i]=arr[j];
        arr[j]=t;

        }
        }
        }

        for(int k=0;k<arr.length;k++){
        System.out.println(arr[k]);
        }
    }
}
Output:
11
22
31
33
34

How to get repeated string in java

package java_interview;

public class RepetedStringCount {
    public static void main(String[] args) {

        String s="regression";
        int repeated;

        for(int i=0;i<s.length();i++)
        {
        for(int j=i+1;j<s.length();j++)
        {
        if(i!=j && s.charAt(i)==s.charAt(j)){
        System.out.println(s.charAt(i));
        break;
        }
        }
        }}
}

Output:
r
e
s

Wednesday, 15 June 2016

How to print minimum number and maximum number in java


public class Print_Min_Max_Value {
   

public static void main(String[] args)
{
    int i;  //loop counter
    int[] arr=new int[]{10,20,90,50}; //arrays
    int min=arr[0]; //set values for comparison from start to end of the array
    int max=arr[0]; //set values for comparison from start to end of the array
    for (i=0;i<arr.length;i++)
    {
        if(arr[i]>max)
        {
            max=arr[i];
        }
        else if(arr[i]<min)
        {
            min=arr[i];
        }
    }
    System.out.println(max);
    System.out.println(min);
   
}




}
output:
90
10