127. Using type command to browse a file






On uploading a file using Browse Button, the path of the file to be uploaded gets displayed in the File field. So resultant of this operation is to display the file path in File field.

Hence in Selenium Automation, we use type command to get the file path displayed in the File field as shown below:

selenium.type("css path for locating the file field", "Path of the file with \\ escape characters");

Why should we provide \\ Java escape characters while providing the file path in the above statement ?

Suppose an example path of a file is C:\abc.txt . When the type command types the original file path C:\abc.text it wont recognize the \ character. In order to type \ character into the text field, we've to use the Java escape character \\ as shown in the below example:

selenium.type("css path for locating the file field", "C:\\abc.txt");

Test Description:



Pre-requisite 1:

1. Go to C Drive as shown below:



2. Create a New Folder and name it as FolderOne as shown below:



3. Inside FolderOne folder, right click and select New -> Text Document option as shown below:


4. Name the create Text Document as abc as shown below:


5. Open the 'abc.txt' file and enter some text into the file as shown below:


6. Save the text file and close.
7. Now the path of this text file 'abc.txt' will be C:\FolderOne\abc.txt
8. So the path with Java Escape Character \\ which is used to enter \ when used with type command is C:\\FolderOne\\abc.txt


Pre-requisite 2:

1. Create a new Project say 'RC-Project43' in Eclipse IDE
2. Configure the Project to work with Selenium RC
3. Configure the Selenium Standalone Server to run from Eclipse IDE
4. Create a package say 'package43' under the newly created project.
5. Create a Java Class file say 'Class43' 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. Write the test method 'browseATextFile()' which uses CSS Path Statement for locating the 'Filename' text box field  as shown below:



3. Start the Selenium Standalone Server
4. Save and Run the 'Class43.java' file by selecting the 'JUnit Test' option and ensure that our Automation Test has enter the file path C:\FolderOne\abc.txt into the 'Filename' textbox field as shown in the below video:

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.


Please comment below to feedback or ask questions.

Using click command for clicking a button  will be explained in the next post.









No comments:

Post a Comment

Followers

Labels