Wednesday, February 27, 2013

Selenium :Java Constructor Example

Constructor

  

public class Car
{
String mod;
int price;
public Car (String mod)
{
mod=mod;
//price=price;
}
public Car (String mod, int price )
{
this.mod=mod;
this.price=price;
}
public static void main(String[] args)
{
Car c1=new Car("Constructor 1");
System.out.println(c1.mod);
Car c2=new Car ("Constructor 2",10000);
System.out.println(c2.mod+"---"+c2.price);

}

}

Tuesday, February 26, 2013

Selenium: Parameterization

Parameterization

  1. TestNg should be installed before execution.
  2. Runas>TestNg



package package_name;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Parameter
{

@Test (dataProvider = "registerData")
public void SampleTest1A(String US,String Pass,String Id ,String name)
{
System.out.println(US+ "--"+Pass+ "--"+Id+ "--"+name);
}
@DataProvider
public Object[][] registerData()
{
Object[][] Data=new Object[3][4];//execute 3 Times , 4 Data Series taking 1 series each time
//Rows = No of times test should be repeated
//Columns = No of data
Data[0][0]="US1";
Data[0][1]="Pass1";
Data[0][2]="ID1";
Data[0][3]="Name1";
Data[1][0]="US2";
Data[1][1]="Pass2";
Data[1][2]="ID2";
Data[1][3]="Name2";
Data[2][0]="US3";
Data[2][1]="Pass3";
Data[2][2]="ID3";
Data[2][3]="Name3";

return Data;
}

}

Friday, February 22, 2013

Selenium : Apache POI ,xls vs xlsx


xls vs xlsx(http://poi.apache.org/spreadsheet/quick-guide.html#NewWorkbook)


// Use a file
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));

// Use an InputStream, needs more memory
Workbook wb = WorkbookFactory.create(new FileInputStream("MyExcel.xlsx"));

Thursday, February 21, 2013

Selenium : Import One Java file into another


Residing in Same package:

Example : 

 In the below example Sample1 is using a variable from OR , here there is no need to import any file as both files are residing in the same package , hence can be directly called .

------------------------File Name : Sample1 --------------------File1
public class Sample1
{
    public static void main(String[] args)   
    {       
        WebDriver oDriver=new FirefoxDriver();
        oDriver.get("https://www.google.co.in");
        WebElement oTitle=oDriver.findElement(By.xpath(OR.button));  // Directly using the static variable
    }
}
===================================================================

------------------------File Name : OR --------------------File2
public class OR {

   
        public static final String button="//button[@id='gbqfba']";
       

}
===================================================================


Residing in Different package:

One Java file can be imported into another by using "Package" keyword .

To do this a new package has to be created with Java files to be used, residing inside .In the below eg "File_StringCheck.java" is residing inside the package "utilityfunction" .Hence the below code can access the methods from "File_StringCheck.java" by  import Utilityfunction.

Eg :

import Utilityfunction.File_StringCheck;
package Utilityfunction;


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.swing.JOptionPane;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


import jxl.*;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;
import jxl.read.biff.BiffException;
import jxl.write.*;
import java.util.Date;



 
public class Test{
{





}
}

Selenium :To goto google.apps and get corresponding app name , downloaded times ,ratings .

Program to goto google.apps and get corresponding app name , downloaded times , ratings and update it in a excel file with TimeStamp .


Here we are doing following things :-
1.Read Data from Input Excel Sheet
2.Search the data in the input excel and store it in an array.
3.Create a new excel sheet with Time stamp.
4.Insert the gathered info into the newly created excel.

 

The data (i.e., the app to search )is fed using excel  Eg-"D:\\Selenium\\test\\test.xls";

EX :
NFS
AngryBirds



Format in Input Excel:
r1c1         r1c2        r1c3      r1c4   -----Column Names
Input      appname      stars   downloaded

r2c1       r2c2         r2c3       r2c4
 Nfs


r3c1       r3c2         r3c3       r3c4
AngryBirds




//package Utilityfunction;


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.swing.JOptionPane;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


import jxl.*;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;
import jxl.read.biff.BiffException;
import jxl.write.*;
import Utilityfunction.File_StringCheck;
import java.util.Date;



public class Test
{

public static void main(String[] args) throws BiffException, IOException, WriteException
{
//
String sSrc="D:\\Selenium\\test\\test.xls";
String sDest="D:\\Selenium\\test\\test_"+Time_stamp()+".xls";
int iRows,i,j,iCols;
String[] aRes;
Label Llabel;

Workbook workbook1 = Workbook.getWorkbook(new File(sSrc));
iRows=workbook1.getSheet(0).getRows();//-1;
iCols=workbook1.getSheet(0).getColumns();//-1; //getRows()-1;
String[][] App_data=new String[iRows][iCols];
Sheet oSheet1=workbook1.getSheet(0);

for ( i=0 ; i<=oSheet1.getRows()-1;i++)
{
for ( j=0 ; j<=oSheet1.getColumns()-1;j++)
{
App_data[i][j]=oSheet1.getCell(j, i).getContents();


}
}
workbook1.close();

WebDriver driver= new FirefoxDriver();
driver.get("https://play.google.com/store");
for ( i=1 ; i<=iRows-1 ;i++)
{
driver.findElement(By.id("search-text")).sendKeys(App_data[i][0]);//("nfs");
driver.findElement(By.id("search-button")).click();
aRes=(mData(driver)).split(";");
for (j=0 ; j<=aRes.length-1 ; j++)
{
App_data[i][j+1]=aRes[j];
}

driver.findElement(By.id("search-text")).clear();
}

WritableWorkbook workbook2 = Workbook.createWorkbook(new File(sDest));//, workbook1);
WritableSheet oSheet2 = workbook2.createSheet("First Sheet", 0);
for ( i=0 ; i<=App_data.length-1;i++)
{
for ( j=0 ; j<=App_data[0].length-1;j++)
{
Llabel = new Label(j, i, App_data[i][j]);
oSheet2.addCell(Llabel);
}
}
workbook2.write();
workbook2.close();
System.out.println("over");
driver.quit();
System.exit(0);
}


public static String Time_stamp() //TimeSatmp
{
//java.util.Date date = new java.util.Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return (((dateFormat.format(date)).replace("/", "_")).replace(":", "_")).replace(" ", "_");
}

public static String mData(WebDriver oBrowser)
{
if (oBrowser.getCurrentUrl().contains("https://play.google.com/store/search") == false)
{
System.out.println("Not in the website");
return "0";
}

String sApp_name=oBrowser.findElement(By.xpath("//li[1]/div/div[2]/a")).getText();
String sStars=oBrowser.findElement(By.xpath("//li[1]/div/div/div/div/div")).getAttribute("title");
String sDownloads=oBrowser.findElement(By.xpath("//li[1]/div/div[2]/div[2]//span")).getText();

return (sApp_name+";"+sStars+";"+sDownloads);
}
}

Wednesday, February 20, 2013

Selenium: Copy data from one Excel Sheet to another

Program to Copy 1 Data from 1 Excel to another



import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.swing.JOptionPane;
import jxl.*;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;
import jxl.read.biff.BiffException;
import jxl.write.*;
import Utilityfunction.File_StringCheck;
import java.util.Date;



public class Test
{

public static void main(String[] args) throws BiffException, IOException, WriteException
{
String sTS=Time_stamp();
//String sSrc=
msExcel_Copy_File("D:\\Selenium\\test\\test.xls", "D:\\Selenium\\test\\test_"+sTS+".xls", "Sheet1", "Sheet1");
//System.out.print(;//("D:\\Selenium\\test\\output_"+sTS+".xls"), workbook);
}

public static String Time_stamp()
{
//java.util.Date date = new java.util.Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return (((dateFormat.format(date)).replace("/", "_")).replace(":", "_")).replace(" ", "_");

}

public static boolean msExcel_Copy_File(String sSrc_File , String sDest_File ,String sSrc_Sheet,String sDest_Sheet) throws BiffException, IOException, RowsExceededException, WriteException
{
String sTemp;
Label Llabel;
Workbook workbook1 = Workbook.getWorkbook(new File(sSrc_File));
Sheet oSheet1=workbook1.getSheet(sSrc_Sheet);

WritableWorkbook workbook2 = Workbook.createWorkbook(new File(sDest_File), workbook1);
WritableSheet oSheet2 = workbook2.getSheet(sDest_Sheet);


for (int i=0 ; i<=oSheet1.getRows()-1;i++)
{
for (int j=0 ; j<=oSheet1.getColumns()-1;j++)
{
sTemp=oSheet1.getCell(j, i).getContents();
Llabel = new Label(j, i, sTemp);
oSheet2.addCell(Llabel);
}
}
workbook2.write();
workbook2.close();
workbook1.close();
return true;
}


}

Selenium:Time Stamp

TimeStamp

import java.io.File;
import
java.io.IOException;
import

java.text.SimpleDateFormat;
import

java.util.Locale;
import

javax.swing.JOptionPane;
import

jxl.*;
import

jxl.write.*;
import

jxl.write.Number;
import

jxl.write.biff.RowsExceededException;
import

jxl.read.biff.BiffException;
import

jxl.write.*;
import

Utilityfunction.File_StringCheck;
import

java.util.Date;
 

public class Test

{
public static void main(String[] args) throws BiffException, IOException, WriteException
{
system.out.print(Time_stamp());

}
public static String Time_stamp()
{
//java.util.Date date = new java.util.Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

Date date = new Date();
return (((dateFormat.format(date)).replace("/", "_")).replace(":", "_")).replace(" ", "_");
}
}

Selenium : Program on File operations and String operations as 2 diffrent classes called in main class

Program on File operations and String operations as 2 diffrent classes called in main class




import java.io.File;
import java.io.IOException;
import java.util.Locale;
import javax.swing.JOptionPane;
import jxl.*;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;
import jxl.read.biff.BiffException;
import jxl.write.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class File_StringCheck
{
public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException
{
String sDir = JOptionPane.showInputDialog("Enter any Directory");
cFile_Operations oFile_Operations=new cFile_Operations();

System.out.println(oFile_Operations.mCreate_New_Excel(sDir, "Sheet1"));
JOptionPane.showMessageDialog(null, "over");
}
}



class cCheck_String
{
static String sRes="";
static int i;

public cCheck_String( )
{
// TODO Auto-generated constructor stub
}



//aArr - is an string array type ,sPattern - any pattern of regular expression
//Returns - index of array elements matching in the form of string seperated by comma eg-2,3,4
public static String cCheck_Pattern_Array(String aArr[] , String sPattern)
{


Pattern oPattern = Pattern.compile(sPattern);
for (i=0 ; i <= aArr.length-1 ; i++ )
{
Matcher oMatcher = oPattern.matcher(aArr[i]);
if (oMatcher.find( )== true)
{
System.out.println(aArr[i]+"--matches");
sRes=sRes+","+i;
}
}
return sRes;
}

//sStr - is an string type ,sPattern - any pattern of regular expression
//returns true if matching else returns false
public boolean cString_match(String sStr, String sPattern)
{
Pattern oPattern = Pattern.compile(sPattern);
Matcher oMatcher = oPattern.matcher(sStr);
if (oMatcher.find( )== true)
{
System.out.println(sStr+"--matches");
return true;
}
else
{
System.out.println(sStr +"--no matches");
return false;
}
}

}



class cFile_Operations
{
public static boolean mFile_Exsistence(String sPath)
{
File oFile=new File(sPath);
if (oFile.exists()== true )
{
System.out.println("file exist--mFile_Exsistence");
return true;
}
else
{
System.out.println("file does not exist--mFile_Exsistence ");
return false;
}
}

public static boolean mFile_delete(String sFull_path)
{
if (mFile_Exsistence(sFull_path)== true )
{
File oFile=new File(sFull_path);
oFile.delete();
System.out.println("File Deleted--mFile_delete");
return true;
}
else
{
System.out.println("File does not exsist--mFile_delete");
return false;
}
}
//sFull_path="D:\Selenium\test\Excel.xls"
public static boolean mCreate_New_Excel(String sFull_path,String sSheet_Name) throws IOException, WriteException
{
if (mFile_Exsistence(sFull_path)== false )
{
WritableWorkbook workbook = Workbook.createWorkbook(new File(sFull_path));
workbook.createSheet(sSheet_Name, 0);
workbook.close();
System.out.println("Excel file created--mCreate_New_Excel");
return true;
}
else
{
System.out.println("Excel file not created--mCreate_New_Excel");
return false;
}
}

public static String[] mFiles_in_directory(String sDirectory_Path)
{
File oDir = new File(sDirectory_Path);
String[] aFiles = oDir.list();
return aFiles;
}
}