Sunday, November 17, 2013

Keyword Driven Frame work for Selenium

Keyword drivern FW:

1. Class files: yahoo.

package KeywordDriver;
import java.io.FileInputStream;
import org.testng.Reporter;
import jxl.Workbook;
import com.thoughtworks.selenium.DefaultSelenium;
public class yahoo extends Driver_yahoo
{
public static DefaultSelenium ds=new DefaultSelenium("localhost",1234,"*firefox","http://");
public static String subj;
public static void fn_open() throws Exception
{
//get sheet0 having URL
 fn_getdata(0);
 ds.start();
 ds.open(ws.getCell(1,1).getContents());
 ds.windowMaximize();
 Thread.sleep(5000);
//   driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
//   driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
//   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
public static void fn_login() throws Exception
{

//get sheet2 and 3 having obj and testdata
fn_getdata(2,3);
ds.type(ws2.getCell(0,1).getContents(),ws3.getCell(0, 1).getContents().trim());
ds.type(ws2.getCell(0,2).getContents(),ws3.getCell(0, 2).getContents().trim());
ds.click(ws2.getCell(0,3).getContents());
Thread.sleep(10000);

}
public static void fn_compose() throws Exception
{
//get sheet2 and 3 having obj and rep
fn_getdata(2,3);
subj=ws3.getCell(1,2).getContents();
ds.click(ws2.getCell(1,1).getContents().trim());
   Thread.sleep(5000);
   ds.type(ws2.getCell(1,2).getContents(),ws3.getCell(1,1).getContents());
   ds.type(ws2.getCell(1,3).getContents(),subj);
   ds.type(ws2.getCell(1,4).getContents(),ws3.getCell(1,3).getContents());
   ds.click(ws2.getCell(1,5).getContents());
   Thread.sleep(5000);
}
public static void fn_checkmail() throws Exception
{
//get sheet2 having obj
fn_getdata(2);
ds.click("link="+ws.getCell(2,1).getContents());
Thread.sleep(8000);
String sub=ds.getTable(ws.getCell(2,2).getContents()).trim();
if (sub.matches(subj))
{
 Reporter.log("<b><font color='green'>Mail is received</Font></b>         "+sub);
}
else
{
 Reporter.log("<b><font color='red'>Mail NOT received</Font></b>         "+sub);
}
}
public static void fn_logout()
{
ds.click("link=Sign Out");
ds.close();
}
private static void fn_getdata(int s1) throws Exception
{
fs=new FileInputStream("C:\\Test AutomationTesting\\TestAutomation\\src\\KeywordDriver\\data.xls");
wb=Workbook.getWorkbook(fs);
ws=wb.getSheet(s1);
}
private static void fn_getdata(int s1,int s2) throws Exception
{
fs=new FileInputStream("C:\\Test AutomationTesting\\TestAutomation\\src\\KeywordDriver\\data.xls");
wb=Workbook.getWorkbook(fs);
ws2=wb.getSheet(s1);
ws3=wb.getSheet(s2);
}

}

2. driver script:
package KeywordDriver;

import java.io.FileInputStream;
import java.lang.reflect.Method;

import jxl.Workbook;
import jxl.Sheet;

import org.testng.annotations.*;

public class Driver_yahoo 
{
   static Workbook wb;
   static Sheet ws,ws1,ws2,ws3;
   static FileInputStream fs;
   static String testr,classname,func;
   
   @Test
   public static  void fn_executetests() throws Exception
   {
  
    fs=new FileInputStream("C:\\ AutomationTesting\\Automation\\src\\KeywordDriver\\data.xls");
    wb=Workbook.getWorkbook(fs);
    //get TestCases sheet
    ws1=wb.getSheet(1);
    for(int r=1;r<ws1.getRows();r++)
    {
    testr=ws1.getCell(7,r).getContents();
   
    if(testr.matches("yes"))
    {   
   
    classname= ws1.getCell(5, r).getContents();
    @SuppressWarnings("unchecked")
Class c=  Class.forName(classname);
    func = ws1.getCell(6, r).getContents();
    Method m= c.getMethod(func, null);
    Object obj=c.newInstance();
    m.invoke(obj,null);
   
//     classname=ws1.getCell(5,r).getContents();
//     Class c=Class.forName(classname);
//     func=ws1.getCell(6,r).getContents();
//     Method m=c.getMethod(func,null);
//     Object obj=c.newInstance();
//     m.invoke(obj,null);
    }
    }
  }
   
}


3. Unexpected alerts handle:
package KeywordDriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class UnexpectedAlerts {

public static void main(String[] args) {
// TODO Auto-generated method stub
DesiredCapabilities ieCapabilities = DesiredCapabilities.firefox();

ieCapabilities.setCapability("unexpectedAlertBehaviour", "ignore");

WebDriver driver = new FirefoxDriver(ieCapabilities);
driver.get("http://google.com");
}

}


4. Sample program:

package KeywordDriver;

import org.testng.Reporter;

import com.thoughtworks.selenium.DefaultSelenium;

public class google1  
{
  static DefaultSelenium ds=new DefaultSelenium("localhost",1234,"*firefox","http://");
  public static void fn_searchdata() throws Exception
  {
ds.start();
ds.windowMaximize();
ds.open("www.google.co.in");
Thread.sleep(3000);
ds.type("q","selenium");
ds.click("btnG");
Thread.sleep(3000);
if(ds.isTextPresent("selenium"))
{
 Reporter.log("selenium text present");
}
else
{
 Reporter.log("selenium text not present");
}
  }
   public static void fn_check() throws Exception
  {
Thread.sleep(5000);  
String t=ds.getTitle();
if(t.contains("Google"))
{
Reporter.log("Google page is displayed correct");
}
else
{
Reporter.log("Google page is not displayed correct");
}
  } 

}



5. build.xml file:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>

<project name="yahoo" default="usage" basedir=".">  

<!-- ========== Initialize Properties =================================== 

-->
    <property environment="env"/>
    <property file="./build.properties"/>
    
    <property name="ws.home" value="e:/sel_may_wk"/>
    <property name="test.src" value="$ws.home/pr1/src"/>
<property name="test.dest" value="${ws.home}/pr1/bin"/>
    
    <target name="selenium-server">
        <java jar="${ws.home}/Lib/selnew.jar" fork="true" />
    </target>

    <target name="setClassPath" unless="test.classpath">
        <path id="classpath_jars">
            <fileset dir="${ws.home}/Lib" includes="*.jar"/>
        </path>
        <pathconvert pathsep=":" 
            property="test.classpath" 
            refid="classpath_jars"/>
    </target>

    <target name="init" depends="setClassPath">
        <tstamp>
            <format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
        </tstamp>
        <condition property="ANT" 
            value="${env.ANT_HOME}/bin/ant.bat" 
            else="${env.ANT_HOME}/bin/ant">
                    <os family="windows" />
        </condition>
        <taskdef name="testng" classpath="${test.classpath}"
               classname="org.testng.TestNGAntTask" />
    
    </target>
  
    <!-- all -->
    <target name="all">
    </target>
<!-- build -->
    <target name="build" depends="init">
    </target>

    <!-- run -->
    <target name="run" depends="init" >
        <testng classpath="${test.classpath}:${test.dest}" suitename="s1">
            <xmlfileset dir="${ws.home}/pr1/src" includes="testng.xml"/>
        </testng>
   
        <!--
        <testng classpath="${test.classpath}:${test.dest}" groups="fast">
            <classfileset dir="${test.dest}" includes="example1/*.class"/>
        </testng>
        -->
    </target>

    <target name="usage">
        <echo>
            ant run will execute the test
        </echo>
    </target>

    <!-- ****************** targets not used ****************** -->
 
</project>


6.
test data:




1 comment:

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...