Using this code snippet we can read data from any file (excel in this example) saved in computer memory and use that data to login in desired web application (Ultimatix in this example). From that logged in account, we will read some particular information (User name and Role in this example) and store in another text file in computer memory.
package excelImport; import com.thoughtworks.selenium.*; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.Writer; import jxl.*; public class dataDriven1 extends SeleneseTestCase { public void ultimatixInfo()throws Exception { try { selenium = new DefaultSelenium("localhost",4444,"*firefox C:\Documents and Settings\377075\My Documents\Mozilla Firefox 10.1\Mozilla Firefox\firefox.exe","https://www.gooole.com/"); selenium.start(); selenium.open("/"); //Reading Login Info from excel sheet from one location in Computer Workbook workbook = Workbook.getWorkbook(new File("C:\Documents and Settings\377075\test1.xls")); Sheet sheet = workbook.getSheet(0); int row_count = sheet.getRows(); int i=0; String user = null, pw = null; //System.out.println(row_count); for(int j=0;j<row_count-1;j++) { if(sheet.getCell(i,i).getContents().equalsIgnoreCase("uname")) { user=sheet.getCell(i,j+1).getContents(); //System.out.println(user); } if(sheet.getCell(i+1,i).getContents().equalsIgnoreCase("password")) { pw=sheet.getCell(1,j+1).getContents(); //System.out.println(pw); } //Login in ultimatix page with data obtained from excel sheet selenium.type("id=USER",user); selenium.type("id=PASSWORD",pw); selenium.click("id=login_button"); selenium.waitForPageToLoad("90000"); //Reading User's Role Info from ultimatix account String Role = selenium.getText("xpath=html/body/div[1]/div[1]/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td[2]/div[2]/span[2]"); //Reading User's Name from ultimatix account String Name = selenium.getText("xpath=html/body/div[1]/div[1]/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td[2]/div[2]/span[1]"); //Opening a new text file at one location and writing the User's Info there File file = new File("C://Documents and Settings/377075/My Documents/Downloads//write.txt"); Writer output = null; output = new BufferedWriter(new FileWriter(file)); output.write(user+":"+Name+":"+Role); output.close(); selenium.click("link=Log Out"); } selenium.stop(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { try { dataDriven1 obj = new dataDriven1(); obj.ultimatixInfo(); } catch (Exception e) { e.printStackTrace(); } } }