×

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

WebDriver-Link Text locator

WebDriver-Link Text locator The Link text locator is used to identify only web link elements based on the value of the link. The Link text locators cannot be used for other than...

4 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.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

Selenium Tool Suite

The selenium tool suite is a combination of multiple software tools, and the entire tools having a different approach to support automation testing. Selenium test tool suites are as follows- Selenium...

2 minutes read.

Selenium IDE Tutorial for Beginners

Selenium IDE Introduction Selenium IDE stands for Integrated Development Environment. Selenium-IDE is released in 2006 and developed by SHINYA [JAPAN] THOUGHTWORK Company. Selenium IDE implemented as a Firefox plug-in, and concedes the record, edit, and debug tests....

2 minutes read.

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.

Difference between Selenium WebDriver and Selenium RC

Difference between Selenium WebDriver, and Selenium RC: Selenium WebDriver and selenium RC (Remote Control) both are different in any manners which we see later in this tutorial, let us see the...

3 minutes read.

Selenium WebDriver- Radio button handling

Selenium WebDriver- Radio button handling In this tutorial, we will be learning about handling the radio button in selenium WebDriver. Let us take one example to give you a better understanding of...

3 minutes read.

Selenium WebDriver-CSS Selector locator

Selenium WebDriver-CSS Selector locator A CSS selector is used to identify the web element based on their “# id” and “. Class”, Html tags, and attributes. A CSS stands for Cascading Style...

1 minute read.

Selenium WebDriver Commands

Selenium WebDriver Commands: As we learn previously in the Selenium-IDE tutorial, Selenium commands are the set of commands that are used to run a Selenium test script. But whereas, Selenium WebDriver had...

2 minutes read.

CSS Selector: ID in Selenium IDE

CSS Selector: ID The id attribute is used to identify the web element uniquely in the web pages. Syntax of ID Attribute:  [HTML tag][#][Value of ID attribute] The hash sign (#): it is mandatory while using the...

1 minute read.

Link Text Locator in Selenium IDE

LINK TEXT Link textlocator is used to identify only the weblink element based on the name of the link. Whenever multiple links are there, it will not work; it will work only...

1 minute read.

Selenium Wait

In this tutorial, we will understand how we can avoid synchronization issues while executing the test script in a selenium WebDriver. Before going further in this tutorial, first, we will understand that...

10 minutes read.

Operational Controls Methods in Selenium WebDriver

Operational Controls Methods The Operational controls are used to perform all the actions on the web elements like click on the button, pass the value in the text box, and click...

4 minutes read.

Selenium WebDriver Characteristics

Characteristics of Selenium WebDriver Some of the essential characteristics of Selenium WebDriver are as follows: Multiple Operating System Support Selenium Webdriver supports multiple operating systems like: Desktop OS: Window, Linux, and macOS Mobile OS:...

2 minutes read.

Selenium WebDriver-Locators

Selenium WebDriver-Locators The WebDriver uses the same set of locating strategies like selenium -IDE for specifying the location of the web element. Each locating strategy has its command in Java to locate...

1 minute read.

First test case of Selenium-IDE

First Test Case of Selenium-IDE: In this part, we are discussing how to create a basic test case in Selenium-IDE: The entire test script execution process in Selenium -IDE can be divided...

4 minutes read.

Selenium WebDriver – Web Element Controls

Selenium WebDriver – Web Element Controls A web element is an interface that is implemented by a remote web element class. The Web element control can be used to perform the data...

1 minute read.

Selenium WebDriver - Google Chrome Browser

Selenium WebDriver - Google Chrome Browser In this tutorial, we will learn how to run our Selenium Test Scripts in the Google Chrome Browser. To implement the WebDriver protocol with the help...

3 minutes read.

Selenium WebDriver-CSS Selector: Substring Matching

CSS Selector: Substring Matching Selenium WebDriver In this tutorial, we will learn how to identify the web element using the substring method. A Sub-string contains three methods for identifying the web element...

9 minutes read.