Selenium WebDriver Tutorial 1

Introduction to Selenium WebDriver

i) Introducing Selenium WebDriver

ii) Selenium WebDriver Environment Setup

iii) Write first Selenium Test Case
----------------------------------------
i) Introducing Selenium WebDriver

Selenium Tool's 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 Webdriver with Selenium RC in order to form more powerful tool called Selenium 2.0.

Selenium 1.0 + Selenium 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 is a most important tool in Selenium suite.

> It has Programming interface only, no IDE.
--------------------------
Selenium IDE - we can create Test cases using Recording features and enhance Test cases using Selenese commands. (No Programming)

Selenium WebDriver - we can create Test cases using Element locators and Webdriver methods/commands. And we can use programming features to enhance Test cases.

UFT/QTP - we can create Tests using Tool features (Recording) and enhance Test cases using Tool features (Checkpoints, Output values etc...) as well as Script/Program features (Conditional statements, Loop statements functions etc...).
-------------------------------------------------------
> Selenium WebDriver supports various programming languages to write programs (Test Scripts)

Java
C#
Python
Perl
Ruby
PHP

> 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 Windows

Linux

Macintosh etc...
-------------------------------------------
> Selenium WebDriver supports Data Driven Testing and Cross Browser Testing

> Selenium WebDriver is faster in execution than other tools of Selenium suite.

> Selenium WebDriver supports parallel test execution with the help of either JUnit or TestNG framework.

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

> Selenium WebDriver doesn't have IDE (takes more time and more efforts to create Test cases)

> No built in Result Reporting facility.

> No other tool integration for Test Management.
---------------------------------------------------
How we create Test Cases / Test Scripts / Tests in Selenium WebDriver

Selenium IDE:

Using Element locators and Selenese (Selenium IDE) Commands.

Selenium WebDriver

Using element Locators and WebDriver Methods /Commands.

UFT/QTP

Using Objects Information and Test Methods.
-------------------------------------------
Element Locators - to recognize elements /objects.

WebDriver Methods/Commands - to perform operations on elements.
----------------------------------------------------------
Java Programming - to enhance Test cases.

JUnit / TestNG Framework - to group Test cases, batch testing and generating Test Reports.
----------------------------------------------
ii) Selenium WebDriver Environment Setup

Steps:

i) Download and install Java (JDK) software. - To create and execute programs (Test scripts)

ii) Set Environment variable path (Path Variable) - To use Java from any directory.

iii) Download Eclipse IDE and extract. - To write and execute Java programs.
----------------------------------
iv) Download Selenium WebDriver Java Language binding (from www.seleniumhq.org) and add WebDriver jar files to Java project in Eclipse IDE.
------------------------------------------------ 
v) Install Firebug and Firepath plug ins for Mozilla Firefox Browser to inspect elements.

Or Use built in tools to inspect elements

Mozilla Firefox - Page Inspector

Chrome, IE - Developer tools
--------------------------------------------
vi) Firefox Driver is default driver in Selenium Webdriver, for IE, Chrome and Safari etc... Browsers we need to download browser drivers and set path.
-------------------------------------------------------------------------
Download Selenium WebDriver Java language binding and extract.

Add Selenium WebDriver jar files to Java project in Eclipse IDE.

Navigation:

Create Java Project
> Select Java project and right click
> Build path
> Configure Build path
> Select Libraries tab
> Click "external jars"
> Browse path of Selenium Webdriver jars.
> Add
--------------------------------------
Create Selenium WebDriver Test Case

> Import WebDriver and Firefox/IE/Chrome libraries. (In The Test Case/Program)

> Using Element locators and WebDriver Commands/methods write Test steps.

> Insert Java programming statements to enhance Test cases.
----------------------------------------
iii) Write first Selenium Test Case

Manual Test Case:

Test Case ID: gcrshop_admin_TC001

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

Test Steps:
i) Launch the Browser and navigate to "www.gcrit.com/build3/admin"
ii) Enter User name
iii) Enter Password
iv) Click "Login" Button

Input Data / Test Data:
User name =admin
Password = admin@123

Expected URL: "www.gcrit.com/build3/admin/index.php"

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

Test Result: Pass
----------------------------------------
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/login.php");//Naviage 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 the Browser
}
}
------------------------------------------------------------

Java Conditional Statements


Java Conditional Statements
 

Flow Control
    1) Conditional Statements
    2) Loop Statements
--------------------------------------
Conditional Statements

a) Usage of Conditional Statements in Test Automation

1) To insert Verification points

2) Error Handling
---------------------------------------
b) Two types of Conditional Statements
 

1) if Statement

2) switch Statement
---------------------------------------
c) Types of Conditions
 

1) Single Condition (Positive, Negative)
Ex:
Positive Condition
if (a > b) {
.
}
----------------------
Negative Conditionif (!(a < b)) {
.
}

2) Compound Conditionex:

if ((a > b) && (a > c)) {
.
}

Or

if ((a>b) || (a>c)){
.
}
----------------
if ((a>b) && (a>c) && (a>d)){
.
}
---------------------------------
3) Nested Condition
if (a>b) {
 if (a>c) {
  if (a>d) {
  }
 }
}
---------------------------------------
d) Usage of Conditional Statements

1) Execute a block of Statements when condition is True.
 

Syntax:

if (condition) {
Statements
----------
----------
----------
}

Example:
Positive Condition
int a =100, b =50;
if (a > b){
System.out.println("A is a Big Number");
}
-----------------------
Negative Condition
public static void main(String[] args) {
int a =100, b =50;
if (! (a < b)){
System.out.println("A is a Big Number");
}
}
---------------------------------------------------
2) Execute a block of statements when a Compound condition is True.
 

Syntax:

if ((condition) && Or || (condition)) {
Statements
----------
---------
}
---------------------------
Example:

public static void main(String[] args) {
int a =100, b =50, c =300;
if ((a > b) || (a > c)){
System.out.println("A is a Big Number");
}
}
------------------------------------------------------
3) Execute a block of statements when condition is true otherwise execute another block of statements.
 

Syntax:

if (Condition) {
Statements
----------
-----------
}
else
{
Statements
----------
-----------
}
----------------------
Example:

public static void main(String[] args) {
int a =100, b =100;
if (a > b){
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}
}
-----------------------------------------------------------
4) Decide among several alternates (else if)
 

Syntax:
if (condition) {
Statements
----------
---------
}
else if (condition) {
Statements
----------
---------
}
else if (condition) {
Statements
----------
---------
}
else if (condition) {
Statements
----------
---------
}
else
{
Statements
----------
---------
}
----------------------------------
Example:
Initialize an Integer variable and verify the range.

if the Number is in between 1 and 100 then display "Number is a Small Number"

if the Number is in between 101 and 1000 then display "Number is a Medium Number"

if the Number is in between 1001 and 10000 then display "Number is a Big Number"

if the Number is more than 10000 then display "Number is High Number"

Otherwise display "Number is either Zero or Negative Number"
-------------------------------------------------
public static void main(String[] args) {
int val = -100;

if ((val >= 1) && (val <= 100)){
System.out.println("Value is a Small Number");
}
else if ((val > 100) && (val <= 1000)) {
System.out.println("Value is a Medium Number");
}
else if ((val > 1000) && (val <= 10000)) {
System.out.println("Value is a Big Number");
}
else if (val > 10000) {
System.out.println("Value is High Number");
}
else {
System.out.println("Value is either Zero or Negative Value");
}
}
------------------------------------------------------------------
5) Execute a block of statements when more than one condition is True (Nested if)
 

Syntax:
if (condition){
 if (condition) {
  if (condition){
Statements
-----------
-----------
----------
}
}
}
---------------------------------
Example:
Check if the value of "a" variable is bigger than , c, d variable values or not?

public static void main(String[] args) {
int a =100, b =90, c=80, d=70;

if (a>b){
 if (a>c){
  if (a>d){
  System.out.println("A is a Big Number"); 
  }
  else{
 System.out.println("A is Not a Big Number");  
  }
 }
 else{
 System.out.println("A is Not a Big Number");     
 }
}
else {
System.out.println("A is Not a Big Number");     
}

}
-----------------------------------------------------------
Using Compound Condition
 

public static void main(String[] args) {
int a =100, b =90, c=80, d=700;

if ((a>b) && (a>c) && (a>d)){
 System.out.println("A is a Big Number"); 
 }
else{
 System.out.println("A is Not a Big Number");  
  }

}
-----------------------------------------------------
Advantages of Nested condition over Compound condition
 

In Nested Condition we can write multiple else parts

In Compound condition single else part only.
-------------------------------------------------------
Problem: Get Biggest Number out of Four numbers.

Solution:

public static void main(String[] args) {
int a =100, b =90, c=80, d=700;

if ((a>b) && (a>c) && (a>d)){
System.out.println("A is a Big Number");
}
else if((b>a) && (b>c) && (b>d)){
System.out.println("B is a Big Number");
}
else if((c>a) && (c>b) && (c>d)){
System.out.println("C is a Big Number");
}
else{
System.out.println("D is a Big Number");
}
}
-------------------------------------------------------------
6) Decide among several alternates (using switch statement)
 

Syntax:
switch (expression){
case value:
Statements
-----------
------------
--------
break;
case value:
Statements
-----------
------------
--------
break;
case value:
Statements
-----------
------------
--------
break;

default:
Statements
------------
------------
---------
}

Example:


public static void main(String[] args) {
char grade ='X';

switch (grade){
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Well Done");
break;
case 'C':
System.out.println("Better");
break;

default:
System.out.println("Invalid Grade");
}
   
}
---------------------------------------------------------