Monday, July 25, 2011
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.
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.
Thursday, June 17, 2010
TestNg provides a lot of added features. One of the good thing it provides is test execution results in the form of XML. We can use this XML file for other type of report format generation. One of them is Junit reporting.
In this blog I will mention how to generate a Junit report for a TestNG execution.
Below ant target will generate the Junit report for the TestNG execution results.
In the above ant target you need to specify the path to the TestNG results xml file in the "dir" attribute under the "fileset" tag.
You need to mention the path where you want to generate the Junit report in the "todir" attribute under the "report" tag.
Also you can mention whether you want the Junit report with frames or without frames in the "format" attribute under the "report" tag.
Saturday, March 20, 2010
There are mainly two reporting framework available for Selenium RC they are Junit and Test-NG. There are two more frameworks Report-NG and testng-xslt but they are implemented over the Test-NG framework.
In this blog I will mention about generating a Junit Report for your Selenium RC test execution using Ant script. I had written a “build.xml” for Ant for executing Selenium cases and generating a Junit report for the said selenium execution. I will be discussing about the same in this blog. If someone doesn’t have an idea about Ant can follow my steps and can generate a Junit report for Selenium Execution.
Explaining the “build.xml”
- In Build.xml I am mainly defining 5 properties
o RELEASE_ROOT this has been set to the Java Project folder.
o SRC which defines the source folder of the project
o LIB which defines the lib folder of the project
o BIN which defines the binary folder where the classes needs to be pasted after compiling
o REPORT folder which defines the report folder where the test execution reports will be generated.
- There are 4 targets or tasks of ant inside the build.xml
o “init’ which deletes the binary folder and creates it again.
o “compile” which depends upon the “init” task and which compiles the java files under the source folder and paste the classes onto the binary folder.
o “run-single-test” which depends on “compile” and runs only a single Selenium Junit test mentioned in it. This task also generates a Junit html report under the report/html folder of Java project after Selenium Test execution is done.
o “run-batch-test” which depends on “compile” and runs multiple Selenium Junit test mentioned in it. This task also generates a Junit html report under the report/html folder of Java project after Selenium Test execution is done.
- In each of the compile, run-single-test and run-batch-test I am setting the classpath of the binary folder, the library folder and the source folder in case of “run-batch-test”.
- In targets “run-single-test” and “run-batch-test” I am using the “junit” task of test execution. This task provides two types of execution
o Single class execution which I had used in “run-single-test” target. There is a subtask “test” of “junit” that can used for single test class execution. It is written as follows:
<test name="test.TestClass" haltonfailure="no" todir="${REPORT}/xml" outfile="TEST-result">
<formatter type="xml" />
</test>
Here “name” is the test class name. “todir” is the directory where initial xml report needs to be generated. “outfile” is the xml file name for initial result xml file. “formatter” is the initial file format type.
o Batch test class execution which I had used in “run-batch-test” target. There is a subtask “batchtest” of “junit” that can used for single test class execution. It is written as follows:
<formatter type="xml" usefile="true" />
<batchtest fork="yes" todir="${REPORT}/xml">
<fileset dir="${SRC}">
<include name="**/Test*.java" />
</fileset>
</batchtest>
Here I am telling the “batchtest” what all test classes to include using the “fileset” and “include” task. Along with this if you want you can use the “exclude” task to exclude particular type of classes. In the following example the Junit will include all the test class starting from “Test” and exclude all the test class having “Base” word in their name.
<formatter type="xml" usefile="true" />
<batchtest fork="yes" todir="${REPORT}/xml">
<fileset dir="${SRC}">
<include name="**/Test*.java" />
<exclude name="**/*Base*.java" />
</fileset>
</batchtest>
5. For generating the test report I am using the “junitreport” task of ant. It takes all the xml file from the “report/xml” and generates an html report at “report/html”.
Setting up a Java Project for test execution using Ant.
In this I am just telling how to setup a basic java project for using the build.xml file attached with this blog and running the selenium test using
Preconditions:
1. Install Ant from apache.
2. Install Java JDK (not JRE) and add its bin folder to your “PATH” environment variable of your system. For checking JDK is mapped check whether "javac" is recognized in your system by typing "javac" in your command prompt.
3. Download the Selenium RC.
4. Test cases are already converted to Selenium Junit cases.
Steps:
- Create a Java project in your Java IDE. I am using Eclipse so I will refer to it.
- By default there will be the “src” folder inside your Java Project. Right click on the “src” folder and create a new package under it say”test”.
- Now copy your Selenium Junit classes to this new “test” package.
- Create a new folder called “lib” under your Java Project.
- Copy selenium-java-client-driver.jar, junit-4.5.jar(if you have another version of Junit 4 you can use that) and ant-junit.jar(can be found under the “lib” folder of your ant installation) to your “lib” folder of Java Project.
- Now create a folder called “build” under your Java Project and copy the “build.xml” file attached with this blog to the build folder you created.
- Now open the build.xml file for edit.
- Search for “run-single-test” in build.xml. Go to the “<test” under the “run-single-test” target .Replace the values for “name=” variable with your Selenium Junit class name. For ex if your class name is TestSelenium.java and it is under package “test” under “src” then replace the value with “test.TestSelenium” then save the build.xml file.
- Now start the Selenium RC server.
- Now go to the build folder of the Java Project using command prompt and run the command “ant run-single-test”
- It should execute your Selenium cases on selenium server and generate an html report onto the “report/html” folder of your Java project.
Similarly you can modify the “run-batch-test” target of build.xml and execute multiple Selenium Tests together.
Download build.xml from Here