Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, September 29, 2011

Get list of files in a directory using Java

While doing an automation I came up with a problem where I need to get a list of all files in a Directory.
So just though to share the same with you the program to do so. I had made used of recursion in the following programs to get the list of files. Following are the two programs one will give you a ArrayList of files with their absolute path where as another will give you an ArrayList of File object of respective files.
Following program will store the absolute path of all the files inside a directory to the fileList argument provided.

Following  program will store the File object of the files that are found into the respective fileList Array provided as an argument to the function

Monday, July 25, 2011

Sending an E-mail with your Test Execution Reports

While doing automation testing many of us had thought that it is good to send the excetuion report mail after your test execution. In this blog I am going to tell two ways using which you can send a mail with to multiple E-mail addresses with your execution report attached.

Using ant:
Ant supports a mail task which can be used as one of your ways for sending a mail. Following target can be used to send a mail using ant:




In the above target you need to mention the following things:
mailhost – this the sender host of your mail box. You can this from your outgoing configuration of account in you mail client. For gmail it is “smtp.gmail.com”

port – this is the port at which the above mailhost support sending of email. This configuration also you can check in your mailbox configuration. By default it is “25” but for SSL and TLS support it will be different for different hosts.

User/password – some mail hosts need the username and password of the account mail box to authenticate the user while sending a mail. You need to provide this in the “user” & “password” attributes.

ssl – if you want to use ssl for contacting the mailbox you need to set this attribute value to “true”



subject – Subject of the mail you want to give.

to – you need provide the address of the email recepients here using the “address” attribute. You use a property also here. This property you can setusing the properties file and then importing to the build.xml

attachments – this can be used to attach your report as part of the mail and then send it.

Please note: If you get a MIME error or java mail error while running the above target. Please download a jar of “java mail” and put it to the lib folder of your ant installation.

Using Java code:
When I had this problem of sending the execution report as mail, I did it in a hard way by writing the following java code. In the following code I had used the javax mail API for composing and sending the mail.