Google+

412. Using sendKeys( ) command to browser 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 sendKeys( ) command to get the file path displayed in the File field as shown below:

_driver.findElement(By.cssSelector("CSS path for locating the file field")).sendKeys("Path of the file with \\ escape characters");

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");
_driver.findElement(By.cssSelector("css path for locating the file field")).sendKeys("C:\\abc.txt");


Test Description:



Pre-requisite before Implementation:

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


Lets Implement This:

Pre-requisites:

1. Create a new Java Project say 'WebDriver-Project56' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package56' under the newly created project.
4. Create a Java Class file say 'Class56' 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. Save and Run the 'Class56.java' file by selecting the 'JUnit Test' option and ensure that our Automation Test has typed the required file path into the Filename Text Box field as shown below:





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( ) for clicking a button will be explained in the next post.


 

No comments:

Followers :)

Blog Index