Wednesday, 3 July 2013

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:
  1. 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:
  1. 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:
  1. 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:
  1. 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:
  1. 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:
  1. 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:
  1. 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".
  1. package blog;  
  2.   
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.firefox.FirefoxDriver;  
  6. import org.testng.annotations.AfterTest;  
  7. import org.testng.annotations.BeforeTest;  
  8. import org.testng.annotations.Test;  
  9.   
  10. public class DocumentIdentifiers {  
  11.    
  12.  WebDriver driver;  
  13.    
  14.  @BeforeTest  
  15.  public void start(){  
  16.  driver = new FirefoxDriver();  
  17.  }  
  18.    
  19.  @Test  
  20.  public void ok(){  
  21.  driver.get("https://docs.google.com/spreadsheet/viewform?fromEmail=true&formkey=dGp5WEhMR0c1SzFTRFhmTjJNVk12T1E6MQ");  
  22. //Send text to firstname, lastname and email address fields  
  23.  driver.findElement(By.id("entry_0")).sendKeys("First Name");  
  24.  driver.findElement(By.id("entry_3")).sendKeys("Last Name");  
  25.  driver.findElement(By.id("entry_13")).sendKeys("Emailaddress");  
  26. //Setting value for Gender radiobutton  
  27.  driver.findElement(By.id("group_2_1")).click();  
  28. //Selecting values for "Your preferred Programming language" checkbox  
  29.  driver.findElement(By.id("group_5_1")).click();  
  30.  driver.findElement(By.id("group_5_2")).click();  
  31.  driver.findElement(By.id("group_5_3")).click();  
  32. //Setting value for "your Location" dropdown list  
  33.  driver.findElement(By.id("entry_6")).sendKeys("Non India");  
  34. //Giving the value for user rating radiobutton  
  35.  driver.findElement(By.id("group_7_3")).click();  
  36. //sending value to feedback textarea elemenet  
  37.  driver.findElement(By.id("entry_8")).sendKeys("Adding new comments ");  
  38. //Submitting the form  
  39.  driver.findElement(By.name("submit")).submit();     
  40.  }  
  41.    
  42.  @AfterTest  
  43.  public void close(){  
  44.  driver.quit();  
  45.  }  
  46. }  
Starting from next post , I will concentrate more on live problems. See you all in next post ..!! :)

No comments:

Post a Comment