Ant file for running a suite file
For running a suite file provide the ant script with the task "testng". The "testng" task will only be executed if a task definition for testng is specified.
<property name="xml" value=""/>
<taskdef name="testng" classname="com.beust.testng.TestNGAntTask" classpathref="classpath"/>
<target name="run_tests" depends="compile the code">
<echo message="running tests" />
<testng classpathref="classpath of jars and other classes used" haltOnfailure="true">
<xmlfileset file="${xml}" />
</testng>
</target>
A xml suite file can be used to specify the test to be run. The parameters to be passed to the test etc.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="Regression1" preserve-order="true">
<parameter name="env" value="test"/>
<parameter name="driver" value="ie"/>
<classes>
<class name="mytests.First">
</class>
</classes>
</test>
</suite>
Save the above suite as Sample.xml. It contains the class to be executed, the parameters to be passed to the test etc.
Ant Command
ant -Dxml = Sample.xml
Thursday, July 21, 2011
Yaml as config file
One of the best options to specify a config file for java based selenium project is by using a yaml file. In the yaml file the commonly changing config values or values which are used in all tests can be given. There are number of parsers available. I am using snakeyaml.
Jar File
snakeyaml-1.6.jar
Sample yaml file
base: &base # defines anchor label base
location_username: 'xpath'
location_password: 'xpath'
location_submit: 'xpath'
page: 'http://xxxxxxxxxxx'
test:
<<: *base # merges key : value from base anchor
username: 'testtest'
password: '123456'
page: 'http://xxxxxxxxxxx'
prod:
<<: *base
username: 'testprod'
password: '123456'
page: 'http://xxxxxxxxxxx'
I have saved the above as sample.yaml in my config folder. The use of "*" and "&" is the inherit the values defined in "base" to "test" and "prod"
Sample java program
import org.yaml.snakeyaml.Yaml;
public class xxx{
protected Map<String, Map<String,Object>> fullConf;
protected Map<String,Object> conf;
Yaml yaml = new Yaml();
InputStream input = new FileInputStream(new File(System.getProperty("user.dir") + "/config/sample.yaml"));
this.fullConf = ((Map<String, Map<String,Object>>) yaml.load(input));
System.out.println("values of fullConf "+fullConf);
if (fullConf.containsKey("test")) {
this.conf = fullConf.get("test");
System.out.println("values of test env "+conf);
System.out.println("Username "+conf.get("username"));
System.out.println("Passowrd "+conf.get("password"));
System.out.println("Page "+conf.get("page"));
System.out.println("location_username "+fullConf.get("location_username"));
System.out.println("location_password "+fullConf.get("location_password"));
}
}
This program prints the contents of yaml. The variable "fullConf " contains all data of the yaml file as a "key, Map". The variable "conf " will have data of any specific node.
Project home
src
test.com
one/two/three
config/config.yaml
InputStream in = this.getClass().getClassLoader().getResourceAsStream("config/config.yaml");
System.out.println("Hello1 : "+in);
in = this.getClass().getResourceAsStream("/config/config.yaml");
System.out.println("Hello3 : "+in);
http://viralpatel.net/blogs/2009/10/loading-java-properties-files.html
Jar File
snakeyaml-1.6.jar
Sample yaml file
base: &base # defines anchor label base
location_username: 'xpath'
location_password: 'xpath'
location_submit: 'xpath'
page: 'http://xxxxxxxxxxx'
test:
<<: *base # merges key : value from base anchor
username: 'testtest'
password: '123456'
page: 'http://xxxxxxxxxxx'
prod:
<<: *base
username: 'testprod'
password: '123456'
page: 'http://xxxxxxxxxxx'
I have saved the above as sample.yaml in my config folder. The use of "*" and "&" is the inherit the values defined in "base" to "test" and "prod"
Sample java program
import org.yaml.snakeyaml.Yaml;
public class xxx{
protected Map<String, Map<String,Object>> fullConf;
protected Map<String,Object> conf;
Yaml yaml = new Yaml();
InputStream input = new FileInputStream(new File(System.getProperty("user.dir") + "/config/sample.yaml"));
this.fullConf = ((Map<String, Map<String,Object>>) yaml.load(input));
System.out.println("values of fullConf "+fullConf);
if (fullConf.containsKey("test")) {
this.conf = fullConf.get("test");
System.out.println("values of test env "+conf);
System.out.println("Username "+conf.get("username"));
System.out.println("Passowrd "+conf.get("password"));
System.out.println("Page "+conf.get("page"));
System.out.println("location_username "+fullConf.get("location_username"));
System.out.println("location_password "+fullConf.get("location_password"));
}
}
This program prints the contents of yaml. The variable "fullConf " contains all data of the yaml file as a "key, Map". The variable "conf " will have data of any specific node.
Project home
src
test.com
one/two/three
config/config.yaml
InputStream in = this.getClass().getClassLoader().getResourceAsStream("config/config.yaml");
System.out.println("Hello1 : "+in);
in = this.getClass().getResourceAsStream("/config/config.yaml");
System.out.println("Hello3 : "+in);
http://viralpatel.net/blogs/2009/10/loading-java-properties-files.html
Sunday, July 17, 2011
Execute a java class using Ant
Sample Ant Script
<project name="selenium-demo" default="compile" basedir=".">
<property name="build.dir" value="build" />
<property name="src.dir" value="src"/>
<property name="lib.dir" value="lib"/>
<property name="reports.dir" value="report"/>
<property name="xml" value="Sample.xml"/>
<property name="main-class" value="mytests.Sample"/>
<path id="classpath">
<pathelement location="${build.dir}/classes"/>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="compile">
<delete dir="${build.dir}"/>
<mkdir dir="${build.dir}/classes"/>
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" executable="javac">
<classpath refid="classpath"/>
</javac>
</target>
<target name="run_tests" depends="compile">
<java classname="${main-class}">
<classpath refid="classpath"/>
</java>
</target>
</project>
Save the above file as ant.xml.
Sample Java File
package mytests;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Sample{
public static void main(String str[]){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.addthis.com");
try {
Thread.sleep(5);
} catch (InterruptedException e) {
}
driver.close();
}
}
Save the java file as Sample.java and put it into a folder named "src" . Create a new folder and name it "lib" and paste the jar files to be used in the project. For above project only "selenium-server-standalone" will be enough. From the CMD go to the project location and excute the command ant run_tests. Now a new folder called build will be created and it will have all compiled classes.
<project name="selenium-demo" default="compile" basedir=".">
<property name="build.dir" value="build" />
<property name="src.dir" value="src"/>
<property name="lib.dir" value="lib"/>
<property name="reports.dir" value="report"/>
<property name="xml" value="Sample.xml"/>
<property name="main-class" value="mytests.Sample"/>
<path id="classpath">
<pathelement location="${build.dir}/classes"/>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="compile">
<delete dir="${build.dir}"/>
<mkdir dir="${build.dir}/classes"/>
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" executable="javac">
<classpath refid="classpath"/>
</javac>
</target>
<target name="run_tests" depends="compile">
<java classname="${main-class}">
<classpath refid="classpath"/>
</java>
</target>
</project>
Save the above file as ant.xml.
Sample Java File
package mytests;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Sample{
public static void main(String str[]){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.addthis.com");
try {
Thread.sleep(5);
} catch (InterruptedException e) {
}
driver.close();
}
}
Save the java file as Sample.java and put it into a folder named "src" . Create a new folder and name it "lib" and paste the jar files to be used in the project. For above project only "selenium-server-standalone" will be enough. From the CMD go to the project location and excute the command ant run_tests. Now a new folder called build will be created and it will have all compiled classes.
Subscribe to:
Posts (Atom)