Selenium WebDriver Tutorial


Selenium WebDriver

> It is a most important tool in Selenium Suite(Others are Selenium IDE, Selenium RC and Selenium Grid).

> It is an Interface (Programming Interface) allows us to execute tests
against different browsers (ex: Firefox, IE, Chrome etc...)

> WebDriver enables us to use different programming languages to create tests.

Java
.NET
PHP
Perl
Python
Ruby
---------------------------
WebDriver Environment Setup:

> Download and Install Java (on OS(Ex: Microsoft Windows))

> set path Environment variable

> Download and unzip Eclipse software

> Download WebDriver Java language binding and Add webDriver jar files (in EClipse)

> Install Firebug plug-in for Firefox Browser (Install on FireFox) to inspect Web elements.
---------------------------------------------------
Navigation to Add WebDriver Jar files in eclipse:
Create Java Project
> Select src and right click
> Build Path
> Configure Build Path
> Select Libraries tab
> Click on "Add External JARs
> Browse path of the webDriver jars
> Add
-----------------------------------
Pre-requisites to create Automated Tests:
-----------------------------------------
> Import WebDriver Libraries and Firefox driver library
    
> Create driver object

> Using web element (Object) locaters(properties) and Methods, write object call statements.

> Insert java programmatic statements to enhance tests.
---------------------------------------------------------- 

WebDriver Sample Programs:
1) Launch Firefox Browser, Navigate to Google home, Capture the Title and Display.
package WebDriverSample;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GetBrowserTitle {
    public static void main(String [] args){
    WebDriver driver = new FirefoxDriver(); // Launches Firefox Browser with blank url
    driver.get("https://www.google.com"); // Navigates to the specified URL
    String Title = driver.getTitle();
    System.out.println(Title);
    driver.close(); // Closes the Browser
}
}
-------------------------------------------

2) Launch Firefox Browser, Navigate to Google home, Capture the Title and verify with expected:

package WebDriverSample;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GetBrowserTitle {
    public static void main(String [] args){
    WebDriver driver = new FirefoxDriver(); // Launches Firefox Browser with blank url
    driver.get("https://www.google.com"); // Navigates to the specified URL
    String actual = driver.getTitle();
   
    if (actual.contentEquals("Google")){
        System.out.println("Test Passed");
    }
    else {
        System.out.println("Test Failed");
    }
    driver.close(); // Closes the Browser
}
}
----------------------------------------
3) Launch Google Home page and click on Gmail link
package WebDriverSample;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GetBrowserTitle {
    public static void main(String [] args){
    WebDriver driver = new FirefoxDriver(); // Launches Firefox Browser with blank url
    driver.get("https://www.google.com");
    driver.findElement(By.linkText("Gmail")).click();
    //driver.close(); // Closes the Browser
}
}
--------------------------------------------------------
4) Gmail Login

/* Gmail Login
 * Steps:
 * i) Launch the Browser
 * ii) Navigate to Gmail Home page
 * iii) Enter Email
 * iv) Enter Password
 * v) Click on Sign in
 */
//---------------------------------------------
package WebDriverSample;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GmailLogin{
    public static void main (String []args){
        WebDriver abcd = new FirefoxDriver(); //i) Launch the Browser
        abcd.get("http:/www.gmail.com"); //ii) Navigate to Gmail Home page
        abcd.findElement(By.id("Email")).sendKeys("gcreddy123"); //Locating Email edit box and entering a value
        abcd.findElement(By.name("Passwd")).sendKeys("selenium123");//Locating Password edit box and entering a value
        abcd.findElement(By.id("signIn")).click(); //Click on Sign in
            }
}
------------------------------------------------
Selenium     UFT
Element -    Object
Locater        Property
--------------------------
// Close Browser

UFT:
'Close the Bower
Browser("title:=Google").Close

'Enter a Value into Edit box
Browser("title:=Google").Page("title:=Google").WebEdit("name:Email").Set "gcrindia"
-------------------------------------------
Selenium:

driver.close();
driver.findElement(By.id("Email")).sendKeys("gcrindia");

abcd.findElement(By.id("signIn")).click();
-------------------------------------------

1 comments:

Post a Comment