Tuesday, April 17, 2012

How to Upload / Attach any file or document with Selenium RC



Steps that need to follow
1. In order to upload files we need to have AutoIT (freeware BASIC-like scripting language designed for automating the Windows GUI -- keystrokes, mouse actions ).
2. Download AutoIT software from http://www.autoitscript.com/site/autoit/downloads/
3. Install upon clicking the downloaded AutoIT.exe file
4. If you want to upload a file into your application/system, create a document (Ex: lets say uploadFile.doc )
5. Next step is go to any folder in your drive and right click --> New --> AutoIT v3 Script    and select script.
6. It will open up AutoIT v3 script file, enter the following code at last and save the file as if you save any document with some name (Ex: lets say attachDocScript.au3  )


WinWaitActive("Choose File")
Send("C:\Program Files\AutoIt3\uploadFile.doc")   // specify your own path where attaching document is located.
Send("{ENTER}")


7. Close the AutoIT script file and right click on this, select compile the script -- which generates .exe file of the same. (Ex: attachDocScript.exe )
8. In your selenium program enter the following code after clicking the attach button from your application.


try {
String[] commands = new String[]{};
commands = new String[]{"C:\\Program Files\\AutoIt3\\attachDocScript.exe"}; //location of the autoit executable
Runtime.getRuntime().exec(commands);
    }
catch (IOException e) {}


9. Now run your progam. you Can see that you uploaded the document in your application successfully.

write the selenium output result into a document instead of console


If you want to display all the links into a text file or word document then you can do as follows
1. We have getAllLinks() in selenium which will prints all links to the console output.
     
     selenium.getAllLinks();

2. Instantiate FileWriter and BufferedWriter from java.io package.
4. If need to write the output into a text file then store all links into an String array as follows

     
 String alllinks [] = selenium.getAllLinks();

5. Use "for loop" to write the values into file with help of BufferedWriter object.


System.out.println("Get All Links");
String alllinks [] = selenium.getAllLinks();

FileWriter fileWriter2 = new FileWriter("D:\\allLinks.doc");
BufferedWriter bufferedWriter2 = new BufferedWriter(fileWriter2);
bufferedWriter2.write("Get All Links");
for(int c=0; c<alllinks.length; c++){

        bufferedWriter2.write(alllinks[c]);
        bufferedWriter2.newLine();
    System.out.println("\n");
} bufferedWriter2.close();