Automation Using Selenium Webdriver

Thursday 6 April 2017

how-to-find-duplicate-words-in-string


======How To Find Duplicate Words In String==========


public class DuplicateWordsInString
{
public static void main(String[] args)
{
String test = "This sentence contains two words, one and two";
Set<String> duplicates = duplicateWords(test);
System.out.println("input : " + test);
System.out.println("output : " + duplicates);
 }
/** * Method to find duplicate words in a Sentence or String * @param input String * @return set of duplicate words */
 public static Set<String> duplicateWords(String input)
{
 if(input == null || input.isEmpty()){
return Collections.emptySet();
 }
 Set<String> duplicates = new HashSet<>();
 String[] words = input.split("\\s+");
 Set<String> set = new HashSet<>();
 for(String word : words){ if(!set.add(word))
{
duplicates.add(word);

}
 }
 return duplicates;
}
 }
Output : input : This sentence contains two words, one and two
output : [two]

how-to-find-duplicate-words-in-string

Sunday 19 March 2017

Top 16 Java Inheritance Interview questions for freshers and experienced

1.what is inheritance?
  • inheritance is one of the oops concepts in java.inheritance is concept of  getting properties of one class object to another class object.
  • Inheritance represents the IS-A relationship,also known as parent-child relationship.
2.what are the types of inheritance?

1.Multiple inheritance( java doesn't support multiple inheritance).
2.Multilevel inheritance.

3.How Inheritance can be implemented in java?
  • Inheritance can be implemented in JAVA using below two keywords:
1.extends
2.implements
  • extends is used for developing inheritance between two classes and two interfaces.
  • implements keyword is used to developed inheritance between interface and class.
4.Why we need to use Inheritance?

1.For Code Re usability.
2.For Method Overriding.

5.what is syntax of inheritance?

public class subclass extends superclass{

//all methods and variables declare here
}

6.what is multilevel inheritance?
  • Getting the properties from one class object to another class object level wise with different priorities.

6.what is Multiple inheritance?why Java Doesn't Support multiple Inheritance.
  • The concept of Getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.
  • In multiple inheritance there is every chance of multiple properties of multiple objects with  the same name available to the sub class object with same priorities leads for the ambiguity. also known as diamond problem. one class extending two super classes.
  • Because of multiple inheritance there is chance of the root object getting created more than once.
  • Always the root object i.e object of object class hast to be created only once.
  1. Because of above mentioned reasons multiple inheritance would not be supported by java.
  2. Thus in java a class can not extend more than one class simultaneously. At most a class can extend only one class.
8.How do you implement multiple inheritance in java?
  • Using interfaces java can support multiple inheritance concept in java. in java can not extend more than one classes, but a class can implement more than one interfaces.
Program:

interface A{

}
interface B{
}
class C implements A,B{
}

9.Can a class extend itself?

  • No,A class can't extend itself.

10.What happens if super class and sub class having same field name?


  • Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.

Wednesday 1 March 2017

Finding Largest String in ArrayList

Finding Largest String in ArrayList

package com.java.exp;

import java.util.ArrayList;

public class HighestCharinString {

public static void main(String[] args) {

ArrayList<String> al=new ArrayList<>();
al.add("Bangalore");
al.add("hyd");
al.add("jagan");
al.add("upenderathota");
//I would set your largestString variable to your first String that you add:
int largeststring=al.get(0).length();
int index=0;
for(int i=0;i<al.size();i++)
{
//Then you should use the following to check for the largest String:
if(al.get(i).length()>largeststring)
{
largeststring=al.size();
index=i;
}
}
System.out.println("Index " + index + " "+ al.get(index) + " " + "is the largest and is size " + largeststring);


}

}
//OUTPUT:Index 3 upenderathota is the largest

Tuesday 28 February 2017

PrintDaimond program in java

package com.java.exp;

public class Diamond {
public static void main(String[] args)
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
}


}

OUTPUT::


        *
      * * * 
    * * * * *
  * * * * * * *
* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *

Tuesday 7 February 2017

Difference between throw and throws in java


throw keyword:
  • throw keyword used to throw user defined exceptions.(we can throw predefined exception too)
  • If we are having our own validations in our code we can use this throw keyword.
  • For Ex: BookNotFoundException, InvalidAgeException (user defined).

Program:
  1. package com.instanceofjava;
  2. public class MyExceptionThrow {
  3.  
  4.  public static void main(String a[]){
  5.  
  6.  try{
  7.  
  8. MyExceptionThrow thr = new MyExceptionThrow();
  9. System.out.println("length of INDU is "+thr.getStringSize("INDU"));
  10. System.out.println("length of SAIDESH is "+thr.getStringSize("SAIDSH"));
  11. System.out.println("length of null string is "+thr.getStringSize(null)); 
  12.  
  13.  }
  14. catch (Exception ex){
  15.   System.out.println("Exception message: "+ex.getMessage());  
  16.  }
  17.  }
  18.  
  19.  public int getStringSize(String str) throws Exception{
  20.  
  21.  if(str == null){
  22.    throw new Exception("String input is null");  
  23.  }
  24.  return str.length();
  25. }
  26.  
  27. }


Output
length of INDU is 4
length of SAIDESH is 5
Exception message: String input is null

throws keyword:

  •  The functionality of throws keyword is only to explicitly to mention that the method is proven transfer un handled exceptions to the calling place.

Program:
  1. package com.instanceofjava;
  2. public class ExcpetionDemo {
  3.  
  4. public static void main(String agrs[]){
  5.  
  6. try
  7. {
  8. //statements
  9. }
  10. catch(Exception e)
  11. {
  12. System.out.println(e);
  13. }
  14. finally(){compulsorily executable statements
  15. }
  16. }
  17.  
  18. }

Tuesday 24 January 2017

Closing All Tabs Using Robot Class In Selenium WebDriver

Closing All Tabs Using Robot Class In Selenium WebDriver

Earlier we have used java robot class to save Image from page In THIS POST. Robot class Is useful to send key-press and key-release events. We will use same thing here to perform keyboard ALT + SPACE + "c" (Shortcut key)  key-press events to close all tabs of browser. We can perform this key-press event sequence very easily using Robot class. I am suggesting you to use always WebDriver's driver.quit(); method to close all tabs of browser. Intention of this postIs to make you more aware about Robot class usage with selenium WebDriver so you can perform such tricky actions 
easily whenever required.

Bellow given example has used Robot class In @AfterTest methodto close browser tabs using ALT + SPACE + 'c' key-press event sequence. Run It In your eclipse IDE to check how It works.


package Testing_Pack;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Tabs {

 WebDriver driver;
 Robot rb;

 @BeforeTest
 public void setup() throws Exception {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://use any website try ur self");
 }

 @Test
 public void openTab() {  
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
  driver.get("http:try your own");
  switchToTab();
  driver.findElement(By.xpath("//input[@id='6']")).click();
  driver.findElement(By.xpath("//input[@id='plus']"));
  driver.findElement(By.xpath("//input[@id='3']"));
  driver.findElement(By.xpath("//input[@id='equals']"));
  
  switchToTab();
  driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys("hi");
  driver.findElement(By.xpath("//input[@name='LastName']")).sendKeys("test");
  
  switchToTab();
  String str = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Sum result Is -> "+str);
 } 

 public void switchToTab() {  
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");  
  driver.switchTo().defaultContent();  
 }

 @AfterTest
 public void closeTabs() throws AWTException {
  //Used Robot class to perform ALT + SPACE + 'c' keypress event.
  rb =new Robot();
  rb.keyPress(KeyEvent.VK_ALT);
  rb.keyPress(KeyEvent.VK_SPACE);
  rb.keyPress(KeyEvent.VK_C);
 }
}

Monday 23 January 2017

Difference between Collections and Collection in java with example program

Difference between Collections and Collection in java with example program

Famous java interview question: difference between collections and collection in java
Major difference between Collection and Collections is Collection is an interface and Collections is a class.
Both are belongs to java.util package
Collection is base interface for list set and queue.
Collections is a class and it is called utility class.
Collections utility class contains some predefined methods so that we can use while working with
Collection type of classes(treeset, arraylist, linkedlist etc.)
Collection is base interface for List , Set and Queue.


Collection vs Collections

public interface Collection<E>
extends Iterable<E>


public class Collections
extends Object

Collections utility class contains static utility methods so that we can use those methods by using
class name without creating object of Collections class object
Lest see some methods of Collections class.


addAll: public static <T> boolean addAll(Collection<? super T> c,T... elements)
reverseOrder: public static <T> Comparator<T> reverseOrder()
shuffle: public static void shuffle(List<?> list)
sort:public static <T extends Comparable<? super T>> void sort(List<T> list)
How to Relate Collection and Collections
ArrayList is a Collection type of class means it is implementing Collection interface internally
Now lets see a java example program to sort ArrayList of elements using Collections.sort() method.

public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable


1.Basic Java example program to sort arraylist of integers using Collections.sort() method

package com.javasortarraylistofobjects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class SortArrayListExample{

public static void main(String[] args) {

//create an ArrayList object
 ArrayList<Integer> arrayList = new ArrayList<Integer>();
     
 //Add elements to Arraylist
arrayList.add(10);
arrayList.add(7);
arrayList.add(11);
arrayList.add(4);
arrayList.add(9);
arrayList.add(6);
arrayList.add(2);
arrayList.add(8);
arrayList.add(5);
arrayList.add(1);
     
     
 System.out.println("Before sorting ArrayList ...");
 Iterator itr=arrayList.iterator();
     
while (itr.hasNext()) {

System.out.println(itr.next());
   
}

     
 /*
 To sort an ArrayList object, use Collection.sort method. This is a
  static method. It sorts an ArrayList object's elements into ascending order.
*/
  Collections.sort(arrayList);
   
  System.out.println("After sorting ArrayList ...");
     
 
     
Iterator itr1=arrayList.iterator();
     
while (itr1.hasNext()) {

System.out.println(itr1.next());
         
}
 

}
}



Output:

Before sorting ArrayList ...
10
7
11
4
9
6
2
8
5
1
After sorting ArrayList ...
1
2
4
5
6
7
8
9
10
11