×

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods:

The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc.

Data-Capture Controls Methods

Some of the most commonly used data-capture controls are as follow:

DataCapture controls Description Return Type Command Syntax
getText() It is used to capture the visible text from the identified element. It always returns a String. getText() : String   element.getText();  
getAttribute() It is used to capture any back-end attribute value from the web element. It returns the value of an attribute in the form of String. getAttribute(String Name) : String   element.getAttribute();  
getCssValue() It is used to capture any style related attributed from the element like, color, back ground color, font style, etc.   It return RGBA color code in the form of String. getCssvalue() : String  element.getCssValue();    
getSize() It is used to capture height and width of the web element. It always returns the dimension of aclass object. getSize() : Dimension   element.getSize();  
getLocation() It is used to capture X and Y co-ordinates of the web elements. It returns the point of aclass object. getLocation() : Point  element.getLocation();  

Let us take a sample test script in which will try to cover mostly used data_capture_controls Commands:

For our testing purpose, we are using the login page of yahoo to perform primarily used data capture commands of web element controls.

In this test, we will automate the following test scenarios:

Steps Action Method Used Input Excepted Result
1 Open the Google Chrome Browser System.setProperty()   The google chrome browser must be opened.
2 Navigate to yahoo login page. get() www.yahoo.com The yahoo login page must be displayed.
3 Verify the visible text of sigh-in checkbox. getText()   The visible text should be displayed.
4 Verify the next button. getAttribute()   The name of the button should be next.
5 Check the color of the next button. getCssValue()   The color of the button should be blue.
6 Verify yahoo logo image. getSize()     Verify the logo image of the yahoo login page and get the size of the logo image.  
7 Verify the yahoo logo location. getLocation()   Get the location of the logo.
8 Close the browser close()   The Browser should be closed.

Open the Eclipse IDE and open the existing test suite new_test, which we have created in  WebDriver Installation section of Selenium WebDriver tutorial.

Then, right-click on the new_test folder and create a new Class File from New > Class.

new_test folder and create a new Class File

And give the Class name as data_capture_controls and click on the Finish button.

Class name as data_capture_controls

We are creating our test case step by step to give you a complete understanding of data capture Commands in WebDriver.

Step1:

To launch the Google Chrome browser, we need to download the ChromeDriver.exe file and set the system property to the path of ChromeDriver.exe file.

Hence, here is the code for setting the system property for google chrome:

//set the system property of Google Chrome
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");   

To initialize the Chrome driver using Chrome Driver class:

// create driver object for CHROME browser
WebDriver driver=new ChromeDriver(); 

Step2:

Launch the website using the get() method.

//navigate to yahoo login page
driver.get("https://login.yahoo.com"); 

Step3:

Verify the visible text of the sigh-in checkbox using getText() data capture method.

//identify the visible text sigh-in
WebElement wb= driver.findElement(By.id("mbr-login-greeting"));
String acttext=wb.getText();
System.out.println(acttext); 

Step4:

Verify the next button using the getAttribute() data capture method.

//identify next button
         WebElement wb1 = driver.findElement(By.id("login-signin"));
//name of the button should be next.
        String actval=wb1.getAttribute("value");
         System.out.println(actval);
         if (actval.contentEquals("Next"))
         {
       System.out.println("button name is verify ==pass");
         }
         else {
         System.out.println("button name is not verifyed == fail");
} 

Step5:

Check the color of the Next button using the getCssValue() data capture method.

// color of the button should be blue
        String actcol=wb1.getCssValue("background-color");
       System.out.println(actcol); 

Step6:

Verify the yahoo logo image size using the getSize() data capture method.

WebElement iwb= driver.findElement(By.xpath("(//img[@class='logo'])[2]"));
//get the size of logo-image
 Dimension dem=iwb.getSize();
  System.out.println("get the height =>"+ dem.getHeight());
 System.out.println("get the width =>"+ dem.getWidth()); 

Step7:

Verify the yahoo logo location using the getLocation() data capture method.

//get the location of the logo image
           org.openqa.selenium.Point pt =iwb.getLocation();
           System.out.println("get the x-axis co oridinate =>" +pt.getX());
           System.out.println("get the y- axis co oridinates =>"+ pt.getY()); 

Step8:                                                                                 

Finally, we are terminating the process and close the browser.

 //Closing browser 
driver.close();  

Our final test script will look like this:

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
   public class data_capture_controls {
         public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
// create driver object for CHROME browser
          WebDriver driver=new ChromeDriver();
 //navigate to yahoo login page
         driver.get("https://login.yahoo.com");
  //identify the visible text of sign-in
         WebElement wb= driver.findElement(By.id("mbr-login-greeting"));
         String acttext=wb.getText();
         System.out.println(acttext);  
  //identify next button
         WebElement wb1 = driver.findElement(By.id("login-signin"));
 // name of the button should be next.
         String actval=wb1.getAttribute("value");
         System.out.println(actval);
         if (actval.contentEquals("Next"))
         {
         System.out.println("button name is verify ==pass");
    }
         else {
         System.out.println("button name is not verifyed == fail");
         }
    // color of the button should be blue.
         String actcol=wb1.getCssValue("background-color");
        System.out.println(actcol);
         WebElement iwb= driver.findElement(By.xpath("(//img[@class='logo'])[2]"));
 // get the size of logo-image
          Dimension dem=iwb.getSize();
          System.out.println("get the height =>"+ dem.getHeight());
          System.out.println("get the width =>"+ dem.getWidth());
// get the location of the logo image
         org.openqa.selenium.Point pt =iwb.getLocation();
        System.out.println("get the x-axis co oridinate =>" +pt.getX());
        System.out.println("get the y- axis co oridinates =>"+ pt.getY());
//close the browser
         driver.close();
         }
} 
  • To run the test script in Eclipse, right-click on the window and then click Run as à Java application.
  • The test script will be launched in the chrome browser and automate all the test scenarios.
  • And the output console window will show the following results for all the print commands.
output console window

 

 


Related Topics

Selenium-IDE Installation in Chrome

Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Google Chrome browser in your system. However, you can...

1 minute read.

Selenium WebDriver-CSS Selector: Tag Class and Attribute

Selenium WebDriver-CSS Selector: Tag, Class, and Attribute We will locate the web element with the help of Tag, class, and attribute CSS selector locator in this section of the tutorial. Where Tag,...

5 minutes read.

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods: The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc. Some of the most commonly used data-capture controls are...

7 minutes read.

Id Locator in Selenium IDE

Id Locator Id locator is used to find the web element by the help of id of that element because ID is likely to be unique for each web element. Target Format: Id=id of the...

2 minutes read.

CSS Selector Locator in Selenium IDE

It used to identify the web element based on HTML tag, id, class, and attributes. To describe the look and formatting of a document written in the markup language are known...

1 minute read.

WebDriver-Tag Name Locator

Selenium WebDriver-Tag Name Locator The tag name locator is used along with the findElements() method to identify multiple similar elements in the UI. It is used to locate the web element with...

3 minutes read.

CSS Selector: Attribute in Selenium IDE

CSS Selector: Attribute Locating the attribute of a CSS selector, we would access the Password textbox, which is present in the login form of Facebook. The Password textbox has a type attribute...

1 minute read.

Drag and Drop Event Handling

Drag and Drop Event Handling: In this tutorial, we will learn how to handle the drag and drop event in selenium WebDriver using the action class. Before going further in this tutorial...

6 minutes read.

Selenium WebDriver-CSS Selector: Tag and Attribute

Selenium WebDriver-CSS Selector: Tag and Attribute We will locate the web element with the help of Tag and attribute CSS selector in this section of the tutorial. Where Tag and attribute are...

5 minutes read.

Selenium WebDriver Tutorial

Seleniun WebDriver Introduction Selenium WebDriver is the essential tool of Selenium Tool Suite because it can directly communicate the browser without any server.  Selenium WebDriver is a browser automation framework that accepts...

3 minutes read.

Selenium vs QTP

In this section, we are comparing the most frequently used automation tool –Selenium and QTP. Selenium and QTP have relative advantages over each other- FeaturesSeleniumHP QTPLicensingSelenium is an open-source testing tool....

3 minutes read.

WebDriver-Id Locator

WebDriver-Id Locator: The Id Locator is used to identify any web element in UI with the help of the ID attribute. The First preference is always goes to ID locator while inspecting...

4 minutes read.

Verification Controls Methods in Selenium WebDriver

Verification Controls Methods  The verification controls methods are used to check whether the web element is displayed, enabled, and selected. Some of the most commonly used verification controls are as follow: Verification controls Description Return...

4 minutes read.

CSS Selector: Class in selenium IDE

CSS Selector: Class Identify a web element with the help of Class as a CSS Selector is like using id attribute. The only difference between Class and id is in their...

2 minutes read.

Advantages and Disadvantages of Selenium-IDE

Advantages and Disadvantages of Selenium-IDE Advantages of Selenium IDE: UI based record and playback tool. Easy to understand & developed a test script. Selenium-IDE test script export into web driver code & Selenium–RC It is...

1 minute read.

CSS Selector: Inner text in Selenium IDE

CSS Selector: Inner text The inner text is used to identify and create a CSS Selector using a string pattern of the HTML tag which is displayed on the web page. Syntax...

1 minute read.

Characteristics of Selenium IDE

Characteristics of Selenium IDE Selenium IDE is separated into different parts, each having their features and functionalities. The seven various components of Selenium IDE are as follows: MENU BAR: The menu bar is...

3 minutes read.

Selenium-IDE Locators

Selenium-IDE Locators: Before we start learning about Selenium –IDE Locators, firstly we have to learn about UI elements. UI Elements -UI elements are the elements which are displayed on the web application....

1 minute read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes read.

Selenium Tutorial

What is Selenium? Selenium is a functional testing tool and compatible with the non-functional testing tools also. Selenium is free and does not require any licensing. Functional tests script executed by...

6 minutes read.