Selenium WebDriver Quick Tutorial

Selenium WebDriver Quick Tutorial

i) Introduction to Selenium WebDriver

ii) Selenium WebDriver Environment Setup

iii) Web Elements and Operations

iv) Element Locators

v) WebDriver API Commands
------------------------------------------------
i) Introduction to Selenium WebDriver

> Selenium WebDriver is a programming Interface, no IDE

> Selenium WebDriver supports various Programming Languages to write programs (Test Scripts)

> Selenium WebDriver supports various Operating Environments.

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

> Using Element Locators and WebDriver Commands we can create Test Cases.

> Its support Batch Testing, Cross Browser Testing and Data Driven Testing
--------------------------------
Drawbacks of Selenium WebDriver

> It doesn't have IDE, so difficult to create Test cases and takes more time.

> It doesn't have built-in Result Reporting facility.

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

Steps:

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

ii) Download Eclipse IDE and extract. - To write and execute Java programs.
----------------------------------
iii) Download Selenium WebDriver Java Language binding (from www.seleniumhq.org) and add WebDriver jar files to Java project in Eclipse IDE.
------------------------------------------------  
v) Firefox Driver is default driver in Selenium Webdriver, for IE, Chrome and Safari etc... Browsers we need to download browser drivers and set path.
--------------------------------------------
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

Verification Point:
Capture the Url (Actual) after Login to Application and Compare with Expected.

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

Test Result: 
------------------------------------------------
1) Element Locators - To Identify the Elements

Example for Elements: Link, edit box, Button etc...
Examples for Operations on Elements: Click, Enter a Value, Check, select etc...

2) WebDriver Commands - Perform Operations on Elements

3) Java Programming - To enhance Test cases
-------------------------------------------------------------
Object / Element Property/Locator Value
Man name Santosh
height 6
-------------------------------------------------------------
Object name abc
Dog color white
weight 5
-------------------------------------------------------------
Edit box name Email
Button id tdb1
-------------------------------------------------------------
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
}
}
------------------------------------------------
iii) Web Elements and Operations on Elements

a) WebElements

Browser
Page
----------------
Edit box
Link
Button
Image, Image Link, Image Button
Text Area
Check box
Radio Button
Drop down box
List box
Combo box
Web Table/ HTML Table
Frame 
Etc..
-------------------------------
b) Operations on Elements

i) Browser
(Launch Browser, Navigate to specified URL, Navigate back, Navigate forward, refresh, close Browser....)

ii) Page
(Return Page Title, Return Page URL...)

iii) Link
(Click, Check the existence, Check Enabled status ....)

iv) Button

(Click, Check Displayed status, Enabled status, return button name ....)

v) Image
a) General Image (Return Image Title etc...
b) Image Button (Click, Check Displayed status, Enabled status, return button name ....)
c) Image Link (Click, Check the existence, Check Enabled status ....)

vi) Text Area / Error Message

(Return Text Area/ Return Error Message etc...)

vii) Radio Button
(Check Displayed status, Enabled status, Select....)

viii) Check box
(Check Displayed status, Enabled status, Select, Unselect, check Selected status....)

ix) Drop down box
(Check Displayed status, Enabled status, Select an Item, Get Items Count ....)

x) List Box
(Check Displayed status, Enabled status, Select one or more Items, Get Items Count ....)

xi) Combo box 
(Check Displayed status, Enabled status, Select an Item, Enter an Item, Get Items Count ....)

xii) Web Table
(Return specified Cell value, Return Rows Count, Column Count etc...)

xiii) Frame 
(Switch from Top window to specified frame, Frame to Top window

inLine Elements
------------------------------------------------
iv) Element Locators

What is Locator?

> It is an address that identifies a Web element uniquely within the page.

Selenium supports 8 Element locators to recognize elements in web pages.

id,

name,

className,

tagName,

linkText,

partialLinkText,

cssSelector,

xpath
------------------------------------------------
Syntax:

By.elementLocator("element locator value")

Example:

driver.findElement(By.id("Email"))
----------------------------------------------
driver -Browser Object

findElement- WebDriver Method / Command

By - Built in Class

id - Locator

Email - Value

sendKeys -WebDriver Method / Command

India - Input Data
------------------------------------------------
Examples:

driver.findElement(By.name("Email")).sendKeys("India");

driver.findElement(By.className("textboxcolor")).sendKeys("India");

driver.findElement(By.tagName("input")).sendKeys("India123");

driver.findElement(By.linkText("Gmail")).click();

driver.findElement(By.partialLinkText("Gma")).click();

driver.findElement(By.cssSelector("#next")).click();

driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys("abcd123");
------------------------------------------------
v) WebDriver Commands / Methods

> Selenium WebDriver methods/commands are used to perform operations on Web Elements.

> Using Element Locators and WebDriver Methods we create Test Cases.

Element Locators - To recognize / Identify Elements

WebDriver Methods - To perform operations on Elements
------------------------------------------------
Important WebDriver API Commands

1) get() - Opens a specified URL in the Browser window.

driver.get("https://www.google.com");
------------------------------------------------
2) getTitle() - Returns the Browser Title.

String Title = driver.getTitle();
------------------------------------------------
3) getCurrentUrl() -  Returns Current URL of the Browser

String Url = driver.getCurrentUrl();
----------------------------------------------------------
4) navigate().to() - Loads a new page in the current browser window.

driver.navigate().to("https://in.yahoo.com");
------------------------------------------------
5) navigate().back() - It moves a single item back in the Browser history.

driver.navigate().back();
------------------------------------------------
6) navigate().forward() - It moves single item forward in the browser history.

driver.navigate().forward();
------------------------------------------------
7) navigate().refresh() - Refreshes the Current web page

driver.get("https://www.google.com");
------------------------------------------------
8) close() - Closes the focused Browser.

driver.close();
------------------------------------------------
9) findElement() - Finds the first Element within the current page using given locator.

driver.findElement(By.id("Email")).sendKeys("India123");
------------------------------------------------
10) sendKeys() - Enters a value into Edit box/Text box.

driver.findElement(By.id("Email")).sendKeys("India@123");
------------------------------------------------
11) clear() - It clears the value from Edit box

driver.findElement(By.id("Email")).clear();
------------------------------------------------
12) click() - Clicks an Element (Buttons, Links etc...)

driver.findElement(By.id("next")).click();
------------------------------------------------
13) isEnabled() - Checks weather the Element is in Enabled state or not?

boolean a = driver.findElement(By.id("next")).isEnabled();
------------------------------------------------
14) isDisplayed() - Checks weather the Element is displayed or not?

boolean a = driver.findElement(By.linkText("Gmail")).isDisplayed();
------------------------------------------------
15) isSelected() - Checks weather the Element is selected or not?

boolean after = driver.findElement(By.xpath("html/body/input[3]")).isSelected();
------------------------------------------------

0 comments:

Post a Comment