Automation Using Selenium Webdriver

Monday, 14 November 2016

Calling static method from non static method in java

  • Static means class level and non static means object level.
  • Non static variable gets memory in each in every object dynamically.
  • Static variables are not part of object and while class loading itself all static variables gets memory.
  • Like static variables we have static methods. Without creating object we can access static methods.
  • Static methods are class level. and We can still access static methods in side non static methods.
  • We can call static methods without using object also by using class name.
  • And the answer to the question of  "is it possible to call static methods from non static methods in java" is yes.
  • If we are calling a static method from non static methods means calling a single common method using unique object of class which is possible. 


Program #1: Java example program to call static method from non static method. 


;
public class StaticMethodDemo {
void nonStaticMethod(){
        
        System.out.println("Hi i am non static method");
        staticMethod();
 }
    
 public static void staticMethod(){
        
        System.out.println("Hi i am static method");
  }
    
 public static void main(String[] args) {
        StaticMethodDemo obj= new StaticMethodDemo();
        
        obj.nonStaticMethod();
    }
}
 Output:

Hi i am non static method
Hi i am static method

In the above program we have created object of the class and called a non static method on that object and in side non static method called a static method.
So it is always possible to access static variables and static methods in side non static methods

 Program #2: Java example program to call static method from non static method.



Sunday, 13 November 2016

Collections List

List Interface:
List allows Duplicate Elements.
List having index.
List allows n number of null values.
List will display Insertion order with index.
List having classes like :
Vector
ArrayList
LinkedList
Vector:
Vector is a legacy class.
Vector is synchronized.
Vector initial capacity is 10.
Vector allows n number of null values
Vector can be accessible by index.
Vector allows Duplicate Elements.
Program for Vector:

package com.instanceofjavaforus;
import java.util.Enumeration;

import java.util.List;

import java.util.Vector;



public class A{

 public static void main(String[] args) {

 

 Vector vector=new Vector();



 vector.add("india");

 vector.add("jagan");

 vector.add("sam");

 vector.addElement(null);

 vector.addElement(null);
 
  Enumeration em=vector.elements();
 while(em.hasMoreElements()){
  System.out.println(em.nextElement());
 
 }
 }

}
Output:

india
jagan
sam
null

null



ArrayList:

ArrayList is not Synchronized.
Arraylist also allows Duplicate Elements.
ArrayList allows n number of null values.
Arraylist  having insertion order with index.
For retrieve purpose ArrayList is best choice.
ArrayList having Randaom Access Nature.
Program for ArrayList ;



package com.instanceofjava;
import java.util.ArrayList;
import java.util.ListIterator;

import java.util.List;



public class A{

 public static void main(String[] args) {

 

             ArrayList arrayList=new ArrayList();




 arrayList.add("jagan");

 arrayList.add("jagan");

 arrayList.add("upendra");

 arrayList.add(null);

 arrayList.add(null);
 
  ListIterator it=arrayList.listIterator();
 while(it.hasNext()){
  System.out.println(it.hasNext());
 
 }
 }
}

Output:
jagan
jagan
upendra
null
null

LinkedList:

LinkedList is not synchronized.
LinkedList allows n number of null values.
LinkedList allows Duplicate Elements.
LinkedList  having insertion order with index.
Insertion and Deletion purpose LinkedList is better choice.
LinkedList Follows Doubly linked list Structure.
Program for LinkedList:


package com.instanceofjava;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.List;

public class A{

 public static void main(String[] args) {

   LinkedList linkedList=new LinkedList();




 linkedList.add("jagan");

 linkedList.add("jagan");

 linkedList.add("naresh");

 linkedList.add(null);

 linkedList.add(null);


  ListIterator it=linkedList.listIterator();
  while(it.hasNext()){
  System.out.println(it.hasNext());
 
 }
 }
}

Output:


jagan
jagan
naresh
null
null
In List interface if you want  to add elements we can use add() method.
If you want  to remove elements we can use remove() method.

Using Keys Method to Perform Keyboard Actions in Suggestions List Box

There’s always a need to perform keyboard actions in Suggestion List.For that purpose we  can use this ‘org.openqa.selenium.Keys’ method. Here I am sharing the Code, how to do it.
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class indianrail
{
public static void main(String[] args) throws Exception
{
FirefoxDriver fd=new FirefoxDriver();
fd.get(“http://www.indianrail.gov.in”);
fd.findElement(By.linkText(“Trains between Stations”)).click();
Thread.sleep(5000);
fd.findElement(By.id(“txtStationFrom”)).sendKeys(“hyd”);
fd.findElement(By.id(“txtStationFrom”)).sendKeys(Keys.ARROW_DOWN);
fd.findElement(By.id(“txtStationFrom”)).sendKeys(Keys.ENTER);
Thread.sleep(2000);
fd.findElement(By.id(“txtStationTo”)).sendKeys(“bangalo”);
fd.findElement(By.id(“txtStationTo”)).sendKeys(Keys.ARROW_DOWN);
fd.findElement(By.id(“txtStationTo”)).sendKeys(Keys.ARROW_DOWN);
fd.findElement(By.id(“txtStationTo”)).sendKeys(Keys.ENTER);
Thread.sleep(2000);
new Select(fd.findElement(By.name(“lccp_classopt”))).selectByVisibleText(“THIRD AC”);
Thread.sleep(2000);
fd.findElement(By.name(“submit2”)).click();
}
}