Skip to content

Crazy blog

No money, no honey, no funny…

Monthly Archives: July 2011

1. Class MyAuto is class to run Selenium. It will class TCManager to manage flow of testcase

package com.testscripts;

import com.thoughtworks.selenium.SeleneseTestCase;

public class MyAuto extends SeleneseTestCase {
	
	private TCManager tcm;
	
	public void setUp() throws Exception {
		setUp("http://google.com/", "*chrome");
	}
	public void testRun() throws Exception {
		
		tcm = new TCManager(this.selenium);
		tcm.testcase1();
		
	}
	
}

2. Here is TCManager, it also read value in excel file

package com.testscripts;


import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;

public class TCManager {
	
	private Selenium selenium;
	private Excel excel;
	
	public TCManager(Selenium _selenium) {
		selenium = _selenium;
		excel = new Excel();
	}
	
	public void testcase1() {
		selenium.open("/");

		selenium.windowMaximize();
		selenium.type("q", "selenium IDE");
		selenium.click("btnG");
		
		excel.openConnection();
		excel.setSheet("testsheet");
		int row = 0;
		int col = 1;
		System.out.println( excel.getValueFromExcel(row,col) );
		pause(5000);
		excel.closeConnection();
		
		SeleneseTestCase.assertEquals(false, true);
	}
	
	public void pause(long milisecond) {
		try {
			Thread.sleep(milisecond);
		} catch (Exception e) {
			System.out.println("TCManager\\pause: " + e.toString());
		}
	}
}

3. Here is class Excel file

package com.testscripts;

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

public class Excel {

	 private Connection con;
	 private ResultSet rs;

	 public String dbURL = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};"
		 				 + "DBQ= "+ "resource\\FBFriends.xls;"
	  					 + "DriverID=22;READONLY=false";
	 
	 public void openConnection() {
		 try {
			 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			 con = DriverManager.getConnection(dbURL);
			 
		 } catch (Exception e) {
			System.out.println("Excel\\OpenConnection: " + e.toString());
		}
	 }
	 
	 public void closeConnection() {
		 if (con != null) {
			 try {
				 rs.close();
				 con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		 }
	 }
	 
	 public void setSheet(String sheetName) {
		 if(con == null) {
			 System.out.println("Not able to connect to MS EXCEL");
			 return;
		 }
		 try {
			  Statement stmnt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			  String query = "select * from [" + sheetName + "$]" ;
			    
			  rs = stmnt.executeQuery( query );  
			  
		 } catch (Exception e) {
			System.out.println("Excel\\setSheet: " + e.toString());
		}
	 }
	 
	 public ResultSet getResultSet() {
		 return rs;
	 }
	 
	 public String getValueFromExcel(int rowIndex,int colIndex) {
		String value = "";
		try {
			rs.absolute(rowIndex);
			value = rs.getString(colIndex);
		} catch (SQLException e) {
			System.out.println("Excel\\getValueFromExcel: " + e.toString());
		}
		return value;
	 }
	 
}
Advertisements

Tags: ,

If cannot go to facebook, or cannot upload image/picture to facebook, this post will help you 🙂

Update hosts file in C:\Windows\System32\drivers\etc

69.171.224.42 http://www.facebook.com
69.171.224.42 facebook.com
69.171.224.42 login.facebook.com
69.171.224.42 static.ak.connect.facebook.com
69.171.224.42 vupload.facebook.com
69.171.224.42 apps.facebook.com
69.171.224.42 graph.facebook.com
69.171.224.42 developers.facebook.com
69.171.224.42 upload.facebook.com