Selenium WebDriver


Create Tests Using Selenium WebDriver
--------------------------------------------
I) Selenium WebDriver setup

II) Element Locators

III) WebDriver Methods

IV) Create Tests using Element locators and WebDriver methods

V) Cross Browser Testing
 

--------------------------------------------
I) Selenium WebDriver setup:

 --------------------------------------------
Details:

Operating Environment: MS Windows 7

Programming Language: Java

IDE: Eclipse
-----------------------------------
> Download Java (JDK) and Install

> Set Path variable (Environment Variable) in order to access Java from all directories in C Drive.

> Download Eclipse Software

> Download WebDriver Java Language binding and add to Eclipse

> Download and Install Firebug and Firepath Add ons for Firefox Browser to inspect web elements.
(Note: If it is IE or Chrome, we no need install, developer tools are built in.)
--------------------------------------------------
If we want to create Tests / Test Scripts/ Test cases in Selenium WebDriver, two things are important:

i) Element Locators to identify/recognize web elements
(Note: Use single unique locater to recognize web elements)

ii) WebDriver Methods to perform operations on Web Elements
---------------------------------------------
iii) Programming features like Conditional statements, Loop statements, Methods, variables, operators etc... to enhance Test cases.
--------------------------------------------
II) Web Element Locators:

 --------------------------------------------
Selenium WebDriver uses 8 element locators to find elements on Web pages.

Why we need to use different locators?

i) Developers may not provide all locaters for all elements

ii) Some locaters may be duplicated.

So we have to choose any one unique locater to recognize the element.
-----------------------------------------
1) id

Syntax:

By.id("id value")

Examples:

driver.findElement(By.id("Email"))

driver- is Object

findElement - WebDriver method

By - pre-defined Class

id - Elment locater

Email - id locator value
-------------------------------------
driver.findElement(By.id("Email")).sendKeys("gcrindia");
--------------------------------------------
id locator for Edit box

WebElement Email = driver.findElement(By.id("Email"));
Email.sendKeys("gcrindia");
-------------------------------------------
id locator for Button

WebElement Email = driver.findElement(By.id("signIn"));
Email.click();

Or

driver.findElement(By.id("signIn")).click();
-------------------------------------------------------
2) name

Synatx:

By.name("name value/locator name")       

Examples:

driver.findElement(By.name("Email")).sendKeys("gcrindia");
       
Or

WebElement e = driver.findElement(By.name("Email"));
e.sendKeys("gcrindia");
------------------------------------------
WebElement e = driver.findElement(By.name("signIn"));
e.click();
------------------------------------------
3) className

Syntax: By.className("class name value")

Example:

driver.findElement(By.className("textboxcolor")).sendKeys("Hyderabad");
----------------------------------------------
4) tagName

Syntax:
By.tagName("tag name value")

Example:
driver.findElement(By.tagName("input")).sendKeys("Hyderabad");
--------------------------------------------------------------       
5) linkText

Syntax:
By.linkText("Link Text Value")

Example:

driver.findElement(By.linkText("Gmail")).click();
-----------------------------------------------------               
6) paritialLinkText

Syntax:
By.partialLinkText("Partial Link Text Value")

Example:

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

Syntax:
By.cssSelector("value")

Example:
driver.findElement(By.cssSelector(".gb_m")).click();
---------------------------------------------------
8) xpath

Syntax:

By.xpath("xpath value")

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

--------------------------------------------
1) get Method()

It opens specified URL in the Browser window

Syntax:
driverObject.get("URL")

Example:
driver.get("http://www.google.com");
--------------------------------------------
2) getTitle Method()

It returns title of the Browser

Syntax:
String stringName = driverObject.getTitle();

Example:
String Title = driver.getTitle();
System.out.println(Title);
--------------------------------------------
3) getPageSource()

It returns the page source

Syntax:
String stringName = driverObject.getPageSource();

Example:
String Pagesource = driver.getPageSource();
System.out.println(Pagesource);
--------------------------------------------
4) getCurrentUrl()

It returns current url of the Browser

Syntax:
String stringName = driverObject.getCurrentUrl();

Example:
String CurrentUrl = driver.getCurrentUrl();
System.out.println(CurrentUrl);
--------------------------------------------
Browser Navigation Methods:

5) navigate().to() Method

Loads a new web page in the current browser window.

Synatx:

driverObject.navigate().to();

example:
driver.navigate().to("http://www.yahoo.com");
------------------------------------------
6) navigate().back();

It moves a single item back in the web browser's history

Syntax:

driverObject.navigate().back();
---------------------
Example:

driver.get("http://www.google.com");
driver.navigate().to("http://www.yahoo.com");
driver.navigate().back();
--------------------------------------------
7) navigate().forward()

It moves a single item forward in the Browser history

Syntax:
driverObject.navigate().forward();

Example:
driver.get("http://www.google.com");
driver.navigate().to("http://www.yahoo.com");
driver.navigate().back();
driver.navigate().forward();//yahoo
--------------------------------------------
8) naviagte().refresh()

It refreshes the current web page

Syntax:
driverObject.navigate().refresh();

Example:
driver.get("http://www.google.com");
driver.navigate().refresh();
--------------------------------------------
9) close()

It closes the foused browser

Synatx:

driverObject.close();

Example:

driver.get("file:///E:/HTMLExamples/LoginPage.html");
driver.findElement(By.linkText("Sign In")).click();
driver.close();
--------------------------------------------
10) quit()

It closes all Browsers that opened by WebDriver during execution.

Synatx:

driverObject.quit();

example:
driver.get("file:///E:/HTMLExamples/LoginPage.html");
driver.findElement(By.linkText("Sign In")).click();
driver.quit();
--------------------------------------------
11) findElement()

It finds first element within the current page using the given locator and returns single element.

Syntax:

WebElemnt objectname = driver.findElement(By.locator("locator value"));
objectname.method();

Or
driver.findElement(By.locator("locator value")).method();

Example:
WebElement e = driver.findElement(By.id("Email"));
e.sendKeys("abcd");

Or
driver.findElement(By.id("Email")).sendKeys("abcd");
--------------------------------------------
12) sendKeys()
Enters a Value in to Text box / Edit box

Syntax:

driverObject.findElement(By.lcator("locator value")).sendkeys("value");

Or

WebElement ObjectName = driverObject.findElement(By.lcator("locator value"))
ObjectName..sendkeys("value");

Example:
driver.findElement(By.id("Email")).sendKeys("abcd");

Or

WebElement e = driver.findElement(By.id("Email"));
e.sendKeys("abcd");
--------------------------------------------
13) clear()
It clears the value from a text box or edit box

Syntax:
driverObject.findElement(By.lcator("locator value")).clear();

Example:
driver.findElement(By.id("Email")).clear();
--------------------------------------------
14) click()
It clicks an element

Synatx:
driverObject.findElement(By.locator("locator value")).click();

Or
WebElement ObjectName = driverObject.findElement(By.locator("locator value"));
ObjectName.click;

Examples:
click method on Button Object

driver.get("http:/gmail.com");
driver.findElement(By.id("signIn")).click();
---------------------------
click method on Link Object

driver.get("http:/google.com");
driver.findElement(By.linkText("Gmail")).click();
--------------------------------------------
15) isEnabled()
It checks weather the element is in enabled state or not?

Syntax:

boolean variableName = driverObject.findElement(By.locator("locator value")).isEnabled();

Example:
driver.get("http:/google.com");
boolean i = driver.findElement(By.linkText("Gmail")).isEnabled();
System.out.println(i);
--------------------------------------------
16) maximize()
Syntax:
driver().manage().window().maximize();

Example:
driver.get("http:/google.com");
driver.manage().window().maximize();
--------------------------------------------

1 comments:

gr8 effort for intial .......................

Post a Comment