Selenium Webdriver


Selenium WebDriver is a tool for automating web application testing, and in particular to verify that they work as expected. It enables us to use a programming language in creating our test scripts.

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.

We may or may not, need the Selenium Server, depending on how we intend to use Selenium-WebDriver. For ex. If our browser and tests both run on the same machine, and our tests only use the WebDriver API, then we do not need to run the Selenium-Server.

In this case WebDriver will run the browser directly.

Advantages of Webdriver:
  • It supports multiple browsers like FF, Chrome, Safari, Opera, IE, etc.
  • No need to start selenium server which is a required condition while testing using selenium RC.
  • Supports Implementation of listeners which is not supported by RC and IDE.
  • The execution of test is fastest as compared to RC and IDE.
  • Supports multiple platforms like Windows, Linux,etc.
  • WebDriver's API is more concise than Selenium RC's.
  • WebDriver can support HtmlUnit while Selenium RC cannot.

Limitations of WebDriver

•           WebDriver Cannot Readily Support NewBrowsers:
Since WebDriver operates on the OS level and different browsers communicate with the OS in different ways. So, If a new browser comes , it may have a different way of interaction/communication  with the OS as compared to other browsers. Hence, it takes time to figure out the new process by the Webdriver dev team.
•           Selenium does not have a built-in command for automatic generation of test results.
•           Programming knowledge(Java,Ruby,Perl,etc) knowledge is required for the person developing test using selenium webdriver.
•           Selenium do not support the features of record and playback as in case of selenium RC.


Selenium Locators: 

Locate element by Id:
ID is the most preferred and the easiest way to locate an element on a web page and considered  as the safest and fastest locator option and should always be the first choice.
HTML: <input id="xyz" type="text">
WebDriver command: 
driver.findElement(By.id("xyz"));


Locate element by Name:
If the name is unique in the page, it is most preferred after ID.

HTML: <input id="uid" name="userid" type="text">

WebDriver command: 
driver.findElement(By.name("userid"));

Locate element by link text:
This locator is used in case we want to locate any hyperlink. But before that we need to be sure that there is only one unique link on the web page. If there are multiple links with the same link text, then in such cases Selenium will perform action on the first matching element with link text on page.

HTML:<a style="background-color: transparent;" href="#">Forgot Email</a>

WebDriver command:
 driver.findElement(By.linkText("Forgot Email"));


Locate element by Partial LinkText
Partial LinkText works same as LinkText, the only difference is you can use a part of the text of the link.

HTML: <a href="#">Forgot Password? </a>

WebDriver command:
driver.findElement(By. PartialLinkText ("Password"));


Selenium Locators: Locate element by Tag Name
Tag Name can be used for the elements like drop downs, check boxes, radio buttons, etc. 

HTML:

<select name="selFood" id="selCity">
<option value="none">--Select--</option>
<option value="Ind">Indian</option>
<option value="Chin">Chinese</option>
</select>

WebDriver command:

Select select = new Select(driver.findElement(By.tagName("select")));
select.selectByVisibleText("Indian");
or
select.selectByValue("Ind");


Locate element by Class Name:

HTML: <input id="uid" class="usercls" tabindex="1" type="text">

WebDriver command: 
driver.findElement(By.className(“usercls”));



Locate element by Xpath:
XPath is the standard navigation tool for XML. XPath is used everywhere where there is XML.

HTML:
<div id="triptype">
      <button type="button" name="trip" value="Oway">Oneway</button>
      <button type="button" name="trip" value="RTrip">RoundTrip</button>
 </div>

WebDriver Command:
xpath=//button[@value="Oway"]//div[@id="triptype"]/button[0]


Locate element by CSS:

The CSS locator strategy uses CSS selectors to find the elements in the page
For above HTML: css=div[id="triptype"] > button[value="Oway"] 


Browsers commands:
Get Command:
get(String arg0):Void– This method Load a new web page in the current browser window. It accepts String as a parameter and returns nothing.

Command – driver.get(appUrl);
Where appUrl is the website address to load. We can also write:
driver.get("http://www.google.com");

Get Title Command
getTitle() : String – This method fetches the Title of the current page. It accepts nothing as a parameter and returns a String value.

Command – driver.getTitle();
As the return type is String value, the output must be stored in String object/variable.
String Title = driver.getTitle();


Get Current URL Command
getCurrentUrl() : String – This method fetches the string representing the Current URL which is opened in the browser. It accepts nothing as a parameter and returns a String value.

Command – driver.getCurrentTitle();
As the return type is String value, the output must be stored in String object/variable.
String CurrentUrl = driver.getCurrentUrl();


Get Page Source Command
getPageSource() : String – This method returns the Source Code of the page. It accepts nothing as a parameter and returns a String value.

Command – driver.getPageSource();
As the return type is String value, the output must be stored in String object/variable.
String PageSource = driver.getPageSource();


Close Command
close() : void – This method Close only the current window the WebDriver is currently controlling. It accepts nothing as a parameter and returns nothing.

Command – driver.close();
Quit the browser if it’s the last window currently open.
driver.close();


Quit Command
quit() : void – This method Closes all windows opened by the WebDriver. It accepts nothing as a parameter and returns nothing.

Command – driver.quit();
driver.quit();


Navigation commands:
The navigate interface exposes the ability to move backwards and forwards in the browser’s history. To access the navigation’s method, just type driver.navigate().The intelligence feature of eclipse will automatically display all the public methods of Navigate Interface J

Navigate To Command
to(String arg0) : void – This method Loads a new web page in the current browser window. It accepts a String parameter and returns nothing.

Command – driver.navigate().to(appUrl);
It does exactly the same thing as the driver.get(appUrl) method

Forward Command:
forward() : void – This method does the same operation as clicking on the Forward Button of any browser. It neither accepts nor returns anything.

Command – driver.navigate().forward();
Takes you forward by one page on the browser’s history.
driver.navigate().forward();

Back Command
back() : void – This method does the same operation as clicking on the Back Button of any browser. It neither accepts nor returns anything.

Command – driver.navigate().back();
Takes you back by one page on the browser’s history.
driver.navigate().back();

Refresh Command
refresh() : void – This method Refresh the current page. It neither accepts nor returns anything.

Command – driver.navigate().refresh();
Perform the same function as pressing F5 in the browser
driver.navigate().refresh();



Webelements commands:

What is WebElement?
WebElement represents an HTML element. These are written with a start tag, with an end tag and with the content in between:<tagname> content </tagname>
List of WebElement Commands/Actions

Clear Command
clear( ) : void – If this element is a text entry element, this will clear the value. This method accepts nothing as a parameter and returns nothing.

Command – element.clear();
This method has no effect on other elements
WebElement element = driver.findElement(By.id("UserName"));
element.clear();

SendKeys Command
sendKeys(“text or value to be sent) : void – This simulate typing into an element, which may set its value. This method accepts CharSequence as a parameter and returns nothing.

Command – element.sendKeys(“text”);
This method works fine with text entry elements like INPUT and TEXTAREA elements.
WebElement element = driver.findElement(By.id("UserName"));
element.sendKeys("ToolsQA");

 
Click Command
click( ) : void – This simulates the clicking of any element. It accepts nothing as a parameter and returns nothing.

Command – element.click();
WebElement element = driver.findElement(By.linkText("signin"));
element.click();

 
IsDisplayed Command
isDisplayed( ) : boolean – This method determines if an element is currently being displayed or not. It accepts nothing as a parameter but returns boolean value(true/false).

Command – element.isDisplayed();
WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isDisplayed();

IsEnabled Command
isEnabled( ) : boolean – This determines if the element currently is Enabled or not? It accepts nothing as a parameter but returns boolean value(true/false).
Command – element.isEnabled()'
WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isEnabled();

IsSelected Command
isSelected( ) : boolean – Determine whether or not this element is selected or not. It accepts nothing as a parameter but returns boolean value(true/false).

Command – element.isSelected();
This operation only applies to input elements such as Checkboxes, Select Options and Radio Buttons. This returns True if the element is currently selected or checked, false otherwise.
WebElement element = driver.findElement(By.id("oneway"));
boolean status = element.isSelected();

Submit Command
submit( ) : void– This method works well/better than the click() if the current element is a form, or an element within a form. It accepts nothing as a parameter and returns nothing.

Command – element.submit();
If this causes the current page to change, then this method will wait until the new page is loaded.
WebElement element = driver.findElement(By.id("SubmitButton"));
element.submit();

GetText Command
getText( ) : String– This method will fetch the visible (i.e. not hidden by CSS) innerText of the element. It accepts nothing as a parameter but returns a String value.

Command – element.submit();
WebElement element = driver.findElement(By.xpath("anyLink"));
String linkText = element.getText();

getTagName Command
getTagName( ) : String– This method gets the tag name of this element. It accepts nothing as a parameter and returns a String value.

Command – element.getTagName();
WebElement element = driver.findElement(By.id("SubmitButton"));
String tagName = element.getTagName();

getCssValue Command
getCssvalue( ) : String– This method Fetch CSS property value of the give element. It accepts nothing as a parameter and returns a String value.

Command – element.getCssValue();


getAttribute Command
getAttribute(String Name) : String– This method gets the value of the given attribute of the element. It accepts the String as a parameter and returns a String value.

Command – element.getAttribute();
Attributes are Ids, Name, Class extra and using this method you can get the value of the attributes of any given element.
WebElement element = driver.findElement(By.id("SubmitButton"));
String attValue = element.getAttribute("id"); //This will return "SubmitButton" 

getSize Command
getSize( ) : Dimension – This method fetch the width and height of the rendered element. It accepts nothing as a parameter but returns the Dimension object.

Command – element.getSize();
This returns the size of the element on the page. 
WebElement element = driver.findElement(By.id("SubmitButton"));
Dimension dimensions = element.getSize();

getLocation Command:
getLocation( ) : Point – This method locate the location of the element on the page. It accepts nothing as a parameter but returns the Point object.

Command – element.getLocation();
This returns the Point object, from which we can get X and Y coordinates of specific element.
WebElement element = driver.findElement(By.id("SubmitButton"));
Point point = element.getLocation();

System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y);