Saturday 14 December 2013

Conversion Html tc to excel

Convert Selenium IDE  HTML TC in to HTML format 


so we can run excel in Keyword driven Framework..

Src Given Below jst Add apache poi jars to ur Eclipse Project.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import javax.script.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExportToExcel {

/**
 * @param args
 * @throws IOException 
 */
int Stepno=0;
String str="";
String temp="";
static String Outputfile="";
List <String> Action=new LinkedList<String>();
List <String> Locator=new LinkedList<String>();
List <String> Value=new LinkedList<String>();
int i=4;
public void Export(String path) throws ScriptException, IOException
{
/*ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");*/
File f=new File(path);
 BufferedReader br = new BufferedReader(new FileReader(f));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append('\n');
            line = br.readLine();
        }
        str += sb.toString();
        
        //str="<td>aman</td>";
        String a[]=str.split("<td>");
        for (i=1;i<a.length;i=i+3)
        {
         temp=a[i].substring(0, a[i].indexOf('<'));
         Action.add(temp);
        } 
        for (i=2;i<a.length;i=i+3)
        {
         temp=a[i].substring(0, a[i].indexOf('<'));
         Locator.add(temp);
        } 
        for (i=3;i<a.length;i=i+3)
        {
         temp=a[i].substring(0, a[i].indexOf('<'));
         Value.add(temp);
        }      
        
      System.out.println("Action List is : "+Action);
      System.out.println("Locator List is : "+Locator);
      System.out.println("Value List is : "+Value);
        
    } finally {
        br.close();
    }  
     }
public void writetoExcel(String excelFileName) throws InvalidFormatException, IOException
{
//String excelFileName = "";//name of excel file
 
String sheetName = "Sheet1";//name of sheet
 
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(sheetName) ;
XSSFRow r1 = sheet.createRow(0);
XSSFCell Srnocell = r1.createCell(0);
Srnocell.setCellValue("Srno");
XSSFCell actioncell = r1.createCell(1);
actioncell.setCellValue("Action");
XSSFCell loccell = r1.createCell(2);
loccell.setCellValue("Locator");
XSSFCell valuecell = r1.createCell(3);
valuecell.setCellValue("Value");
for (int r=1;r <4; r++ )
{
XSSFRow row = sheet.createRow(r);
Stepno++; 
//iterating c number of columns
XSSFCell cell = row.createCell(0);
cell.setCellValue(Stepno);
    XSSFCell cell1 = row.createCell(1);
cell1.setCellValue(Action.get(r-1)); 
System.out.println(Action.get(r-1));
 
    XSSFCell cell2 = row.createCell(2);
cell2.setCellValue(Locator.get(r-1)); 
System.out.println(Locator.get(r-1));
    XSSFCell cell3 = row.createCell(3);
cell3.setCellValue(Value.get(r-1));
System.out.println(Value.get(r-1));
}
 
FileOutputStream fileOut = new FileOutputStream(excelFileName);
 
//write this workbook to an Outputstream.
wb.write(fileOut);
fileOut.flush();
fileOut.close();
System.out.println("Your excel file has been generated!");
JOptionPane.showMessageDialog(null,excelFileName+" is generated successfully");
}
public static void main(String[] args) throws ScriptException, IOException, InvalidFormatException {
// TODO Auto-generated method stub
ExportToExcel e=new ExportToExcel();
JFileChooser chooser=new  JFileChooser();
        int returnVal = chooser.showOpenDialog(null);
        
        if(returnVal == JFileChooser.APPROVE_OPTION) 
        {
        //File f = chooser.getSelectedFile();
        String filename=chooser.getSelectedFile().getPath();
        if (filename.endsWith(".html")){
         Outputfile=filename.replace(".html", ".xlsx");
         e.Export(filename);
e.writetoExcel(Outputfile);
        }
        else
        {
         JOptionPane.showMessageDialog(null, "Pls select only Recorded HTML file");
         //System.exit(0);
        }
        
    }

}}

Testlink Selenium Integration

Sample Eclipse Project ->>.Sample Java Selenium Project


Download the files needed:




Notes:
a. In order to make this work, make sure you can add the libraries of the Testlink client api package to your project.

b. Your testlink project must have the test cases as automatic execution and project must be enabled for automation.

2. Generate an API key from testlink
2.1 Go to http://127.0.0.1:8887/testlink-1.9.3/index.php and login with your credentials
2.2 Go to My Settings and generate a new api key.use this key in sample java proj in interface Iconstant.

Make sure that Test Plan, Test Case, Test Suite Name,Project Name, Build Name must be same for both testlink and initialized in Java Proj.



Now run the project as JUnit Test!!




Android Automation Selenium

Android Automation <Selenium >

Steps to Configure Selenium Web Driver in Android Device
Download the SDK zip file and unzip in your local computer.
Say download at “C:\android ” location
Go to unzipped folder and click on “Android SDK Manager” and install the following:
         a.Tools
         b.Extras
         c.Android API’s as per your choice (Say Android 2.2 , Android 4.0)

2. Install the ADT Plugin for Eclipse-
Goto Help > Install New Software....>> Add
3.Configure ADT Plugin
Window > Preferences...>>Android
Set the SDK location to where is your Android SDK has been installed.
Location will be something like:  C:\android\android-sdk-windows

4. Add the new AVD
In Eclipse goto Window >>AVD Manager>>
Create new AVD by clicking on “New” button.(Give the AVD  name  and select the Target)

5.Now download the Android APK 
Copy the file and paste it in the “Platform-tools” folder of the unzipped Android SDK.
Location will be something like “C:\android\android-sdk-windows\platform-tools “

6. Now start the Android AVD (Which has been created in Step 4) by following below in your Eclipse
Window >>AVD Manager>> Select the created AVD and click on “Start”

7.  Find the installed emulator device id .
open the command prompt ( cmd ) and run the following command 
adb devices
Emulator device id will be something like “emulator-5554

8. Now from the command prompt, go to the “Platform-tools” folder of the unzipped Android SDK
cd "C:\android\android-sdk-windows\platform-tools"

9. Install the Android server by running the following command
adb -s emulator-id -e install -r  android-server-2.21.0.apk

10. Start the Android WebDriver application by running the following command
adb -s emulator-id shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity -e debug true

11. Now setup the port forwarding in order to forward traffic from the host machine to the emulator by running the following command
adb -s emulator-id forward tcp:8080 tcp:8080
Create Java Project in Eclipse.
Run this below code :-



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

import org.openqa.selenium.android.AndroidDriver;

public class Sampletc  {

public static void main(String args[])throws Exception
{
WebDriver ad=new AndroidDriver();
System.out.println("Started");
ad.get("http://www.gmail.com");
System.out.println("Application Title"+ ad.getTitle());
Thread.sleep(2000);
ad.findElement(By.name("Email")).sendKeys("ur mail id");
System.out.println(ad.getTitle());
ad.findElement(By.name("Passwd")).sendKeys("ur password");
ad.findElement(By.name("signIn")).click();
System.out.println("Opened");
//ad.close();
}
}

Note: Port 8080 should be free.
Insdtead od Emulator id we can use attached device Id.

Thanks!!

Aman Saraf Jain