Selenium WebDriver Tutorial for Beginners


Selenium WebDriver Tutorial for Beginners

i) Introduction to Selenium WebDriver

ii) Selenium WebDriver Environment Setup

iii) Write first Selenium Test Case
---------------------------------------------------
i) Introduction to Selenium WebDriver

Selenium's Tool Suite
Selenium IDE
Selenium RC
Selenium WebDriver
Selenium Grid
--------------------------------------
Selenium WebDriver:

> In 2006, Selenium WebDriver was launched at Google.

> In 2008, the whole Selenium team decided to merge Selenium RC with Selenium WebDriver in order to form more powerful tool called Selenium 2.0

Selenium 1.0 + WebDriver = Selenium 2.0

Selenium 1.0
(Selenium IDE + Selenium RC + Selenium Grid)

Selenium 2.0
(Selenium IDE + Selenium RC + Selenium WebDriver + Selenium Grid)

Note: Now Selenium RC is only for maintenance Projects.
----------------------------------------
Selenium WebDriver Features:

> It has Programming Interface only, no IDE
----------------------------------------
 Selenium IDE : 
We can create Test cases using Recording feature and enhance Test cases using Selenese Commands/ Selenium IDE Commands.

Using Element Locators and Selenese Commands we create Test cases in Selenium IDE.

No Programming in Selenium IDE.

Selenium WebDriver:

We create Test Cases using Element Locators and WebDriver Methods/Commands, Enhance Test cases using programming features.

No IDE in Selenium WebDriver.

UFT/QTP:

We create Tests using Tool features (Ex: Recording etc...)and enhance Tests using Tool features (Ex: Checkpoints, Output values, Data Table etc...) and Script/Programming features (Ex: Conditional Statements, Loop Statements, Functions etc...).

IDE as well as Programming Interface.
---------------------------------------------
Test Design

Generate Basic Tests/Test Cases
Enhance Tests
----------------------------------------------
General

Object Property Value Operations

Dog name abcd Run, Jump etc...
height 40 Cm
color black
weight etc... 10 Kg

Man name
surname
color
weight
Age etc...
--------------------------------------------------------------
UFT

Object Property Value Operations

Link text Gmail Click, check enabled status etc...
height
enabled true
visible true
x


Selenium

Element Locator Value Operations

Edit box id Email Enter a Value, Check enabled status
name
xpath 
-------------------------------------------------------------------------------------
> Selenium WebDriver supports various programming languages to write Test scripts.

Java
C#
Python
PHP
Ruby
Perl

> Selenium WebDriver supports various Browsers to create and execute Test Cases

Mozilla Firefox

Google Chrome

IE

Safari

Opera etc...

> Selenium WebDriver supports various operating environments

MS Window
Linux
Macintosh etc...

> Selenium WebDriver supports Data Driven Testing and Cross Browser Testing.

> Selenium WebDriver is faster in test execution when it compares to other Functional Test tools.

> Selenium supports parallel test execution.

> Selenium WebDriver supports Batch Testing with the help of either JUnit or TestNG Framework.
----------------------------------------------
Drawbacks of Selenium WebDriver:

> Selenium WebDriver doesn't have IDE (No Tool features), so creating test cases takes more time and efforts.

> Selenium WebDriver supports only Web Applications (doesn't support Desktop Applications)

> No built in Result Reporting facility

> No reliable Technical support

> Limited support for Image Testing

> No other tool integration for Test Management.
---------------------------------------------------
ii) Selenium WebDriver Environment Setup

Steps:

1) Download and Install Java (jdk) software - To create and execute programs (Test Scripts)

2) Set Environment variable (Path Variable) - Optional (only for Command line)

3) Download Eclipse IDE and Extract. - To write and execute Java programs.
--------------------------------------------------------
4) Download Selenium WebDriver Java Language binding from www.seleniumhq.org and Add WebDriver
jar files to Java Project in Eclipse IDE.
----------------------------------------
ii) Selenium WebDriver Environment Setup:

Download Selenium WebDriver Java Language binding and extract.

Add Selenium WebDriver Java langauge binding to Java Project in Eclipse

Navigation:

> Create Java Project
> Select Java Project and Right click
> Build path
> Configure Bulid path
> Select Libraries Tab
> Click "External Jars"
> Browse path of Selenium WebDriver jars
> Add
---------------------------------------------------
iii) Create Selenium WebDriver Test Case:

Manual Test Case:

a) Test Case ID: gcrShop_admin_TC001

b) Test Case Name: Verify Admin Login in GCR Shop web portal.

c) Test Steps:
1) Launch the Browser and Navigate to "http://www.gcrit.com/build3/admin/"
2) Enter Username
3) Enter Password
4) Click "Login" Button
-----------------------
d) Verification Point/s

Capture URL after Login and compare with expected.

Expected: http://www.gcrit.com/build3/admin/index.php

Actual: http://www.gcrit.com/build3/admin/index.php

e) Input Data / Test Data

Username = "admin"
Password = "admin@123"

f) Result: Pass
--------------------------------------------
Inspect Elements

Mozilla Firefox - Page Inspector (Built in Feature) to inspect Elements/ Download and Install Firebug Plug in

Google Chrome - Developer Tools (Built in) / F12

IE - Developer Tools (Built-in) / F12
-------------------------------------------------------
Selenium WebDriver Test Case:

public static void main(String[] args) {
WebDriver driver= new FirefoxDriver();//Launches Firefox browser with blank url
driver.get("http://www.gcrit.com/build3/admin/"); //Navigates to Admin Home page
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();

String URL = driver.getCurrentUrl();

if (URL.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Login Successful - Passed");
}
else {
System.out.println("Login Unsuccessful - Failed");
}
driver.close();//Closes Browser
}
}
-----------------------------------------------------

Java for Selenium Part 11

Java Abstraction and Encapsulation
Java Object Oriented Program

i) Inheritance

ii) Polymorphism

iii) Abstraction

iv) Encapsulation
-------------------------------------------
iii) Abstraction
-------------------------------------------
> It is a Process of hiding implementation details and showing only functionality to the user

Two types of Methods

i) Concrete Methods (The Methods which are having body)

Syntax:

accessModifier retrunType / returnTypeNothing method Name() {
Statements
----------
-----------
-------------
}

ii) Abstract Methods (The Methods which are not having body)

Syntax:

accessModifier abstract returnType / returnTypeNothing MethodName();

> If we know the method name, but don't know the method functionality then we go for Abstract Methods

> Java Class contains 100% concrete Methods.

> Abstract Class contains one or more abstract methods.
------------------------------------
Example:

Class1 (having 10 methods)

10 Methods are concrete Methods.

It is a Java Class
------------------------------
Class2 (having 10 methods)

5 Methods are concrete Methods and 5 methods are abstract methods)

It is an Abstract Class
--------------------------------
Class3 (having 10 Methods)

All Methods are Abstract Methods

It is an Abstract Class
--------------------------------
Example for Abstract Class:

public abstract class Bikes {

public void handle(){
System.out.println("Bikes have Handle");
}
   
public void seat(){
System.out.println("Bikes have Seats");
}

public abstract void engine();

public abstract void wheels();

public static void main(String[] args) {
//Bikes obj = new Bikes();
   
}
}
--------------------------------
Note: We cannot create Object/Instance in Abstract Class.

Note 2: In order to use Methods from Abstract Class then first we need to implement abstract methods
in sub class.

Using methods from Abstract Class:

public class HeroHonda extends Bikes{

public void engine() {
System.out.println("Bikes have Engine");
}

public void wheels() {
System.out.println("Bikes have Wheels");
}

public static void main (String [] args){
HeroHonda abc = new HeroHonda();

abc.seat();
abc.engine();
abc.wheels();
abc.handle();
}
}
-------------------------------------
Calling Methods in Abstract Class

public abstract class Bikes {

public void handle(){
System.out.println("Bikes have Handle");
}
   
public void seat(){
System.out.println("Bikes have Seats");
}

public abstract void engine();

public abstract void wheels();

public static void main(String[] args) {
//Bikes obj = new Bikes();
HeroHonda xyz = new HeroHonda();
xyz.engine();
xyz.wheels();
}
}
------------------------------------------
How to inherit Abstract Class Methods?

    By implementing abstract methods in Sub Class.

How to call Methods in Abstract Class?

    By creating the Object using Sub Class.
---------------------------------------------
Java Interfaces

> Interface is a Java type definition block which is 100% abstract.

i) Class vs. Abstract Class

Java Classes have 100% concrete Methods

Abstract Classes have one or more/all abstract methods.
------------------------------------
ii) Class vs. Interface

Java Classes have 100% concrete Methods

Java Interfaces have 100% Abstract Methods
------------------------------------
iii) Abstract Class vs. Interface

Abstract Classes have one or more/all abstract methods.

Java Interfaces have 100% Abstract Methods
-----------------------------------------------
> All Interface Methods by default public and abstract.

> static and final modifiers are not allowed for Interface methods

> In Interfaces Variables have to initialize at the time of declaration only.

Ex:

int a;//Incorrect
a=100;

int b=200;//Correct

> In Interfaces Variables are public and final bydefault.

> Interfaces are going to be used using "implements" keyword.
-----------------------------------------------
Create Java Interface

Java Project
    Java Package
        Java Class / Java Interface

Example:

public interface Interface1 {
   
public void engine();
public void seat();
public void wheels();
public void handle();
}

Reuse Methods from Interface to Class

public class ClassNew implements Interface1 {

public void engine() {
System.out.println("Bikes have Engine");
}

public void seat() {
System.out.println("Bikes have Seat");       
}

public void wheels() {
System.out.println("Bikes have Wheels");
}
public void handle() {
System.out.println("Bikes have Handle");   
}
public static void main (String [] args){
ClassNew obj = new ClassNew();
obj.seat();
obj.wheels();
obj.handle();
}
}
--------------------------------------------
Call methods in Interface

public interface Interface1 {
   
public void engine();
public void seat();
public void wheels();
public void handle();

public static void main (String [] args){
ClassNew abc = new ClassNew();   
abc.handle();
abc.wheels();
}
}
-----------------------------------------------
Reuse Methods from a Class to another Class - using "extends" keyword

Reuse Methods from an Abstract Class to Class - using "extends" keyword

Reuse Methods from an Interface to Class - using "implements" keyword
---------------------------------------
Assignment

How to reuse Methods from an Interface to another Interface?
-------------------------------------------
iv) Encapsulation

It is a process of wrapping code and data into a single unit.

General Example:

Capsule (Mixer of several medicines)

Encapsulation is the technique making the fields in a class private and providing access via public methods.

> It provides control over the data.

> By providing setter and getter methods we can make a class read only or write only.
-------------------------------------
Example:

Class 1:

public class Class1 {
private String name = "Test Automation";

public String getName(){
return name;
}

public void setName(String newName){
name=newName;   
}

public static void main(String[] args) {
Class1 obj = new Class1();
obj.setName("Selenium with Java");
System.out.println(obj.getName());
}
}
----------------------------------
Class 2:
public class Class2 extends Class1 {

public static void main(String[] args) {
Class2 obj2 = new Class2();
//obj2.setName("Selenium with Java");
System.out.println(obj2.getName());
}
}
--------------------------------------
Java for Selenium the Conclusion

Java Environment Setup
Java Program Structure / Java Syntax
---------------------------------------
A) Java Fundamentals / Basics

1) Comments
2) Data Types
3) Modifiers
4) Variables
5) Operators
6) Flow Control
    Conditional Statements
    Loop Statements
8) String Handling
9) Arrays
10) IO Operations, and File Handling
11) Methods
    Built in Methods
    User Defined Methods
13) Exception Handling
---------------------------------
B) Java OOPS

1) Inheritance
2) Polymorphism
3) Abstraction
4) Encapsulation
-------------------------------------------

Java Programming for Selenium

Java Programming for Selenium

1) Java for Selenium Part 1


(Java Environment Setup on MS Windows, Java Syntax, and Java Program Structure)
-----------------------------------------------------
2) Java for Selenium Part 2


(Java Programming Example, Comments in Java Programs)
----------------------------------------------------- 
3) Java for Selenium Part 3

(Java Data Types, Modifiers and Variables in Java) 
-----------------------------------------------------
4) Java for Selenium Part 4

(Java Operators and Conditional Statements)
-----------------------------------------------------
5) Java for Selenium Part 5

(Java Loop Statements, String Handling in Java)
-----------------------------------------------------
6) Java for Selenium Part 6

(Java Arrays, Input and Output Operations in Java)
-----------------------------------------------------
7) Java for Selenium Part 7

(Java exception Handling, File Handling in Java)
-----------------------------------------------------
8) Java for Selenium Part 8

(User Defined Methods in Java)
-----------------------------------------------------
9) Java for Selenium Part 9

(Java Built-in Methods)
----------------------------------------------------- 
10) Java for Selenium Part 10

(Java Inheritance and Polymorphism)
-----------------------------------------------------
11) Java for Selenium Part 11

(Java Abstraction, Interfaces, Encapsulation)
-----------------------------------------------------
Java for Selenium the Conclusion
----------------------------------------------------- 
Programming for Test Automation (To Write Test Scripts)

Selenium supports 6 Programming Languages (Java, C#.NET, Python, Perl, Ruby and PHP), we can use any one of those languages to write Test Scripts in Selenium.

• Java Fundamentals (Data Types, Modifiers, Variables, Operators, Flow Control Statements, Input and Output Operations, String Handling, Arrays, Exception Handling, File Handling, Built-in Methods, and User defined Methods) 
And Java OOPS (Inheritance, Polymorphism, Abstraction and Encapsulation) are enough for Test Automation with Selenium.

Use Java Programming for writing Test Scripts.  

Why Java? Why can't we use other Languages?

•  Selenium written in Java, It doesn't mean that Java only more compatible with Selenium, we can use other supported programming languages also.•  Majority of Selenium Testers (nearly 77%) using Java to write Test Scripts, so knowledge sharing is very easy.•  Good support for Selenium with Java, You can get more Help documentation and code implementations from internet when it compares to other supported languages.•  Java is platform independent language, we can use on any Operating environment.  
----------------------------------------------------------------
Related Posts:

Selenium Step by Step Videos

Selenium Quick Guide

Java for Selenium Part 10


Java Inheritance and Polymorphism

Java OOPS - Object Oriented Programming System

Four Fundamentals of OOPS

i) Inheritance

ii) Polymorphism

iii) Abstraction

iv) Encapsulation
--------------------------------------------------------
i) Inheritance

> It is a process of Inheriting (reusing) the class members (Variables and Methods) from one Class to another.

> The Class where the class members are getting inherited is called as Super Class/Parent Class/Base Class

> The Class to which the class members are getting inherited is called as Sub Class/Child Class/Derived Class.

> The Inheritance between Super Class and Sub Class is achieved using "extends" keyword.
-----------------------------------------
Static vs. Non Static Methods

> Instance Variables can't be used in Static Methods, but we can use Static and Instance Variables
in Non Static methods.

> Non Static Methods can't be called within the Static Methods, but we can call Static and Non Static Methods within the Non Static Methods.
------------------------------------------- 
Example:

public class Class1 {
//Static Variable
static int a =10;
//Instance Variable
int b=20;
//Static Method
public static void abc(){
System.out.println(a);
}
//Non Static Method
public void abc2(){
System.out.println(a + b);
}
public static void abc3(){
System.out.println("It is a Staic Method");
//abc2();// We can't Access Non Static Methods
abc();//Access Static Method with in Static Method
}
public void abc4(){
System.out.println("It is a Non Static Method");
abc2();//Access Non Static method with in Non Static Method
abc();//Access Static Method within Non Static Method
}

public static void main(String[] args) {
abc();
abc3();

Class1 obj = new Class1();
obj.abc2();
obj.abc4();
}
}
--------------------------------
Types of Inheritance

i) Single Inheritance

Ex: 

ClassB extends ClassA 

ClassA - Super Class
ClassB - Sub Class

ii) Multi Level Inheritance

Ex:

ClassB extends ClassA
ClassC extends ClassB

ClassC - Sub-sub Class / Child Class
ClassB - Parent Class for ClassC, Child Class for ClassA
ClassA - Grand Parent Class for ClassC, Parent Class for ClassB

iii) Multiple Inheritance (* Java Doesn't support)

Ex:

ClassB extends ClassA
ClassB extends ClassD
--------------------------------
Example:

Class 1

public class Class1 {
int a =10;
int b =20;

public void add(){
System.out.println(a+b);
}

public static void main(String[] args) {
Class1 obj = new Class1();
obj.add();
System.out.println(obj.a);
}
}
--------------------------------
Class 2 (With Inheritance, Create Object using Sub class only)

public class Class2 extends Class1{

public static void main(String[] args) {
Class2 obj2 = new Class2();
obj2.add();
System.out.println(obj2.a);
}
}
--------------------------------
(Without Inheritance, Create Object using Super class in the same Package)

public class Class2 {

public static void main(String[] args) {
Class1 obj2 = new Class1();
obj2.add();
System.out.println(obj2.a);
}
}
--------------------------------
(Without Inheritance, Create Object using Super class in another Package by importing the Package)

import abcd.Class1;

public class Class3 {

public static void main(String[] args) {
Class1 obj3 = new Class1();
obj3.add();
}
}
--------------------------------
(With Inheritance, Create Object using Child class in another Package by importing the Package)


import abcd.Class1;

public class Class3 extends Class1 {

public static void main(String[] args) {
Class3 obj3 = new Class3();
obj3.add();
}
}
--------------------------------
(Without Inheritance, Create Object using Super Class in another Project)

import abcd.Class1;

public class Class4 {

public static void main(String[] args) {
Class1 obj4 = new Class1();
obj4.add();
}
}
--------------------------------
(With Inheritance, Create Object using Child Class in another Project)

import abcd.Class1;

public class Class4 extends Class1 {

public static void main(String[] args) {
Class4 obj4 = new Class4();
obj4.add();
}
}
--------------------------------
Example for Multi Level Inheritance

Class 1

public class Class1 {
int a =10;
int b =20;

public void add(){
System.out.println(a + b);
}
public static void main(String[] args) {
Class1 obj = new Class1 ();
obj.add();
}
}
--------------------------------
Class 2:

public class Class2 extends Class1 {
int a =1;
int b =2;

public void add(){
System.out.println(a + b);
}

public static void main(String[] args) {
Class2 obj2 = new Class2();
obj2.add();
}
}
--------------------------------
Class 3:

public void add(){
System.out.println(a + b);
}

public static void main(String[] args) {
Class3 obj3 = new Class3();
obj3.add();//300

Class2 obj4 = new Class2();
obj4.add();//3

Class1 obj5 = new Class1();
obj5.add();//30

}
}
----------------------------------------------
ii) Polymorphism

Existence of Object behavior in many forms

Two types of Polymorphism

1) Compile Time Polymorphism / Method Overloading

2) Run-Time Polymorphism / Method Overriding
--------------------------------------------
1) Compile Time Polymorphism / Method Overloading

If two or more Methods with same name in the same class but they differ in following ways.

a) Number of Arguments

b) Type of Arguments
--------------------------------
Example for Method Overloading:

public class Class1 {
public void add(int a, int b){
System.out.println(a+b);
}
public void add(int a, int b, int c){
System.out.println(a+b+c);
}
public void add(double a, double b){
System.out.println(a+b);
}

public static void main(String[] args) {
Class1 obj = new Class1();
obj.add(10, 20);//30
obj.add(10.234, 4.567);
obj.add(10, 20, 60);//90
}
}
--------------------------------------
Example2:

Class 1

public class Class1 {

public void add(int a, int b){
System.out.println(a+b);
}

public static void main(String[] args) {
Class1 obj = new Class1();
obj.add(10, 20);//30
}
}
-----------------------------------
Class 2:

public class Class2 extends Class1 {
public void add(){
int a =12, b=13;
System.out.println(a + b);
}

public static void main(String[] args) {
Class2 obj2 = new Class2();
obj2.add();
obj2.add(10, 20);
}
}
--------------------------------
2) Run-Time Polymorphism / Method Overriding

If two or more methods with same name available in the Super Class and Sub Class.

Example for Method Overriding

public class Class1 {

public void add(){
int a =100, b=200;
System.out.println(a+b);
}

public static void main(String[] args) {
Class1 obj = new Class1();
obj.add();
}
}
------------------------------
public class Class2 extends Class1 {
public void add(){
int a =12, b=13;
System.out.println(a + b);
}

public static void main(String[] args) {
Class2 obj2 = new Class2();
obj2.add(); //25
}
}
----------------------------------

Java for Selenium Part 9


Java Built-in Methods

Categories of Built in Methods

i) String Methods

ii) Number Methods

iii) Character Methods

iv) Array Methods

etc...
-----------------------------------------------------
i) String Methods

1) CompareTo() Method

It compares two strings and supports 3-way comparison

Result Criteria for 3-way comparison (Strings)

if string1 = string2 then 0
if string1 > string2 then >0/Positive value
if string1 < string2 then <0 / Negative Value

Comparing Numbers vs. Comparing Strings

Comparing Numbers based on Values

int a =10, b=20;
if (a>b){
Statements
---------
------
}

Comparing Strings based on ANSI Character codes

A-Z -(65 to 90)
a-z (97 to 122)
0-9(48 to 57)

Example:

public static void main(String[] args) {
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "seleniuma";
String str4 ="selenium";

System.out.println(str1.compareTo(str2));//>0 / Positive value
System.out.println(str1.compareTo(str3));//<0 / Negative Value
System.out.println(str1.compareTo(str4));//0
}
--------------------------------
2) equals() Method

Compares two strings and supports 2-way comparison

Result Criteria for 2-way comparison

if string1 = string2 then true
if string1 != string2 then false

Example:

public static void main(String[] args) {
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "seleniuma";
String str4 ="selenium";

System.out.println(str1.equals(str2));//false
System.out.println(str1.equals(str4));//true
System.out.println(str1.equals("SELENIUM"));//false

System.out.println(str1 == str2);//false
System.out.println(str1 == str4);//true
}
--------------------------------
3) concat()

It concatenates two strings

public static void main(String[] args) {
String str1 = "Selenium ";
String str2 = "Testing";

System.out.println(str1.concat(str2));//Selenium Testing
System.out.println(str1 + str2);//Selenium Testing
}
--------------------------------
4) charAt() method

Returns a Character value by index

Example:

public static void main(String[] args) {
String str1 = "Selenium ";

System.out.println(str1.charAt(4));//n
System.out.println(str1.charAt(40));//Run-time Error
--------------------------------
5) equalsIgnoreCase()

It compares two strings and supports 2-way comparison

It ignores cases (Upper Case or Lower Case)

Example:

public static void main(String[] args) {
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "UFT";

System.out.println(str1.equalsIgnoreCase(str2));//true
System.out.println(str1.equalsIgnoreCase(str3));//false
}
--------------------------------
6) toUpperCase()

It converts values to Upper case

Example:

public static void main(String[] args) {
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "SELEnium";
String str4 = "selenium123";

System.out.println(str1.toUpperCase());//SELENIUM
System.out.println(str2.toUpperCase());//SELENIUM
System.out.println(str3.toUpperCase());//SELENIUM
System.out.println(str4.toUpperCase());//SELENIUM123
System.out.println(str5.toUpperCase());//S*&^
}
--------------------------------
7) toLowerCase()

Converts values to Lower case and it ignores Numbers and special characters

Example:

public static void main(String[] args) {
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "SELEnium";
String str4 = "selenium123";
String str5 ="S*&^";

System.out.println(str1.toLowerCase());//selenium
System.out.println(str2.toLowerCase());//selenium
System.out.println(str3.toLowerCase());//selenium
System.out.println(str4.toLowerCase());//selenium123
System.out.println(str5.toLowerCase());//s*&^
--------------------------------
8) trim()

Removes spaces from both the sides of a String

Example:

public static void main(String[] args) {
String str1 = "          Selenium              ";

System.out.println(str1);
System.out.println(str1.trim());
}
--------------------------------
9) substring()

It returns sub string based on index

Example:

public static void main(String[] args) {
String str = "Welcome to Selenium Testing";

System.out.println(str.substring(11));//Selenium Testing
System.out.println(str.substring(20));//Testing
System.out.println(str.substring(11, 19));//Selenium
}
--------------------------------
10) endsWith()

Ends with specified suffix, and provides Logical (true/false) Result.

Example:

public static void main(String[] args) {
String str = "Welcome to Selenium Testing";

System.out.println(str.endsWith("Selenium Testing"));//true
System.out.println(str.endsWith("Testing"));//true
System.out.println(str.endsWith("ing"));//true
System.out.println(str.endsWith("Selenium"));//false
}
--------------------------------
11) length

Returns length of a String

Example:

public static void main(String[] args) {
String str1 = "Selenium Testing";
String str2 = "Selenium";
System.out.println(str1.length());//16
System.out.println(str2.length());//8
}
-----------------------------------------------------
ii) Number Methods

//Integer Class wraps a value of primitive datatype int in an Object
//An Object of Integer contains a single field whose type is int
--------------------------------
1) compareTo()

Compares two integers and supports 3-way comparison

Example:

Result Criteria for 3 way Comparison (Integers/Numbers)

if Integer1 = Integer2 then 0
if Integer1 > Integer2 then 1
if Integer1 < Integer2 then -1

public static void main(String[] args) {
int a = 5;
Integer b =a;

System.out.println(b.compareTo(7));//-1
System.out.println(b.compareTo(5));//0
System.out.println(b.compareTo(4));//1
--------------------------------
2) equals()

Supports 2 way comparison

Example:

public static void main(String[] args) {
int a = 5;
Integer b =a;

System.out.println(b.equals(7));//false
System.out.println(b.equals(5));//true
}
--------------------------------
3) abs()

Returns absolute value

Example:

public static void main(String[] args) {
double a = 10.456;
double b = 10.786;
double d = -12.34;
int c=7;
int e = -100;

System.out.println(Math.abs(a));//10.456
System.out.println(Math.abs(b));//10.786
System.out.println(Math.abs(c));//7
System.out.println(Math.abs(d));//12.34
System.out.println(Math.abs(e));//100
}
--------------------------------
4) round()

Rounds the value to nearest integer

Example:
public static void main(String[] args) {
double a = 10.456;
double b = 10.786;
double d = -12.34;
int c=7;
int e = -100;

System.out.println(Math.round(a));//10
System.out.println(Math.round(b));//11
System.out.println(Math.round(c));//7
System.out.println(Math.round(d));//-12
System.out.println(Math.round(e));//-100
}
--------------------------------
5) min()

Returns minimum value between two numbers

example:

public static void main(String[] args) {
double a = 10.456;
double b = 10.786;
int c =10;
int d= 8;

System.out.println(Math.min(a, b));//10.456
System.out.println(Math.min(c, d));//8
System.out.println(Math.min(5, 4));//4
System.out.println(Math.min(1.234, 2.123));//1.234
System.out.println(Math.min(-10, -8));//-10
System.out.println(Math.min(-1.234, -2.123));//-2.123
}
}
--------------------------------
6) max()

Returns maximum value between two numbers

Example:

public static void main(String[] args) {
double a = 10.456;
double b = 10.786;
int c =10;
int d= 8;

System.out.println(Math.max(a, b));//10.786
System.out.println(Math.max(c, d));//10
System.out.println(Math.max(5, 4));//5
System.out.println(Math.max(1.234, 2.123));//2.123
System.out.println(Math.max(-10, -8));//-8
System.out.println(Math.max(-1.234, -2.123));//-1.234
}
--------------------------------
7) random()

Generates a Random Number

Example:

public static void main(String[] args) {
System.out.println(Math.random());//
}
-----------------------------------------------------
iii) Character Methods

// The Character class wraps a value of primitive data type char in an object

1) isLetter()

Checks if the value is Alpha byte or not? and returns Logical (true/false) result.

public static void main(String[] args) {
char a ='A';
char b ='a';
char c ='1';
char d = '*';

System.out.println(Character.isLetter(a));//true
System.out.println(Character.isLetter(b));//true
System.out.println(Character.isLetter(c));//false
System.out.println(Character.isLetter(d));//false
System.out.println(Character.isLetter('Z'));//true
System.out.println(Character.isLetter('7'));//false
}
--------------------------------
2) isAlphabetic()

Checks if the value is Alpha byte or not? and returns Logical (true/false) result.

Example:

public static void main(String[] args) {
char a ='A';
char b ='a';
char c ='1';
char d = '*';

System.out.println(Character.isAlphabetic(a));//true
System.out.println(Character.isAlphabetic(b));//true
System.out.println(Character.isAlphabetic(c));//false
System.out.println(Character.isAlphabetic(d));//false
System.out.println(Character.isAlphabetic('Z'));//true
System.out.println(Character.isAlphabetic('7'));//false
}
--------------------------------
Assignment:

What is the difference between isLetter and isAlphabetic methods.
--------------------------------
3) isDigit

Checks if the value is Number or not?

public static void main(String[] args) {
char a ='A';
char b ='1';

System.out.println(Character.isDigit(a));//false
System.out.println(Character.isDigit(b));//true
System.out.println(Character.isDigit('B'));//false
System.out.println(Character.isDigit('9'));//true
}
--------------------------------
4) isUpperCase()

Checks if the value is Upper Case or not?

5) isLowerCase

Checks if the value is Lower Case or not?

Examples:

public static void main(String[] args) {
char a ='A';
char b ='a';
char c ='1';

System.out.println(Character.isUpperCase(a));//true
System.out.println(Character.isUpperCase(b));//false
System.out.println(Character.isUpperCase(c));//false

System.out.println(Character.isLowerCase(a));//false
System.out.println(Character.isLowerCase(b));//true
}
-----------------------------------------------------
iv) Array Methods

1) length()

It returns length of an Array

Example:

public static void main(String[] args) {
int [] array1 = {10, 20, 30, 40, 5};
System.out.println(array1.length);//5
}
--------------------------------
2) toString()

Prints an Array

public class Class1 {

public static void main(String[] args) {
String [] array1 = {"Selenium", "UFT", "RFT", "SilkTest"};

String str = Arrays.toString(array1);
System.out.println(str); //[Selenium, UFT, RFT, SilkTest]

for (int i=0; i < array1.length; i++){
System.out.println(array1[i]);
}
--------------------------------
3) contains()

Checks if the Array contains certain value or not?, retruns boolean result.

Example:


public static void main(String[] args) {
String [] array1 = {"Selenium", "UFT", "RFT", "SilkTest"};
boolean a = Arrays.asList(array1).contains("UFT");//
System.out.println(a);//true

System.out.println(Arrays.asList(array1).contains("Selenium"));//true
System.out.println(Arrays.asList(array1).contains("Java"));//false
}
--------------------------------
Method Syntax:

object.method();
class.method();
Class/object.property.method();
etc...
-----------------------------------------------------