Thursday, February 21, 2013

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);
}
}

No comments:

Post a Comment