42. Using getValue( ) method




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 RC has a predefined method getValue( ) 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 = object1.getValue( );   // The field value is retrieved by the getValue( ) Selenium 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 Project say 'RC-Project14' in Eclipse IDE
2. Configure the Project to work with Selenium RC
3. Configure the Selenium Standalone Server to run from Eclipse IDE
4. Start the Selenium Standalone Server
5. Create a package say 'package_Fourteen' under the newly created project.
6. Create a Java Class file say 'Class14' 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. Start the Selenium Standalone Server
3. Save and Run the 'Class14.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 getValue( ) Selenium RC predefined method that "Selenium-RC" text is available in the 'Type in a Search' text box field

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. Start the Selenium Standalone Server
3. Save and Run the 'Class14.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 getValue( ) Selenium RC predefined method that "BLABLABAL" text is not available in the 'Type in a Search' text box field

4.  As we have specified assertTrue("Specifed value is not available in the 'Type in a Search' text box field"Field_Value.constains("BLABLABLA"));  in the code, i.e. we've used second kind of assertTrue( ) JUnit predefined method here. That is if the test fails, "Specifed value is not available in the 'Type in a Search' text box field" message need  to be displayed in the JUnit -> Failure Trace as shown below:



Watch the below video:

Click here to watch the video.


Please comment below to feedback or ask questions.

Using *firefox for Running our Tests on Firefox Browser will be explained in the next post.





No comments:

Post a Comment

Followers

Labels