37. Using getAttribute("value") method for retrieving text from the text fields






First lets find  out what a Value is by following the below steps:

1. Open http://www.compendiumdev.co.uk/selenium/search.php
2. View the value inside the 'Type in a search Text box' field as shown below:

3. So in the above screenshot, 'Selenium-RC' text displayed inside the 'Type in a search' text box field.

Selenium WebDriver has a predefined method getAttribute("value") which retrieves the value inside the fields like Text box, Text Area and any other Form fields containing text and assigns the retrieved title to a String object as shown below:

String field_value;   //String object 'field_value' is created
field_value = _driver.findElement(By.name("Name Locator")).getAttribute("value")  // The field value is retrieved by the getAttribute("value") Selenium WebDriver predefined method and assigned to the String object.

We will use the Java's predefined method contains( ) to verify whether the Page Title retrieved by the getTitle( ) method contains the specified string as shown below:

Pass Case -> field_value.contains("Selenium-RC");
Fail Case -> field_value.contains("BLABLABLA");

We will also use the second format of assertTrue( ) JUnit's predefined method to either PASS or FAIL the test using the result (i.e. True or False) returned by the field_value.contians("SpecifedText") as shown below:

assertTrue("Text to be displayed when the specified text is not available in the field", field_value.contains("Specified Text"));

Lets Implement the Pass Case:

Pre-requisites:

1. Create a new Java Project say 'WebDriver-Project11' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package11' under the newly created project.
4. Create a Java Class file say 'Class11' under the newly created package as shown below:





Actual Steps:

1. Write the following code into the newly created Java Class file as shown below and make sure that you resolve all the errors before going to next step:




2. Now lets write Selenium WebDriver code for creating a String object, storing the retrieved value of the field in the created String object and finally verifying whether the specified String text is available in the retrieved field value as shown below:



3. Save and Run the 'Class11.java' file by selecting the 'JUnit Test' option and ensure that Test got passed as shown below:




As the test got passed, now its verified by the getAttribute("value") Selenium WebDriver predefined method that "Selenium-RC" text is available in the field value.

Watch the below video:

Click here to watch the video.

Download this Project:


Click here to download this project and import into Eclipse IDE  on your machine.

Lets Implement the Fail Case:


1. In the above code replace 'Selenium-RC' text with 'BLABLABLA' text as shown below:



2. Save and Run the 'Class11.java' file by selecting the 'JUnit Test' option and ensure that Test got failed as shown below:



As the test got failed, now its verified by the getAttribute("value") Selenium WebDriver predefined method that "BLABLABLA" text is not available in the field value.


Watch the below video:

Click here to watch the video.


Please comment below to feedback or ask questions.

Using FirefoxDriver for running Selenium WebDriver Automation Tests on Firefox Browser
 will be explained in the next post.


Followers

Labels