Selenium Quick Guide

Selenium WebDriver Quick Guide
 

1) Introduction to Selenium WebDriver

2) Selenium WebDriver Environment Setup

3) Write first Selenium Test Case

4) Web Elements and Operations.

5) Element Locators

6) Selenium WebDriver Commands and Operations

7) Handling Elements in Selenium

8) Cross Browser Testing

9) Writing Selenium Test Cases.

10) Writing Selenium Test Cases using User defined Methods
Selenium Tutorial
--------------------------------------------------------------------
1) Introduction to Selenium WebDriver

• Selenium WebDriver was launched at Google In 2006.

• 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 3 (Latest Version released in October 2016)
----------------------------------------
Selenium WebDriver Features

• Selenium WebDriver supports various programming languages(Java, C#, Python, Ruby, Perl and PHP) to write programs (Test Scripts)

• Selenium WebDriver supports various Browsers (Mozilla Firefox, Google Chrome, IE, Safari, Opera etc...) to create and execute Test cases.

• Selenium WebDriver supports various operating environments(MS Windows, Linux, Macintosh etc...) to automate Web Applications.

• Selenium WebDriver supports Data Driven Testing and Cross Browser Testing

• Selenium WebDriver supports Batch Testing with the help of either JUnit or TestNG framework.


• Using Element Locators and WebDriver Methods /Commands we can create Test Cases.
----------------------------------------
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.
---------------------------------------------------

2) 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.
------------------------------------------------ 
Download Selenium WebDriver Java language binding and extract.

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

Navigation to add Selenium Webdriver jar files to Java Project:

> Launch Eclipse IDE
> 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

> Create driver object by importing 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.
---------------------------------------- 

3) 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
}
}
------------------------------------------------

4) Web Elements and Operations

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 and Frame.
------------------------------
i) Operations on Browser

Launch Browser, Navigate to specified URL, Close focused Browser and Close all opened Browsers.

Navigate from one URL to another, Navigate back, Navigate forward, Refresh the Browser and Maximize the Browser etc...
------------------------------
ii) Operations on Web Page

Get Page Title, Get Page source and and Get Page URL etc...
------------------------------
iii) Operations on Edit box

Enter some value, Clear the value, Check the Edit box existence, Check enabled status and Get the Value etc...
------------------------------
iv) Operations on Link

Click, Check the Link existence, Check enabled status and Return Link name etc...
------------------------------
v) Operations on Image

a) General Image

b) Image Button (Submits)

c) Image Link (Click, Redirects to another page/location)
------------------------------
vi) Operations on Check box

Select, Unselect, Check if the check box is displayed or not? and Check if the check box is enabled or not? etc...
------------------------------
vii) Operations on Button

Click, Check the Button enabled statues and Check Displayed status etc...
------------------------------
viii) Operations on Text Area

Return or Capture Text Area
------------------------------
ix) Operations on Radio Button

Select Radio Button, Check the Radio Button existence, Check the enabled status and Verify if the Radio button is Selected or not?
------------------------------
x) Operations on Drop down box

Select an Item, Check the Drop down box existence, Check the enabled status and Return Items Count.
------------------------------
xi) Operations List Box

Select one more items, Return Items count, Check the List box existence and Check the enabled status.
-------------------------------
xii) Operations on Combo box

Select an item, Enter an item, Check the Combo box existence and Check the enabled status.
--------------------------------------
xiii) Operations on Web Table /HTML Table

Get Cell value, Rows count and Cells count etc...
--------------------------------
xiv) Operations on Frame

Switch from Top window to a frame and Switch from a frame to Top window.
-------------------------------------------------- 

5) Element Locators
 

> Element Locator is an address that identifies a Web Element uniquely within the web page.

> Selenium supports 8 Element locators to recognize Elements, they are id, name, className, tagName, linkText, partialLinkText, cssSelecotor and xpath to find elements on Web Pages.

> We can use anyone unique Locator to recgnize an Element.

i) id

Syntax:

By.id("id value")

Example:

driver.findElement(By.id(("Email"))
------------------------------
driver - is Object, findElement is Webdriver Method/command, By is Class, id is Locator and Email is value.
--------------------------------
ii) name

Syntax:

By.name("value")

Example

driver.findElement(By.name("Email"))
--------------------------
iii) className

Syntax:

By.className("value")

Example:

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

Syntax:

By.tagName("value")

Example:

driver.findElement(By.tagName("input")).sendKeys("India");
------------------------------------
v) linkText

Syntax:

By.linkText("value")

driver.findElement(By.linkText("Gmail")).click();
----------------------------------
vi) partialLinkText

Syntax:

By.partialLinkText("value")

driver.findElement(By.partialLinkText("Gma")).click();
---------------------------
vii) cssSelector

Syntax:

By.cssSelector("value")

Example:

driver.findElement(By.cssSelector(".gb_P")).click();
---------------------------------------
viii) xpath

Syntax:

By.xpath("value")

Example:

driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a")).click();
------------------------------------------------------

6) Selenium WebDriver Commands and Operations

> Selenium WebDriver Methods/Commands are used to perform operations on Web Elements.

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

Element Locators - for recognizing Elements

WebDriver Methods - for performing operations on Elements.
-----------------------------------
i) get()

Description: Opens a specified URL in the Browser window.

Syntax:

driverObject.get("URL");

Example:

driver.get("https://www.google.co.in");
------------------------------------
ii) getTitle()

Returns Title of the Browser.

Syntax:

String variable = driver.getTitle();

Example:

driver.get("https://www.google.co.in");
String Title = driver.getTitle();
System.out.println(Title);
--------------------------------------------
iii) getPageSource()

Returns HTML page source.

Syntax:

String stringName = driver.getPageSource();

Example:

driver.get("https://www.google.co.in");
String pageSource = driver.getPageSource();
System.out.println(pageSource);
----------------------------------
iv) getCurrentUrl()

Returns Current URL of the Browser.

Syntax:

String stringName = driver.getCurrentUrl();

Example:
driver.get("https://www.google.co.in");
String URL = driver.getCurrentUrl();
System.out.println(URL);
-------------------------------
Browser Navigation Methods

v) navigate().to()

Loads a new web page in the current browser window.

Syntax:
driverObject.navigate().to("URL");

Example:

driver.get("https://www.google.co.in");
String URL = driver.getCurrentUrl();
System.out.println(URL);
driver.navigate().to("https://login.yahoo.com/");
URL = driver.getCurrentUrl();
System.out.println(URL);
-------------------------------------
vi) navigate().back()

It moves a single item back in the Browser history.

Syntax:

driver.navigate().back();

Example:

driver.get("https://www.google.co.in");
String URL = driver.getCurrentUrl();
System.out.println(URL);
driver.navigate().to("https://login.yahoo.com/");
URL = driver.getCurrentUrl();
System.out.println(URL);
driver.navigate().back();
URL = driver.getCurrentUrl();
System.out.println(URL);
-----------------------------------
vii) navigate().forward()

It moves single item forward in the Browser history.

Syntax:

driver.navigate().forward();

Example:

driver.get("https://www.google.co.in");
String URL = driver.getCurrentUrl();
System.out.println(URL);

driver.navigate().to("https://login.yahoo.com/");
URL = driver.getCurrentUrl();
System.out.println(URL);

driver.navigate().back();
URL = driver.getCurrentUrl();
System.out.println(URL);

driver.navigate().forward();
URL = driver.getCurrentUrl();
System.out.println(URL);
-----------------------------------
viii) navigate().refresh()

Refresh the current web page

Syntax:

driver.navigate().refresh()

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in");
String URL = driver.getCurrentUrl();
System.out.println(URL);
driver.navigate().refresh();
URL = driver.getCurrentUrl();
System.out.println(URL);
------------------------------------
ix) close()

It closes the focused Browser.

Syntax:

driverObject.close();

Example:

driver.get("https://www.google.co.in");
driver.close();
-----------------------------------
x) quit()

It closes all browser that opened by WebDriver during execution.

Syntax:

driverObject.quit();

Example:

driver.get("file:///C:/Users/gcreddy/Desktop/HTMLExamples/LoginPage.html");
driver.findElement(By.linkText("Sign In")).click();
driver.quit();
-----------------------------------
xi) findElement()

It finds the first element within the current page using the give locator.

driver.findElement(By.ElementLocator("Value"))

Syntax:
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/gcreddy/Desktop/HTMLExamples/LoginPage.html");
driver.findElement(By.tagName("input")).sendKeys("abcd");

Or

WebElement Email = driver.findElement(By.id("Email"));
Email.sendKeys("India");
-----------------------------------
xii) sendkeys()

Enters a value into Edit box/Text box

Syntax:
driver.findElement(By.ElementLocator("value").sendkeys("input data");

Example:

driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("India");
}
-----------------------------------
xiii) clear()

It clears the value

Syntax:
driver.findElement(By.ElementLocator("value").clear();

Example:
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("India");
Thread.sleep(5000);
driver.findElement(By.id("Email")).clear();
-----------------------------------
xiv) click()

Clicks an Element (Buttons, Links)

Syntax:

driver.findElement(By.ElementLocator("value").click;

Example:

driver.get("https://www.gmail.com");
driver.findElement(By.id("next")).click();
-----------------------------------
xv) isEnabled()

It checks weather the Element is in enabled state or not?

Syntax:

boolean variableName = driver.findElement(By.ElementLocator("value").isEnabled();

Example:

driver.get("https://www.gmail.com");
boolean a = driver.findElement(By.id("next")).isEnabled();
System.out.println(a);
-----------------------------------
xvi) isDisplayed()

Checks if the Element is displayed or not? in the current web page.

Syntax:

boolean variableName = driver.findElement(By.ElementLocator("value").isDisplayed();

driver.get("https://www.gmail.com");
boolean a = driver.findElement(By.id("next")).isDisplayed();
System.out.println(a);
-----------------------------------
xvii) isSelected()

checks if the Element is Selected or not? in the current web page.

Syntax:

boolean variableName = driver.findElement(By.ElementLocator("value").isSelected();

example:

driver.get("file:///C:/Users/gcreddy/Desktop/HTMLExamples/MultipleCheckbox.html");
boolean a = driver.findElement(By.xpath("html/body/input[2]")).isSelected();
System.out.println(a);//false
driver.findElement(By.xpath("html/body/input[2]")).click();
a = driver.findElement(By.xpath("html/body/input[2]")).isSelected();
System.out.println(a);//true
-----------------------------------
xviii) manage().window().maximize()

Syntax:

driverObject.manage().window().maximize()

Example:

driver.get("file:///C:/Users/gcreddy/Desktop/HTMLExamples/MultipleCheckbox.html");
Thread.sleep(5000);
driver.manage().window().maximize();
------------------------------------------- 

8) Cross Browser Testing

> Cross Browser Testing is a type of Test to check that our Web Application works as expected in different Browsers.

> A web application can be opened in any web browser by the end user, So we need to ensure that the web application will work as expected in all popular browsers.


Note: Selenium WebDriver Test Case is common for all Browsers, browser driver only varies from one Browser to another

 
a) Create Browser Drivers

(For Google Chrome, IE and Other Browsers, download Browser drivers and set path in Selenium Test Scripts)

i) Mozilla Firefox Browser:

WebDriver driverName = new FirefoxDriver();

ii) Google Chrome

//Instantiate Chrome Browser driver

System.setproperty("webdriver.chrome.driver", "driver .exe file path");
WebDriver driverName = new ChromeDriver();

iii) IE Browser driver

System.setproperty("webdriver.ie.driver", "driver .exe file path");
WebDriver driverName = new InternetExplorerDriver();
------------------------------------------------------
b) Create a Test Case and Execute using Mozilla Firefox, Chrome and IE Browsers.

Test Case: Verify Launch Application (Google) functionality in Firefox, Chrome and IE Browsers.

Steps:
i) Launch the Browser
ii) Navigate to https://www.google.com url

Verification point:
Capture the page Title (Actual) and Compare with Expected.

Expected Page Title:
--------------------------------
i) Test Case for Mozilla Firefox Browser

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");

String PageTitle = driver.getTitle();

if (PageTitle.equals("Google")){
System.out.println("Google Application Launched - Passed");
}
else {
System.out.println("Google Application Not Launched -Failed");   
}
driver.close();
------------------------------
ii) Test Case for Google Chrome Browser

System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");

String PageTitle = driver.getTitle();

if (PageTitle.equals("Google")){
System.out.println("Google Application Launched - Passed");
}
else {
System.out.println("Google Application Not Launched -Failed");   
}
driver.close();
------------------------------
iii) Test Case for internet Explorer Browser

System.setProperty("webdriver.ie.driver", "E:\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("https://www.google.com");

String PageTitle = driver.getTitle();

if (PageTitle.equals("Google")){
System.out.println("Google Application Launched - Passed");
}
else {
System.out.println("Google Application Not Launched -Failed");   
}
driver.close();
--------------------------------------

10 comments:

Thank you, Sir for your immense and continuing support. We are really grateful to you.

are this is all command i need it to create any test case by selenium ?

It is always a huge support to us from your side.
Thanks.

Thnku very much sir 4 ur supprt ..

Excellent posting Chandra Kanth garu
I have long break with java now trying to refresh wonderful notes.

Post a Comment