Saturday, May 16, 2015

Basic Java Tutorials For Selenium WebDriver

  • Datatypes In Java
Let we start from different data types In java which we can use in our selenium webdriver test preparation.

In java or any other programming languages, data types represents that which type of values can be stored and what will be the size of that value or how much memory will be allocated. There are nearest eight different data types in java like byte, short, int, long, float, double, char, Boolean. byte and short datatypes are not much more useful in selenium webdriver so I am skipping them here to decrease your confusions.

int datatype
int data type is useful to store 32 bit integer (Example : 4523) values only. We can not store decimal (Example 452.23) values in int data type.
Example : 
int i = 4523;

long datatype
long datatype is useful to store 64 bit integer(Example : 652345) values. You can use it when your value is more larger and can not hold it in int. Same as int datatype, we can not store decimal (Example 452.23) values in long datatype
Example : 
long l = 652345;

double datatype

double datatype is useful to store 64 bit decimal(Example : 56.2354) values. We can store integer (Example 12456) values too in double datatype.
Example : 
double d1 = 56.2354;  
double d2 = 12456;

char datatype

char datatype is useful to store single character(Example : 'd'). It can not store more than one (Example 'dd') character in it.
Example : 
char c = 'd';

boolean datatype

boolean datatype is useful to store only boolean(Example : true) values.
Example : 
boolean b = true;

String Class
String is not a data type but it is a class which is useful to store string in variable.
Example :
String str = "Hello World";

VIEW DIFFERENT FUNCTIONS OF STRING CLASS

Created bellow given example for all these datatypes. Run it in your eclipse and verify results.
public class datatypes {

 public static void main(String[] args) {
  int i = 4523;   //Can store 32 bit integer values only.
  long l = 652345;  //Can store 64 bit integer values only.
  double d1 = 56.2354;         //Can store 64 bit decimal values.
  double d2 = 12456;  //We can use it for integer values too.
  char c = 'd';   //Can store single character only.
  boolean t = true;  //Can store only boolean values like true or false.
  String str = "Hello World";  //Can store any string values.
  
  System.out.println("Integer Var Is --> "+i);
  System.out.println("Long Var Is --> "+l);
  System.out.println("double Var d1 Is --> "+d1);
  System.out.println("double Var d2 Is --> "+d2);
  System.out.println("char Var c Is --> "+c);
  System.out.println("boolean Var b Is --> "+t);
  System.out.println("boolean Var str Is --> "+str);
 }
}

Bellow given result will display in your console at the end of execution.
Integer Var Is --> 4523
Long Var Is --> 652345
double Var d1 Is --> 56.2354
double Var d2 Is --> 12456.0
char Var c Is --> d
boolean Var b Is --> true
String Var str Is --> Hello World


  • String In Java


We have learnt about DIFFERENT DATA TYPES IN JAVA In my past post. Now, Many peoples are understanding that String Is also a data type. Let me correct them -> String Is not data type. If string Is not data types then what Is String? This question can be asked by Interviewer too. String Is an Inbuilt class of
java. String class has many Inbuilt functions which we can use to perform different actions on string.
If you don't know, Let me tell you that we have to work with strings very frequently In selenium WebDriver tests. So you must have knowledge of useful functions of String class to use them In your selenium webdriver test development. Let us take simple example of String to understand different methods of String class.
public class Strng_Example {

 public static void main(String[] args) {
  
  String st1 = "This World is Very Nice";
  String st2 = " And Beautiful.";
  
  //Comparing two strings. Return true If both match else return false.
  System.out.println("st1 equals to st2? -> "+st1.equals(st2));
  
  //Concatenates st2 with st1.
  System.out.println("Concatenation of st1 and st2 Is -> "+st1.concat(st2));
  
  //Retrieve the 9th Indexed character from string.
  System.out.println("Character at Index 9 Is -> "+st1.charAt(9));
  
  //Find the length of string.
  System.out.println("Length Of St1 -> "+st1.length());
  
  //Converting whole string In lower case.
  System.out.println("String In Lowercase -> "+st1.toLowerCase());
  
  //Converting whole string In upper case.
  System.out.println("String In uppercase -> "+st1.toUpperCase());
  
  //Retrieve the Index of first 'i' character.
  System.out.println("Index of 1st charater i Is -> "+st1.indexOf('i'));
  
  //Retrieve the index of 2nd most 'i' character.
  System.out.println("Index of 2nd charater i Is -> "+st1.indexOf('i', 3));
  
  //Retrieve the Index of word 'Very' from string.
  System.out.println("Index of word Very Is -> "+st1.indexOf("Very"));
  
  //Converting value From int to string.
  int j = 75;
  String val2 = String.valueOf(j);
  System.out.println("Value Of string val2 Is -> "+val2);
  
  //Converting string to integer.
  String val1="50";
  int i = Integer.parseInt(val1);
  System.out.println("Value Of int i Is -> "+i);
  
  //Print the String starting from 5th Index to 12th Index.
  System.out.println("Retrieving sub string from string -> "+st1.substring(5, 13));
  
  //Split string. 
  String splt[] = st1.split("Very");
  System.out.println("String Part 1 Is -> "+splt[0]);
  System.out.println("String Part 2 Is -> "+splt[1]); 

  //Trim String.
  System.out.println("Trimmed st2 -> "+st2.trim()); 
 }
}
If you will look at above example, I have prepared different examples of string method to take some action or we can say operations on string. Sort explanation of all above string methods are as bellow.
//Two String Comparison
To compare two strings, we can use syntax like st1.equals(st2). It will return True If both strings are same else It will return False.
//Two String Concatenation
Syntax st1.concat(st2) will concatenate st1 with st2.
//Retrieving Character from Index
Syntax st1.charAt(9)) will retrieve the character from string st1 located at 9th Index. Index Starts from 0.
//Length Of String
st1.length() will return lenght of string st1.
//Convert String In Lower Case Letters
st1.toLowerCase() will convert whole string letters In lower case.
//Convert String In UpperCase Letters
st1.toUpperCase() will convert whole string letters In upper case.
//Retrieving Index Of 1st most character
st1.indexOf('i') will retrieve Index of first most character 'i' from string.
//Retrieving Index Of 2nd most character
st1.indexOf('i', 3)) will retrieve Index of second most character 'i' from string. 3 described the from Index means It will start finding character 'i' from Index 3. First 'i' has Index 2 so we need to use 3 to find 2nd most character.
//Retrieving Index Of specific word from string
st1.indexOf("Very") will retrieve index of word 'Very' from string.
//Convert from Integer To String
String.valueOf(j) will convert value of 'j' from int to string.
//Convert from String To Integer
Integer.parseInt(val1) will conver value of 'val1' from string to int.
//Retrieving sub string from string
Syntax st1.substring(5, 13) will retrieve string from Index 5 To Index 13.
//Split String
String splt[] = st1.split("Very") will split string st1 from word 'Very' and store both strings In array.
//Trim String
If string has white space at beginning or end of the string then you can use trim function like st2.trim() to remove that white space.

Introduction to Selenium


Selenium is a portable software testing framework for web applications. Selenium provides a record/playback tool for authoring tests without learning a test scripting language (Selenium IDE). It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages, including JavaC#GroovyPerlPHPPython and Ruby. The tests can then be run against most modern web browsers. Selenium deploys on WindowsLinux, and Macintosh platforms. It is open-source software, released under the Apache 2.0 license, and can be downloaded and used without charge.