My Thoughts: October 2012

Sunday, October 14

WebDriver Tutorial Part 6 : Working with the different types of web elements

From the previous post , it was clear on how to identify the webelements on webpage. In this post we will see how to work with different webelemts.

By default , selenium defines predefined functions which we can use on different types of webelemts.
Here are some of the predefinied functions:

                driver.findElement(By.id("WebelemntId")).clear();
driver.findElement(By.id("WebelemntId")).isEnabled();
driver.findElement(By.id("WebelemntId")).isDisplayed();
driver.findElement(By.id("WebelemntId")).submit();
driver.findElement(By.id("WebelemntId")).sendKeys("test");
driver.findElement(By.id("WebelemntId")).isSelected();
driver.findElement(By.id("WebelemntId")).getAttribute("");
driver.findElement(By.id("WebelemntId")).getLocation();
driver.findElement(By.id("WebelemntId")).getTagName();
driver.findElement(By.id("WebelemntId")).getText();
driver.findElement(By.id("WebelemntId")).getSize();

All the functions canot be used for every webelement.

Say  "sendKeys()" method is sending text to a webelement .This method can be used with textboxes but we can't use it on images , links. These are all basic things which we will learn through experience.

Here are the samples , on how to achieve basic functionality of different webelements using webdriver functions:

Textboxes :Send text
Sending text to Textboxes can be done by using "sendKeys()" method. Here is how it works:
driver.findElement(By.id("textBoxId")).sendKeys("sending text");
RadioButtons :Select an option
Selecting an option from Radio button can be done by using "click()" method.Here is how it works:
driver.findElement(By.id("radioButtonId")).click();
Hyperlinks :Click on links
Clicking on link can be done by using "click()" method.Here is how it works:
driver.findElement(By.id("linkId")).click();
Checkboxes :Check the checkboxes
Selecting options from Checkboxes can be done by using "click()" method.Here is how it works:
driver.findElement(By.id("checkBoxId")).click();
Drop-down List :Select an option
Selecting an option from Dropdown list  can be done by using  "sendKeys()" method.Here is how it works:
driver.findElement(By.id("dropdownListId")).sendKeys("SelectOption1");
Textarea :Send text
Sending text to Textboxes can be done by using "sendKeys()" method.Here is how it works:
driver.findElement(By.id("textAreaId")).click();
Button :click on it.
Submitting button can be done by using either "click()" or "submit()" mrthods.Here is how it works:
driver.findElement(By.id("butonId")).click();
Below example is the working code for "submitting a form which has most of the webelements like textbox, textarea, radiobutton, checkboxe and dropdown list".
package blog;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class DocumentIdentifiers {
 
 WebDriver driver;
 
 @BeforeTest
 public void start(){
 driver = new FirefoxDriver();
 }
 
 @Test
 public void ok(){
 driver.get("https://docs.google.com/spreadsheet/viewform?fromEmail=true&formkey=dGp5WEhMR0c1SzFTRFhmTjJNVk12T1E6MQ");
//Send text to firstname, lastname and email address fields
 driver.findElement(By.id("entry_0")).sendKeys("First Name");
 driver.findElement(By.id("entry_3")).sendKeys("Last Name");
 driver.findElement(By.id("entry_13")).sendKeys("Emailaddress");
//Setting value for Gender radiobutton
 driver.findElement(By.id("group_2_1")).click();
//Selecting values for "Your preferred Programming language" checkbox
 driver.findElement(By.id("group_5_1")).click();
 driver.findElement(By.id("group_5_2")).click();
 driver.findElement(By.id("group_5_3")).click();
//Setting value for "your Location" dropdown list
 driver.findElement(By.id("entry_6")).sendKeys("Non India");
//Giving the value for user rating radiobutton
 driver.findElement(By.id("group_7_3")).click();
//sending value to feedback textarea elemenet
 driver.findElement(By.id("entry_8")).sendKeys("Adding new comments ");
//Submitting the form
 driver.findElement(By.name("submit")).submit();   
 }
 
 @AfterTest
 public void close(){
 driver.quit();
 }
}

Starting from next post , I will concentrate more on live problems. See you all in next post ..!! :)

Between Advance happy dasara to you and your family. Have a great year ahead..!!:)