Tuesday 20 March 2012

Xpath Count in Selenium2 WebDriver

Selenium 1 uses 'getXpathCount' method to find the Xpath count of an atribute in the screen.
But in Selenium2 webdriver it is done by the following way:

int xpathCount= webDriver.findElements(By.xpath("//div[@id='billingProfiles']/div[@class='cardContainer']")).size();

please note that we are using 'findElements' instead of the usual 'findElement' method.

Monday 19 March 2012

TestNG Suite Set up and Tear Down

ackage selenium.src.com.ofb.supportClasses;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.events.WebDriverEventListener;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;

public class OfbTestSet{
   
    private WebDriver webDriver;
    private String testUrl, loginID, password;
   
    @BeforeMethod(alwaysRun = true)
    @Parameters({"browser", "testUrl", "loginID", "password"})
    public  void setUp(@Optional ("") String browser,
            @Optional ("") String testUrl,
            @Optional ("") String loginID,
            @Optional ("") String password) throws Exception{
       
        if (browser.contentEquals(""))
            browser = ReadProperty.commonValueProps.getProperty("browser");
        if (testUrl.contentEquals(""))
            testUrl = ReadProperty.commonValueProps.getProperty("testUrl");
        if (loginID.contentEquals(""))
            loginID = ReadProperty.commonValueProps.getProperty("LoginID");
        if (password.contentEquals(""))
            password = ReadProperty.commonValueProps.getProperty("Password");
       
        this.testUrl = testUrl; this.loginID = loginID; this.password = password;
       
        if (browser.contentEquals("firefox"))
        {
            //FirefoxProfile ofbFF = new FirefoxProfile(new File("OfbFirefoxProfile/data"));
            //ofbFF.setAcceptUntrustedCertificates(false);
            //ofbFF.setAssumeUntrustedCertificateIssuer(false);
            //ofbFF.setPreference("network.dns.disableIPv6", true);
            //ofbFF.setEnableNativeEvents(false);
           
            WebDriverEventListener eventListener = new CustomEventListener();
            webDriver = new EventFiringWebDriver(new FirefoxDriver()).register(eventListener);
        }
       
        if (browser.contentEquals("iexplorer"))
        {
            WebDriverEventListener eventListener = new CustomEventListener();
            webDriver = new EventFiringWebDriver(new InternetExplorerDriver()).register(eventListener);
        }
        webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
       
        webDriver.get(testUrl);
       
        //To maximize the browser window
        WebDriverBackedSelenium selenium = new WebDriverBackedSelenium(webDriver, "");
        selenium.windowMaximize();
    }

    @AfterMethod(alwaysRun = true)
    public void tearDown() throws Exception {
        webDriver.quit();
    }
   
    public WebDriver getWebDriver() {
        return webDriver;
    }

    public String getTestUrl(){
        return testUrl;
    }
   
    public String getLoginID(){
        return loginID;
    }
   
    public String getPassword(){
        return password;
    }

}

Tuesday 13 March 2012

How to install ANT on LINUX

How to install ANT on LINUX

Apache Ant is a Java library and command-line tool that help building software.
How to install:
  • Create downloads directory if you dont have one
  • Download tar version from http://ant.apache.org/bindownload.cgi
  • Extract
  • Rename the new directory as ant
  • Insert the path into ANT_HOME
  • Update the global PATH variable to include ANT_HOME
  • Run the Ant script fetch.xml to install almost all the dependencies the optional Ant tasks need.
See actual commands:

bash ~]$ mkdir downloads
[bash ~]$ cd downloads/
[bash downloads]$
[bash downloads]$
[bash downloads]$ wget http://mirror.candidhosting.com/pub/
 apache//ant/binaries/apache-ant-1.8.2-bin.tar.gz
[bash downloads]$ tar -zxvf apache-ant-1.8.2-bin.tar.gz
[bash downloads]$ mv apache-ant-1.8.2 ant
[bash downloads]$ ANT_HOME=/home/nx/ls/downloads/ant
[bash downloads]$ PATH=$PATH:${ANT_HOME}/bin
[bash downloads]$ ant -f fetch.xml -Ddest=system

TestNG XSLT Report Generation

Reports are vital part of any automation testing and with Selenium+TestNG framework we have the default TestNG reports, which some consider to be a little out of date... to over come this, we have a add on report for TestNG know as TestNG-XSLT which gives good graphical representation of the generated report.

TestNG-xslt generates user friendly reports using the TestNG results output (testng-results.xml). Its uses the pure XSL for report generation and Saxon as an XSL2.0 implementation.


For generating testng-xslt report for your project do the following:
1. Download the testng-xslt from

     http://code.google.com/p/testng-xslt/
2. Unzip and copy the testng-results.xsl from the testng-xslt folder(testng-xslt-1.1\src\main\resources) to your own project folder.
3. Now copy the saxon library from (testng-xslt-1.1\lib\saxon-8.7.jar)to your project lib folder.
4. Modify your build.xml of ant and add the following target to it.


<project name="test" basedir=".">
    <property name="LIB" value="${basedir}/libs" />
    <property name="BIN" value="${basedir}/bin" />
    <path id="master-classpath">
            <pathelement location="${BIN}" />
            <fileset dir="${LIB}">
                <include name="**/*.jar" />
            </fileset>
        </path>
    <target name="testng-xslt-report">
        <delete dir="${basedir}/testng-xslt">
        </delete>
        <mkdir dir="${basedir}/testng-xslt">
        </mkdir>
        <xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${basedir}/testng-xslt/index.html">
            <param expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir" />

            <param expression="true" name="testNgXslt.sortTestCaseLinks" />

            <param expression="FAIL,SKIP,PASS,CONF,BY_CLASS" name="testNgXslt.testDetailsFilter" />

            <param expression="true" name="testNgXslt.showRuntimeTotals" />

            <classpath refid="master-classpath">
            </classpath>
        </xslt>
    </target>
</project>

You need to provide the testng-xslt stylesheet the TestNG results xml(testng-results.xml) , the path to the style sheet testng-results.xsl and the output index.html path.

Also dont forget to add the saxon library to your target classpath else you will get an error. In my case it is the master-classpath.

Now run the ant target for report generation (in this case "testng-xslt-report
") and check the ouput folder configured by you for testng-xslt report.


Build XML for Selenium+TestNG+ANT framework

I have developed a build.xml that can handle the the build of a test suite and do the reporting. It even send the report as mail. For reporting we are using TestNG-XSLT

<project name="Automation" default="clean" basedir=".">
    <property name="build.dir" value="${basedir}/build"/>
    <property name="lib.dir" value="${basedir}/lib"/>
    <property name="src.dir" value="${basedir}/src"/>
    <property name="browser" value="/home/nxavier/Downloads/firefox/firefox"/>
    <target name="setClassPath">
        <path id="classpath_jars">
            <pathelement path="${basedir}/" />
            <fileset dir="${lib.dir}" includes="*.jar" />
        </path>
        <pathconvert pathsep=":" property="test.classpath" refid="classpath_jars" />
    </target>
    <target name="loadTestNG" depends="setClassPath">
        <taskdef resource="testngtasks" classpath="${test.classpath}"/>
    </target>
    <target name="init">
        <mkdir dir="${build.dir}"/>
        <tstamp>
            <format property="timestamp" pattern="dd-MM-yyyy_(HH-mm-ss)"/>
        </tstamp>
        <property name="build.log.dir" location="${basedir}/buildlogs"/>
        <mkdir dir="${build.log.dir}"/>
        <property name="build.log.filename" value="build_${timestamp}.log"/>
        <record name="${build.log.dir}/${build.log.filename}" loglevel="verbose" append="false"/>
        <echo message="build logged to ${build.log.filename}"/>
    </target>
    <target name="clean">
        <echo message="deleting existing build directory"/>
        <delete dir="${build.dir}"/>
    </target>
    <target name="compile" depends="clean,init,setClassPath,loadTestNG">
        <echo message="classpath:${test.classpath}"/>
        <echo message="compiling.........."/>
        <javac destdir="${build.dir}" srcdir="${src.dir}" classpath="${test.classpath}"/>
    </target>
    <target name="runTests" depends="compile">
        <testng classpath="${test.classpath}:${build.dir}">
            <xmlfileset dir="${basedir}" includes="Debug.xml"/>
        </testng>
    </target>
    <target name="report" depends="runTests">
        <delete dir="${basedir}/testng-xslt"/>
        <mkdir dir="${basedir}/testng-xslt"/>
        <xslt in="${basedir}/test-output/testng-results.xml"
        style="${basedir}/src/xslt/testng-results.xsl" out="${basedir}/testng-xslt/index.html" processor="SaxonLiaison">
            <param expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir"/>
            <param expression="true" name="testNGXslt.sortTestCaseLinks"/>
            <param expression="FAIL,SKIP,PASS,BY_CLASS" name="testNgXslt.testDetailsFilter"/>
            <param expression="true" name="testNgXslt.showRuntimeTotals"/>
            <classpath refid="classpath_jars"/>
        </xslt>
    </target>
    <target name="RunAndViewReport" depends="report">
        <exec executable="${browser}" spawn="yes">
       <arg line="'${basedir}/testng-xslt/index.html'" />
      </exec>
     </target>
    <target name="sendMail" depends="RunAndViewReport">
        <zip destfile="${basedir}/testng-xslt/Report.zip" basedir="${basedir}/testng-xslt"/>
        <mail mailhost="smtp.gmail.com" mailport="465" subject="Notification of TESTNG build" ssl="false" user="tester@gmail.com" password="password">
            <from address="tester@gmail.com"/>
            <to address="tester@gmail.com"/>
            <message>The build has finished. A details report of this build is aatched</message>
            <attachments>
                <fileset dir="testng-xslt">
                    <include name="**/*.zip"/>
                </fileset>
            </attachments>
        </mail>
    </target>
</project>