Sunday, January 26, 2014

RC practice Scripts

Xpath


import com.thoughtworks.selenium.DefaultSelenium;


public class Xpath {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DefaultSelenium selenium = new DefaultSelenium("localhost", 6666,
"*iehta", "http://");
selenium.start();
selenium.open("http://mail.in.com");
selenium.windowMaximize();
selenium.type("xpath=/html/body/div[1]/div[2]/div/table/tbody/tr[2]/td[3]/form/div/div[1]/p[3]/input[@id='f_id']","username");
selenium.type("xpath=/html/body/div[1]/div[2]/div/table/tbody/tr[2]/td[3]/form/div/div[1]/p[5]/input[@id='f_pwd']","password");

}

}

2. Validation:


import com.thoughtworks.selenium.DefaultSelenium;


public class Validation {

public static DefaultSelenium selenium=new DefaultSelenium("localhost",6666,"*iehta","http://");
public static String Bodytext()
{
selenium.start();
selenium.open("http://google.com");
selenium.windowMaximize();

if(selenium.isTextPresent("Nag"))
{
System.out.println("Already checked");
}
else{
selenium.select("", "label=one");
//selenium.type("q", "selenium");
selenium.check("chkbox");

//System.out.println("Alert is not available");
}

selenium.click("btnG");
return "pass";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bodytext();

}

}

3. Test DB


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class testdb {
public static Connection connection;
public static Statement statement;
public static ResultSet resultSet;
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(
"jdbc:sqlserver://servername;instance=SQLEXPRESS;databaseName=ccdata;","Username", "Password");
statement = connection.createStatement();
resultSet = statement.executeQuery("select * from emp");
while (resultSet.next()) {
System.out.println("EMPLOYEE NAME:" + resultSet.getString("emp name"));
System.out.println("EMPLOYEE ID:" + resultSet.getString("emp ID"));
}
} catch (Exception e) {
e.printStackTrace();
}

}

}

4.  TEST

import com.thoughtworks.selenium.DefaultSelenium;

public class Test {

public static DefaultSelenium selenium = new DefaultSelenium("localhost",
6666, "*iehta", "http://");

public static String fun1() throws Exception

{
try{
selenium.start();
selenium.open("//google.co.in");
Thread.sleep(30000);
selenium.type("q", "selenium");
selenium.click("btnG");
}catch(Exception e)
{
}
return "pass";
}

public static void main(String[] args) throws Exception

{
// TODO Auto-generated method stub
fun1();

}

}

5. Sync:


import com.thoughtworks.selenium.DefaultSelenium;


public class Sync {

/**
* @param args
*/
public static String google() throws Exception
{
DefaultSelenium selenium=new DefaultSelenium("localhost",6666,"*iehta","http://");
selenium.start();
selenium.open("http://mail.in.com");
selenium.windowMaximize();
selenium.type("f_id", "seleniumforum_Nageswar");
selenium.type("f_pwd", "selenium");
// selenium.setTimeout("100000");
selenium.click("//input[@value='' and @type='submit']");
//Thread.sleep(10000);
selenium.waitForPageToLoad("130000");
selenium.click("link=Sign out");
selenium.waitForPageToLoad("130000");
selenium.chooseCancelOnNextConfirmation();
selenium.chooseOkOnNextConfirmation();
return "pass";
}
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
google();

}

}
6. Reusability

import java.io.FileInputStream;

import jxl.Sheet;
import jxl.Workbook;

import com.thoughtworks.selenium.DefaultSelenium;
public class ReusableScript {
public static DefaultSelenium selenium = new DefaultSelenium("localhost",
6666, "*iehta", "http://");
public static String gmail() throws Exception {
selenium.start();
selenium.open("http://www.gmail.com");
selenium.windowMaximize();
Thread.sleep(2000);
selenium.click("link=Create an account »");
selenium.waitForPageToLoad("30000");
return "pass";
}
public static String Register() throws Exception {
FileInputStream fi = new FileInputStream(
"D:\\Framework\\TestData\\Register.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
for (int i = 1; i < s.getRows(); i++) {
if (!s.getCell(3, i).getContents().equals("")) {
if (s.getCell(2, i).getContents().contains("Text")) {
if (selenium
.isElementPresent(s.getCell(0, i).getContents())) {
selenium.type(s.getCell(0, i).getContents(), s.getCell(
3, i).getContents());
}
} else if (s.getCell(2, i).getContents().contains("Combo")) {
if (selenium
.isElementPresent(s.getCell(0, i).getContents())) {
selenium.select(s.getCell(0, i).getContents(), "label="
+ s.getCell(3, i).getContents());
}
} else if (s.getCell(2, i).getContents().contains("chkbox")) {
if (selenium
.isElementPresent(s.getCell(0, i).getContents())) {
selenium.click(s.getCell(0, i).getContents());
}

} else if (s.getCell(2, i).getContents().contains("Radio")) {
if (selenium
.isElementPresent(s.getCell(0, i).getContents())) {
selenium.click(s.getCell(0, i).getContents());
}

}
else if (s.getCell(2, i).getContents().contains("Button")) {
if (selenium
.isElementPresent(s.getCell(0, i).getContents())) {
selenium.click(s.getCell(0, i).getContents());
}
  }
}

}

return "pass";
}

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
gmail();
Register();

}

}

7. Keys Events

import java.awt.event.KeyEvent;
public class KeyEvents extends DriverScript{
public static void main(String[] args) {
// TODO Auto-generated method stub
selenium.start();
selenium.open("http://www.google.com");
selenium.windowMaximize();
selenium.setCursorPosition("q", "1");
   selenium.keyDownNative(Integer.toString(KeyEvent.VK_SHIFT));
   selenium.keyPressNative(String.valueOf(KeyEvent.VK_D));
   selenium.keyUpNative(Integer.toString(KeyEvent.VK_SHIFT));
   //selenium.keyDownNative(Integer.toString(KeyEvent.VK_SHIFT));
   selenium.keyPressNative(Integer.toString(KeyEvent.VK_SEMICOLON));
   selenium.keyUpNative(Integer.toString(KeyEvent.VK_SHIFT));
   selenium.keyPressNative(Integer.toString(KeyEvent.VK_SLASH)); 
   selenium.keyPressNative(String.valueOf(KeyEvent.VK_S));
   selenium.keyPressNative(String.valueOf(KeyEvent.VK_E));
   selenium.keyPressNative(String.valueOf(KeyEvent.VK_L));
   selenium.keyPressNative(String.valueOf(KeyEvent.VK_E));
   selenium.keyPressNative(String.valueOf(KeyEvent.VK_N));

}

}

8. Get Data

import com.thoughtworks.selenium.DefaultSelenium;


public class GetData {

public static DefaultSelenium selenium=new DefaultSelenium("localhost",6666,"*iehta","http://");
public static String Bodytext()
{
selenium.start();
selenium.open("http://google.com");
selenium.windowMaximize();
selenium.type("q", "abc");
String str=selenium.getBodyText();
//String str=selenium.getHtmlSource();
String link[]=selenium.getAllWindowNames();
System.out.println("The links are"+link.length);
System.out.println("The links are"+link[0]);
selenium.captureScreenshot("D:\\Selenium\\page.jpg");
return "pass";
}
public static String Dynamic()
{
selenium.click("add1");
String windname[]=selenium.getAllWindowNames();
String windid[]=selenium.getAllWindowIds();
String windtitle[]=selenium.getAllWindowTitles();
selenium.getValue("");
System.out.println("The window names are"+windname.length);
selenium.selectWindow(windname[1]);
selenium.click("ok");
return "pass";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bodytext();
}

}

9. EXCEL Login

import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.thoughtworks.selenium.DefaultSelenium;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class Export_Login {
public static String status = "pass";
public static String export() throws Exception {
FileInputStream file = new FileInputStream(
"D:\\Framework\\TestData\\login_Data.xls");
Workbook w = Workbook.getWorkbook(file);
Sheet s = w.getSheet(0);
FileOutputStream fo = new FileOutputStream(
"D:\\Framework\\Results\\Loginout.xls");
WritableWorkbook wwb = Workbook.createWorkbook(fo);
WritableSheet ws = wwb.createSheet("login", 0);
String a[][] = new String[s.getRows()][s.getColumns()];
DefaultSelenium selenium = new DefaultSelenium("localhost", 6666,
"*iehta", "http://");
selenium.start();
selenium.open("http://mail.in.com");
selenium.windowMaximize();
for (int i = 0; i < s.getRows(); i++) {
selenium.type("f_id", s.getCell(0, i).getContents());
selenium.type("f_pwd", s.getCell(1, i).getContents());
selenium.click("//input[@value='' and @type='submit']");
Thread.sleep(10000);
if (selenium.isElementPresent("link=Sign out")) {
selenium.click("link=Sign out");
Thread.sleep(6000);
status = "Pass";
System.out.println("if loop" + status);
} else {
status = "Fail";
System.out.println("else loop" + status);
}
for (int j = 0; j < s.getColumns(); j++) {

a[i][j] = s.getCell(j, i).getContents();
Label l = new Label(j, i, a[i][j]);
Label Res = new Label(2, 0, "Result");
Label rs = new Label(2, i, status);
ws.addCell(l);
ws.addCell(Res);
ws.addCell(rs);
System.out.println("The contents are" + a[i][j]);
}
selenium.open("http://mail.in.com");
}
wwb.write();
wwb.close();
return "pass";
}

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

export();
}

}
10.EXCEL

import java.io.FileOutputStream;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Export_Excel {
public static String export() throws Exception
{
FileOutputStream fo=new FileOutputStream("D:\\Framework\\Results\\Result_export.xls");
WritableWorkbook wwb=Workbook.createWorkbook(fo);
WritableSheet ws=wwb.createSheet("Res", 0);
Label  l=new Label(1,0,"Nagesh");
Label l1=new Label(0,0,"Rao");
Label l2=new Label(2,3,"Duplicate");
Label l3=new Label(0,0,"Minq");
ws.addCell(l);
ws.addCell(l1);
ws.addCell(l2);
wwb.write();
wwb.close();
return "pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
export() ;

}

}

11. Execute Test Case


public class Execute_Testcases {
 public static void  main(String[] args) throws Exception
   {
      DriverScript.Set_Execution_sheet("D:\\Framework\\Environment\\url.xls");
      DriverScript.Execute("D:\\Framework\\MainScript\\testcases.xls");
       
   }

}

12. EXcel

import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import com.thoughtworks.selenium.DefaultSelenium;
public class Excel extends DriverScript{
public static String login_Excel() throws Exception
{
//DefaultSelenium selenium=new DefaultSelenium("localhost",6666,"*iehta","http://");
FileInputStream file=new FileInputStream("D:\\Framework\\TestData\\login_Data.xls");
Workbook w=Workbook.getWorkbook(file);
Sheet s=w.getSheet(0);
FileInputStream fi=new FileInputStream("D:\\Framework\\ObjectRepository\\login_OR.xls");
Workbook w1=Workbook.getWorkbook(fi);
Sheet s1=w1.getSheet(0);
//Sheet s=w.getSheet("Sheet1");
/*selenium.start();
selenium.open("http://mail.in.com");
selenium.windowMaximize();
*/
// for (int i = 1; i < s.getRows(); i++) {
selenium.type(s1.getCell(0, 1).getContents(), s.getCell(0, 1).getContents());
selenium.type(s1.getCell(1, 1).getContents(), s.getCell(1, 1).getContents());
//System.out.println("the value of i"+i);
//}
// selenium.setTimeout("100000");
selenium.click(s1.getCell(2, 1).getContents());
return "pass";
}
public static String close()
{
selenium.close();
return "pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//login_Excel();

}

}

13. Driver Script

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

import com.thoughtworks.selenium.DefaultSelenium;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class DriverScript{
        static String className = null;
        public static String urlpath=null;
        public static int rs=1;
        public static String result=null;
        public static DefaultSelenium selenium=new DefaultSelenium("localhost",6666,"*iehta","http://");
        public static boolean Set_Execution_sheet(String strPath) throws Exception
        {    
            String urlpath=strPath;    
             FileInputStream file= new FileInputStream(urlpath);
            Workbook wb=Workbook.getWorkbook(file);
            Sheet sh=wb.getSheet(0);
            selenium.start();
            selenium.open(sh.getCell(1, 1).getContents());
            selenium.windowMaximize();
            return true;
         }
       public static boolean Execute(String str) throws Exception
        {
        FileInputStream file=new FileInputStream(str);
        Workbook wb=Workbook.getWorkbook(file);
        Sheet s=wb.getSheet(0);
         for(int iRownum=1;iRownum<=s.getRows();iRownum++)
         {  
            String ScreenShotPath="D:\\Framework\\Results\\"+"screenshot"+rs +".jpg";
             try
            {
             if(!(s.getCell(5, iRownum).getContents()).contentEquals(""))
            {
                 className=s.getCell(5, iRownum).getContents();
            }
             else{
                 for(int i=iRownum;i>1;i--)
                 {
                     if(!(s.getCell(5, i).getContents()).contentEquals(""))
                     {
                         className=s.getCell(5, i).getContents();
                         break;
                     }
                 }
             }
            String functioncall = s.getCell(6, iRownum).getContents();
            int inumofparameters=0;
            Object ret = null;
            String methodName = functioncall.substring(0,functioncall.indexOf("("));
            String strparameters = functioncall.substring(functioncall.indexOf("(")+1,functioncall.indexOf(")"));
            String strparameterslist[]=strparameters.split(",");
            inumofparameters= strparameterslist.length;
            selenium.captureScreenshot(ScreenShotPath);
            if(strparameters.length()==0){
            inumofparameters=0;
                                          }                    
            Class  c = Class.forName(className);
             if(inumofparameters!=0){
                  Class cParameters[]=new Class[inumofparameters];
               for(int temp=0;temp<inumofparameters;temp++)
                 {
                 cParameters[temp]= String.class;
                 }
                 Method m = c.getMethod(methodName,cParameters );
                 Object ob = c.newInstance();
                 Object arglist[] = new Object[inumofparameters];
                for(int iparameternum=0;iparameternum<inumofparameters;iparameternum++)
                {
                arglist[iparameternum] = new String(strparameterslist[iparameternum]);
                }
                ret = m.invoke(ob,arglist);             
                }else
                 { 
                       Method m = c.getMethod(methodName,null);
                      Object ob = c.newInstance();
                       ret = m.invoke(ob,null); 
                      System.out.println("return value:"+ ret);
                 }
             rs++;
                }catch(Exception e){
                     selenium.captureScreenshot(ScreenShotPath);
                     e.printStackTrace();
                 }                     
            }             
                 
         return true;
        
     }
  }

14. DOM


public class DOM extends DriverScript{
public static String click(String str)
{
String index=selenium.getEval("var x=selenium.browserbot.getCurrentWindow().document.getElementsByTagName(\"a\");for(var i=0;i<x.length;i++){if(x[i].innerHTML.trim()=='"+str+"')i*1;}");
    selenium.getEval("var x=selenium.browserbot.getCurrentWindow().document.getElementsByTagName(\"a\");x["+ index + "].click();");
    return "pass";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
selenium.start();
selenium.open("http://www.google.com");
selenium.windowMaximize();
click("Hindi");

}

}

15. Current Time

import java.util.*;

public class CurrentTime{
  public static void main(String[] args){
    Calendar calendar = new GregorianCalendar();
    String am_pm;
    int hour = calendar.get(Calendar.HOUR);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    if(calendar.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
    else
      am_pm = "PM";
    System.out.println("Current Time : " + hour + ":" 
+ minute + ":" + second + " " + am_pm);
  }

16. Create Excel

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Create_Excel {
public static String dateNow=null;
public static void time()
{
 Calendar currentDate = Calendar.getInstance();
 SimpleDateFormat formatter= 
 new SimpleDateFormat("yyyyMMMdd_HHmmss");
  dateNow = formatter.format(currentDate.getTime());
 System.out.println("Now the date is :=>  " + dateNow);
}
public static String excel() throws Exception
{
time();
FileOutputStream fo=new FileOutputStream("E:\\Framework\\Results\\loginRes_"+dateNow+".xls");
WritableWorkbook wwb=Workbook.createWorkbook(fo);
WritableSheet ws=wwb.createSheet("LoginResults", 0);
Label l=new Label(0,0,"Username");
Label l1=new Label(1,0,"Password");
Label l2=new Label(2,0,"Results");
ws.addCell(l);
ws.addCell(l1);
ws.addCell(l2);
wwb.write();
wwb.close();
return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
excel();
}

}

17. Create Excel 1

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Create_Excel1 {
public static String dateNow=null;
public static void time()
{
 Calendar currentDate = Calendar.getInstance();
 SimpleDateFormat formatter= 
 new SimpleDateFormat("yyyyMMMdd_HHmmss");
  dateNow = formatter.format(currentDate.getTime());
 System.out.println("Now the date is :=>  " + dateNow);
}
public static String excel() throws Exception
{
time();
FileOutputStream fo=new FileOutputStream("E:\\Framework\\Results\\loginres"+dateNow+".xls");
WritableWorkbook wwb=Workbook.createWorkbook(fo);
WritableSheet ws=wwb.createSheet("LoginResults", 0);
Label lab=new Label(0,0,"Username");
Label pw=new Label(1,0,"Password");
Label rs=new Label(2,0,"Result");
ws.addCell(lab);
ws.addCell(pw);
ws.addCell(rs);
wwb.write();
wwb.close();
return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
excel();

}

}

18. Data Base Script

public class DataBaseTest {

/**
* @param args
* @throws SQLException 
*/
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
Connection connection = null;
ResultSet objResultSet=null;
PreparedStatement objPreStmnt=null;
try {
DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
String url="jdbc:microsoft:sqlserver://psdbs01\\psdbs01A:1717;DatabaseName=ARROW_0_4_5_0";
connection=DriverManager.getConnection(url,"arrow_sqa","arrow_sqa");
System.out.println(">>>>>>>>>Connected>>>>>>>>");

String strQuery = "select case_id,workitemid,userid from workbasket where case_id=? and workitemid=? and userid=?"; 

//Assigning the Query 
objPreStmnt = connection.prepareStatement(strQuery);
objPreStmnt.setInt(1,2026086); 
objPreStmnt.setInt(2,2000093);
objPreStmnt.setInt(3,2000297);
objResultSet = objPreStmnt.executeQuery(); //Executing the Query
while (objResultSet.next()) { 
System.out.println(" case_id:"+objResultSet.getString("case_id"));
System.out.println(" workitemid:"+objResultSet.getString("workitemid"));
System.out.println(" userid:"+objResultSet.getString("userid"));

} catch (Exception e) {
e.printStackTrace();
}finally{
objResultSet.close();
objPreStmnt.close();
connection.close();
}

}


}


19. Driver Script


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import com.thoughtworks.selenium.DefaultSelenium;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class DriverScript {
      static String className = null;
      public static int rs = 1;
public static DefaultSelenium selenium = new DefaultSelenium("localhost",1212,"*firefox", "http://");
     public static boolean Set_Execution_sheet(String strPath) throws Exception {
            FileInputStream file = new FileInputStream(strPath);
            Workbook wb = Workbook.getWorkbook(file);
            Sheet sh = wb.getSheet(0);
            selenium.start();
            selenium.open(sh.getCell(1, 1).getContents());
            selenium.windowMaximize();
            return true;
      }
      public static boolean Execute(String str) throws Exception {
     //To take the data from Testcases.xls
            FileInputStream file = new FileInputStream(str);
            Workbook wb = Workbook.getWorkbook(file);
            Sheet s = wb.getSheet(0);
          //To create the out put Result file
            FileOutputStream fo=new FileOutputStream("E:\\Framework\\Results\\loginResults.xls");
            WritableWorkbook wwb=Workbook.createWorkbook(fo);
            WritableSheet ws=wwb.createSheet("LoginResults", 0);
            //Add a label 'Result'
            Label res=new Label(8,0,"Results");
            ws.addCell(res);
            //To take each row data
            for (int i = 1; i <= s.getRows(); i++) {
        try {
        if(s.getCell(7, i).getContents().equalsIgnoreCase("Yes"))
        {
        //Taking the class name from 5,i
                if (!(s.getCell(5, i).getContents()).contentEquals("")) {
                  className = s.getCell(5, i).getContents();
                 }
              //Taking the method name from 6,i
                String functioncall = s.getCell(6, i).getContents();        
                               // object declaration
                               Object ret = "Fail";
                               //Reading the method name which is available in functioncall string
                               String methodName = functioncall.substring(0, functioncall.indexOf("("));
                               //Converting the classname
                               Class c = Class.forName(className);
                               //Taking the method name from the class
                                Method m = c.getMethod(methodName, null);
                                //Creating the instance
                                Object ob = c.newInstance();
                                //Executing the method 
                               ret = m.invoke(ob, null);
                               
                                System.out.println("return value:" + ret.toString());
                                //Capturing screen shot
                                selenium.captureScreenshot("E:\\Framework\\Results\\Result" + rs + ".jpg");
                               rs++;
                            for (int j = 0; j < s.getColumns(); j++) {
                    System.out.println(s.getCell(j, i).getContents());
                    Label data=new Label(j,i,s.getCell(j, i).getContents());
                    ws.addCell(data);
                    }
        }
                          } catch (Exception e) {
                        selenium.captureScreenshot("E:\\Framework\\Results\\Result"+ rs + ".jpg");
                  }
            }
       wwb.write();
       wwb.close();
            return true;

      }
}

20. Export Input Data

import java.io.FileInputStream;
import java.io.FileOutputStream;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExportInputdata_Step2 {
public static String export() throws Exception
{
FileInputStream fi=new FileInputStream("E:\\Framework\\Testdata\\logindata.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
FileOutputStream fo=new FileOutputStream("E:\\Framework\\Results\\loginResults.xls");
WritableWorkbook wwb=Workbook.createWorkbook(fo);
WritableSheet ws=wwb.createSheet("LoginResults", 0);
for (int i = 0; i < s.getRows(); i++) {
for (int j = 0; j < s.getColumns(); j++) {
System.out.println(s.getCell(j, i).getContents());
Label data=new Label(j,i,s.getCell(j, i).getContents());
ws.addCell(data);
}
}
Label res=new Label(2,0,"Results");
ws.addCell(res);
wwb.write();
wwb.close();
return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
export();
}

}

21. google Link1

import com.thoughtworks.selenium.DefaultSelenium;
public class Google_link1 {
public static DefaultSelenium selenium;
public static String str=null;
public static void wait(String str) throws Exception
{
for (int i = 0; i < 30; i++) {
if(selenium.isElementPresent(str))
{
Thread.sleep(1000);
selenium.isVisible(str);
break;
}
}
}
public static String openURL()
{
selenium=new DefaultSelenium("localhost",1213,str,"http://");
selenium.start();
selenium.setTimeout("1000000");
selenium.open("http://www.google.co.in/");
selenium.windowMaximize();
return "Pass";
}
public static String hindi() throws Exception
{
try{
selenium.click("link=Hindi");
wait("link=English");
selenium.click("link=English");
Thread.sleep(6000);
}catch(Exception e){e.printStackTrace();}
return "Pass";
}
public static String bengali() throws Exception
{
selenium.click("link=Bengali");
Thread.sleep(6000);
selenium.click("link=English");
Thread.sleep(6000);
return "Pass";
}
public static String telugu() throws Exception
{
selenium.click("link=Telugu");
Thread.sleep(6000);
selenium.click("link=English");
Thread.sleep(6000);
return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
str="*firefox";
for (int i = 0; i < 2; i++) {
openURL();
hindi();
bengali();
telugu();
}
}

}
22. Google Link2

import com.thoughtworks.selenium.DefaultSelenium;
public class Google_link2 {
private static DefaultSelenium selenium=new DefaultSelenium("localhost",1212,"*firefox","http://");
public static String openURL()
{
selenium.start();
selenium.open("http://www.google.co.in/");
selenium.windowMaximize();
return "Pass";
}
public static String Click_link(String str) throws Exception
{
selenium.click("link="+str);
Thread.sleep(6000);
selenium.click("link=English");
Thread.sleep(6000);
return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
openURL();
Click_link("Hindi");
Click_link("Bengali");
Click_link("Telugu");
}

}

23. Google Search 

import com.thoughtworks.selenium.DefaultSelenium;


public class GooglesSearch extends DriverScript{

public static String gs()
{
//DefaultSelenium selenium=new DefaultSelenium("localhost",1212,"*firefox","http://");
//selenium.start();
//selenium.open("http://google.co.in");
//selenium.windowMaximize();
selenium.type("gbqfq", "Nagesh");
selenium.click("gbqfb");
return "Pass";
}


}

24. JUNIT

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Junit extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 1212, "*chrome", "http://www.mail.in.com/");
selenium.start();
}

@Test
public void testUntitled() throws Exception {
selenium.open("/");
selenium.windowMaximize();
selenium.click("css=input.registernowjgfjvku");
selenium.waitForPageToLoad("30000");
selenium.type("id=fname", "nagesh");
selenium.select("id=day", "label=13");
selenium.select("id=month", "label=Mar");
selenium.select("id=year", "label=2000");
selenium.type("id=username", "nagesh_2000");
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

25. Login_OR_TD
import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import com.thoughtworks.selenium.DefaultSelenium;
public class Login_OR_TD {
public static DefaultSelenium selenium=new DefaultSelenium("localhost",1212,"*firefox","http://");
public static String openURL()
{
selenium.start();
selenium.open("http://www.mail.in.com/");
selenium.windowMaximize();
return "Pass";
}
public static String login() throws Exception
{
FileInputStream fi=new FileInputStream("E:\\Framework\\Testdata\\logindata.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
FileInputStream fi1=new FileInputStream("E:\\Framework\\Object Repository\\loginOR.xls");
        Workbook w1=Workbook.getWorkbook(fi1);
        Sheet s1=w1.getSheet("Sheet1");
selenium.type(s1.getCell(0, 1).getContents(), s.getCell(0, 1).getContents());
selenium.type(s1.getCell(1, 1).getContents(), s.getCell(1, 1).getContents());
selenium.click(s1.getCell(2, 1).getContents());
selenium.waitForPageToLoad("30000");
return "Pass";
}
public static String logout()
{
selenium.click("link=");
return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
openURL();
login();
logout();

}

}

26. Login Results 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.thoughtworks.selenium.DefaultSelenium;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Login_Results_Step3 {
public static DefaultSelenium selenium=new DefaultSelenium("localhost",1212,"*firefox","http://");
public static String login() throws Exception
{
//To take data from Input file
FileInputStream fi=new FileInputStream("E:\\Framework\\Testdata\\logindata.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
//To create the out put Result file
FileOutputStream fo=new FileOutputStream("E:\\Framework\\Results\\loginResults.xls");
WritableWorkbook wwb=Workbook.createWorkbook(fo);
WritableSheet ws=wwb.createSheet("LoginResults", 0);
//String declaration
String str=null;
//Launching the application
selenium.start();
selenium.open("http://www.mail.in.com/");
selenium.windowMaximize();
for (int i = 1; i < s.getRows(); i++) {
//To login into the application by taking data from xls 
selenium.type("f_id", s.getCell(0, i).getContents());
selenium.type("f_pwd", s.getCell(1, i).getContents());
selenium.click("css=input.signin");
selenium.waitForPageToLoad("30000");
//Validating the Sign out link
if(selenium.isElementPresent("link=Sign out"))
{
selenium.click("link=Sign out");
Thread.sleep(3000);
str="Pass";
}else{
str="Fail";
System.out.println("Sign out is not available");
}
// To get the home page
selenium.open("http://www.mail.in.com/");
//Export the result into Result file
Label result=new Label(2,i,str);
ws.addCell(result);
// To export input data into Result file
for (int j = 0; j < s.getColumns(); j++) {
System.out.println(s.getCell(j, i).getContents());
Label data=new Label(j,i,s.getCell(j, i).getContents());
ws.addCell(data);
}
}
// Adding labels in the Result file
Label un=new Label(0,0,"Username");
Label pw=new Label(1,0,"Password");
Label res=new Label(2,0,"Results");
ws.addCell(un);
ws.addCell(pw);
ws.addCell(res);
wwb.write();
wwb.close();
return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
login();
}

}

27. Login ReTesting

import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import com.thoughtworks.selenium.DefaultSelenium;
public class Login_Retesting {
public static DefaultSelenium selenium=new DefaultSelenium("localhost",1212,"*firefox","http://");
public static String openURL()
{
selenium.start();
selenium.open("http://www.mail.in.com/");
selenium.windowMaximize();
return "Pass";
}
public static String login() throws Exception
{
FileInputStream fi=new FileInputStream("E:\\Framework\\Testdata\\logindata.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
FileInputStream fi1=new FileInputStream("E:\\Framework\\Object Repository\\loginOR.xls");
        Workbook w1=Workbook.getWorkbook(fi1);
        Sheet s1=w1.getSheet("Sheet1");
        for (int i = 1; i < s.getRows(); i++) {
        selenium.type(s1.getCell(0, 1).getContents(), s.getCell(0, i).getContents());
        Thread.sleep(1000);
    selenium.type(s1.getCell(1, 1).getContents(), s.getCell(1, i).getContents());
}
//selenium.click(s1.getCell(2, 1).getContents());
//selenium.waitForPageToLoad("30000");
return "Pass";
}
public static String logout()
{
selenium.click("link=");
return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
openURL();
login();
//logout();

}

}

28. Mail Login


public class Mail_Login extends DriverScript{
//public static DefaultSelenium selenium=new DefaultSelenium("localhost",1212,"*firefox","http://");
public static String openURL()
{
/*selenium.start();
selenium.open("http://www.mail.in.com/");
selenium.windowMaximize();*/
return "Pass";
}
public static String login()
{
selenium.type("f_id", "mar1selenium");
selenium.type("f_pwd", "selenium");
selenium.click("css=input.signin");
selenium.waitForPageToLoad("30000");
return "Pass";
}
public static String logout()
{
if(selenium.isElementPresent("link=Sign out"))
{
selenium.click("link=Sign out");
System.out.println("Pass");
}else{
System.out.println("Fail");
}

return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

}

}

29. Mail Reg

public class Mail_Reg extends Mail_Login{

public static String reg() throws Exception
{
selenium.click("css=input.registernow");
selenium.waitForPageToLoad("30000");
selenium.type("fname", "Nagesh");
selenium.select("day", "5");
selenium.select("month", "Apr");
selenium.select("id=year", "label=2001");
selenium.type("id=username", "nagesh2001");
selenium.type("id=password", "nagesh123");
selenium.type("id=repassword", "nagesh123");
selenium.type("id=altemail", "nag@gmail.com");
selenium.click("id=imageField");
Thread.sleep(2000);
selenium.click("id=imageField");
selenium.select("mcountry", "India");

return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
openURL();
reg();
}

}


30. Spice Jet popup

import com.thoughtworks.selenium.DefaultSelenium;


public class Spicejet_Popup {
public static DefaultSelenium selenium=new DefaultSelenium("localhost",1234,"*firefox","http://");
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
selenium.start();
selenium.open("http://spicejet.com");
selenium.windowMaximize();
selenium.click("id=oneWayRadio");
selenium.select("id=from1Select", "label=Hyderabad");
selenium.select("id=to1Select", "label=Vizag");
selenium.click("css=#to1Select > option[value=\"VTZ\"]");
selenium.click("id=departDate1text");
selenium.click("link=5");
selenium.click("id=submitBtn");
selenium.getConfirmation();
Thread.sleep(10000);
selenium.click("id=m1f1r1");
selenium.click("id=converterButton");
selenium.waitForPopUp("converter", "30000");
selenium.selectWindow("name=converter");
selenium.type("id=amountText", "100");
selenium.select("name=convert_from", "label=US Dollar (USD)");
selenium.select("name=convert_to", "label=Indian Rupee (INR)");
Thread.sleep(2000);
selenium.click("id=closeButton");
Thread.sleep(2000);
//selenium.waitForPageToLoad("30000");
selenium.selectWindow("null");
selenium.click("id=nextDayButton1");
selenium.waitForPageToLoad("30000");

}

}

31. Step 2
import java.io.FileInputStream;
import java.io.FileOutputStream;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Step2 {
public static String export() throws Exception
{
FileInputStream fi=new FileInputStream("E:\\Framework\\Testdata\\logindata.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
FileOutputStream fo=new FileOutputStream("E:\\Framework\\Results\\loginresults.xls");
WritableWorkbook wwb=Workbook.createWorkbook(fo);
WritableSheet ws=wwb.createSheet("LoginResults", 0);
for (int i = 0; i < s.getRows(); i++) {
for (int j = 0; j < s.getColumns(); j++) {
System.out.println(s.getCell(j, i).getContents());
Label lab=new Label(j,i,s.getCell(j, i).getContents());
ws.addCell(lab);
}
}
Label rs=new Label(2,0,"Results");
ws.addCell(rs);
wwb.write();
wwb.close();
return "Pass";
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
export();
}

}

32. Time

import java.util.Calendar;
import java.text.SimpleDateFormat;

public class Time {
  public static void  main(String arg[]) {
  Calendar currentDate = Calendar.getInstance();
  SimpleDateFormat formatter= 
  new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
  String dateNow = formatter.format(currentDate.getTime());
  System.out.println("Now the date is :=>  " + dateNow);
  }
}























No comments:

Post a 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...