×

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

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.

WebDriver-Partial Link Text locator

WebDriver-Partial Link Text locator In selenium WebDriver, there is an additional way to find a web link element with the help of partial link text locator. The partial link text is used...

4 minutes read.

Selenium WebDriver - Firefox or Gecko (Marionette) browser

Selenium web driver-Firefox or Gecko (Marionette) browser In this tutorial, we are going to learn how to run the Selenium WebDriver test script in the Firefox Browser using the Gecko Driver. Before going...

4 minutes read.

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.

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.

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.

Selenium WebDriver – Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.

Drawback or Limitations of Selenium

Drawback or Limitations of Selenium Followings are the Drawback or limitations of Selenium:- Drawback /LimitationsDescriptionDesktop applications cannot be automated by Selenium.To test the desktop application, we have tools like Test Complete, Winnium, and...

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

Selenium WebDriver - Browser Controls Commands

Selenium WebDriver - Browser Controls Commands WebDriver includes operations like opening a browser, fetch the title, and closing the browser, etc. Some of the most commonly used Browser controls commands for Selenium WebDriver...

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

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.

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.

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-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

2 minutes 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 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.

Selenium WebDriver - IE [Internet Explorer] browser

Selenium WebDriver - IE [Internet Explorer] browser In this segment, we will understand how to run a test script with the help of the IE (Internet Explorer) browser. It is a standalone...

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