"Karmanyevadhikaraste ma phaleshu kadachana, Ma karma phala hetur bhurmatey sangostva akarmani."

Selenium coding basics-1

Test script use web driver client libraries (fire fox driver, IE driver, chrome driver)  supported in java, ruby etc and connect to various browsers and send request to web server and get response.

Steps before you start:


First Install java jdk in your system
Install Eclipse   eclipse.org
Install Firefox,install fire bug, fire path
download web driver client library  http://docs.seleniumhq.org/download/
Firefox driver will come with this package, download ie and chrome driver


Eclipse IDE:
Open a new java project
Under Libraries tab, click on add external jars and select selenium-java-2.33.jar and selenium-java-2.33.srcs.jar files from downloaded location of selenium web driver
Add all jars available under libs folder of  selenium web driver directory
create a new class

web driver is an interface which has 2 main classes
1.Remote web driver
2.html unit  driver

Firefox driver, ie driver,chrome driver,android driver ,iphone driver all implement from remote web driver


Getting  google page:

package com.cc.**;

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

public class NavigateToAUrl {
   public static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}


Locating web elements :  same code from above +

  WebElement searchBox = driver.findElement(By.name("q"));
     searchBox.sendKeys("Packt Publishing");
     searchBox.submit();


findElement()  - Returns the first web element the driver come up with satisfying the searched mechanism
- throw runtime exception, if no element found or if there are more than 1 element satisfy this criteria

findElements() - If developer thinks he my have encounter '0' or more number of elements
if no element- empty list is returned
if multiple elements- all of them returned


There are 8 d/f ways to locate a web element,(Tag Name,Select,Input,List)

Name ID, Tag Name, Class, Link Text,PartialLink Text,XPath,CSS

Google search button tags:


By.name()- 
WebElement searchBox= driver.findElement(By.name("btnK"));

By .id()- 

WebElement searchBox= driver.findElement(By.id("gbqfsa"));

By.tagName()-

No.of buttons present on a google page:

List<WebElement> buttons= driver.fineElements(By.tagName("buttons"));
System.out.println(buttons.sixe());

By xpath:
WebElement box= driver.findElement(By.xPath("//*[@id='gbqfba']"));

By Css:
WebElement boxx= driver.findElement(By.cssSelector("//*[@id='gbqfba']"));

By className:
WebElement boxx= driver.findElement(By.className("gbqfif"));

By linkText:
Suppose theres is a link named : About Google' <a href="/env/about.html">About Google</a>

WebElement boxx= driver.findElement(By.linkText("About Google"));

By.partialLinkText-
WebElement boxx= driver.findElement(By.partialLinkText("About "));