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

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..!!:)

10 comments:

  1. very good article vamsi........i am early waiting for your next article

    ReplyDelete
  2. Very nice post bossssssssssssss.........

    ReplyDelete
  3. Vamsi you are doing good Job... And i am very very thankful to you for this information
    And the inspiration stories...
    Keep on posting the issues in web driver and i wish you all the best ...

    ReplyDelete
  4. Hi Vamshi first of all thanq for your article

    am new to the selenium, i had tried an example with Before Start,after and close but the when am executing with using with new functions am getting am error stating that please provide main function

    could you plz help me out

    ReplyDelete
  5. Suresh ,


    It seems problem might be with some syntax errors. Can you copy paste the code you are trying ?

    ReplyDelete
  6. package MyPackage;
    MyPackage;
    import org.openqa.selenium.firefox.*; org.openqa.selenium.firefox.*;import org.openqa.selenium.By; org.openqa.selenium.By;import org.openqa.selenium.WebDriver; org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement; org.openqa.selenium.WebElement;import org.testng.annotations.BeforeTest; org.testng.annotations.BeforeTest;import org.testng.annotations.AfterTest; org.testng.annotations.AfterTest;import org.testng.annotations.Test;
      org.testng.annotations.Test;
     @SuppressWarnings("unused")("unused")public class Chrome {
    WebDriver driver;
    //BeforeTest class Chrome {
    WebDriver driver;
    //BeforeTestdriver;
    //BeforeTest//BeforeTestpublic void start(){
    driver = new FirefoxDriver();
    }
    //Testpublic void start(){
    driver = new FirefoxDriver();
    }
    //Testdriver = new FirefoxDriver();
    }
    //Test//Testpublic void ok(){
    driver.get("www.gmail.com");
    }
    //AfterTestpublic void ok(){
    driver.get("www.gmail.com");
    }
    //AfterTestdriver.get("www.gmail.com");
    }
    //AfterTest//AfterTestpublic void close(){
    driver.close();
    }
    }public void close(){
    driver.close();
    }
    }driver.close();
    }
    }

    ReplyDelete
  7. hey Vamshi could you please review the code

    ReplyDelete
  8. problem is with your code:

    Try the below one and make sure you have added all the Jars to your project path .

    package MyPackage;
    import org.openqa.selenium.firefox.*;
    import org.openqa.selenium.WebDriver;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.Test;

    public class Chrome{

    WebDriver driver;

    @BeforeTest
    public void start(){
    driver = new FirefoxDriver();
    }

    @Test
    public void ok(){
    driver.get("www.gmail.com");
    }

    @AfterTest
    public void close(){
    driver.close();
    }

    }

    ReplyDelete
  9. Hi there everyone, it's my first visit at this web page,
    and piece of writing is truly fruitful for me, keep up posting these types of posts.



    Look at my homepage - Emergency Dentist In Delray Beach FL

    ReplyDelete