PageRenderTime 79ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/framework/hybrid.java

https://github.com/satishija/Hybrid-Framework
Java | 261 lines | 201 code | 23 blank | 37 comment | 18 complexity | 373fcb16baa2004c92c7dfde386b48d1 MD5 | raw file
  1. package com.framework;
  2. //package com.example.tests;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import org.apache.poi.hssf.usermodel.HSSFCell;
  7. import org.apache.poi.hssf.usermodel.HSSFRow;
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  10. import org.junit.After;
  11. import org.junit.Before;
  12. import org.junit.Test;
  13. import com.thoughtworks.selenium.DefaultSelenium;
  14. import com.thoughtworks.selenium.SeleneseTestCase;
  15. @SuppressWarnings("deprecation")
  16. public class hybrid extends SeleneseTestCase {
  17. private static final String String = null;
  18. // Global variables - accessed any where in this class
  19. public String vExp, vInc, vLoan;
  20. public double vTExp, vTInc, vTLoan;
  21. public String[][] xData;
  22. public int xRows, xCols;
  23. @Before
  24. public void setUp() throws Exception {
  25. myprint("Now creating our Selenium object");
  26. // String xPath = "C:\Selenium\Jul5\csl-data.xls"; Use forward slash instead of backward for file path
  27. String xPath = "C:/Selenium/Jul5/csl_data.xls";
  28. xlRead(xPath); // Reading the excel data
  29. myprint("XL data read and the rows are " + xRows);
  30. //selenium = new DefaultSelenium("localhost", 1235, "*iehta", "http://www.chasestudentloans.com/");
  31. selenium = new DefaultSelenium("localhost", 1236, "*chrome", "http://www.chasestudentloans.com/");
  32. myprint("Now launching Selenium Log and App browser");
  33. selenium.start();
  34. }
  35. @Test
  36. public void testCsl1() throws Exception {
  37. String vTui, vFood, vCC; // Declare Expense variables
  38. String vJob, vGrants; // Declare income variables
  39. int vET, vIT, vLT, vR; // Col numbers for the output values
  40. vET = 11;
  41. vIT = 12;
  42. vLT = 13;
  43. vR = 14;
  44. myprint("Starting my main TEST");
  45. for (int i = 1; i < xRows; i++ ){
  46. myprint("Row# " + i + " being executed");
  47. // Read from each row in the XL into variables
  48. vTui = xData[i][1];
  49. vFood = xData[i][4]; // Taking value into a new variable called vTui
  50. vCC = xData[i][7];
  51. vJob = xData[i][8];
  52. vGrants = xData[i][9];
  53. myprint("Tui is " + vTui + " Job is " + vJob);
  54. // Simulate the application functionality
  55. vTExp = Double.parseDouble(vTui) + Double.parseDouble(vFood) + Double.parseDouble(vCC);
  56. vTInc = Double.parseDouble(vJob) + Double.parseDouble(vGrants);
  57. vTLoan = vTInc - vTExp;
  58. myprint("Expense total FROM Script is " + vTExp);
  59. myprint("Income total FROM Script is " + vTInc);
  60. myprint("Loan FROM Script is " + vTLoan);
  61. // Test Step 1
  62. myprint(ts_001("Budget Calculators", "25000"));
  63. // Test Step 2
  64. myprint(ts_002(vTui, vFood, vCC));
  65. // Test Step 3
  66. myprint(ts_003(vJob, vGrants));
  67. // Test Step 4
  68. myprint(ts_004());
  69. // Test Step 5 - Compare the results
  70. if (ts_005()== "Pass") {
  71. xData[i][vR] = "Pass";
  72. } else {
  73. xData[i][vR] = "Fail";
  74. }
  75. // Value from the app back into the array
  76. xData[i][vET] = vExp;
  77. xData[i][vIT] = vInc;
  78. xData[i][vLT] = vLoan;
  79. }
  80. myprint("Ending my main TEST");
  81. }
  82. @After
  83. public void tearDown() throws Exception {
  84. String res_path= "C:/Selenium/Jul5/csl_data1.xls";
  85. myprint("Stopping the Selenium RC");
  86. selenium.stop();
  87. xlwrite(res_path, xData);
  88. myprint("@ After - all done");
  89. }
  90. // Our customized Functions
  91. public void myprint(String mymessage){
  92. System.out.println(mymessage);
  93. System.out.println("~~~~~~~~~~~~~");
  94. }
  95. public String ts_001(String LinkName, String waitTime){
  96. //Test Step 1 - Open, go to Bud Calc, wait for page to load
  97. myprint("Now launching Selenium Log and App browser");
  98. selenium.open("/");
  99. selenium.click("link=" + LinkName);
  100. selenium.waitForPageToLoad(waitTime);
  101. return "TS001 is a Pass";
  102. }
  103. public String ts_002 (String vFTui, String vFFood, String vFCC){
  104. selenium.type("tuition", vFTui);
  105. selenium.type("food", vFFood);
  106. selenium.type("creditcards", vFCC);
  107. return "TS002 is a Pass";
  108. }
  109. public String ts_003(String vFJob, String vFGrants){
  110. // Income start here
  111. selenium.type("job", vFJob);
  112. selenium.type("grants", vFGrants);
  113. return "TS003 is a Pass";
  114. }
  115. public String ts_004(){
  116. // Clicking on calculate
  117. selenium.click("//input[@class='calcbutton1']");
  118. vExp = selenium.getValue("totexp");
  119. myprint("Expense total is " + vExp);
  120. vInc = selenium.getValue("totinc");
  121. myprint("Income total is " + vInc);
  122. vLoan = selenium.getValue("balance");
  123. myprint("Loan required is " + vLoan);
  124. return "TS004 is a Pass";
  125. }
  126. public String ts_005(){
  127. String vE, vI, vL;
  128. if(vTExp==Double.parseDouble(vExp)){
  129. myprint("Expenses MATCH");
  130. vE = "Pass";
  131. } else {
  132. myprint("Expenses DO NOT MATCH");
  133. vE = "Fail";
  134. }
  135. if(vTInc==Double.parseDouble(vInc)){
  136. myprint("Income MATCH");
  137. vI = "Pass";
  138. } else {
  139. myprint("Income DO NOT MATCH");
  140. vI = "Fail";
  141. }
  142. if(vTLoan==Double.parseDouble(vLoan)){
  143. myprint("Loan MATCH");
  144. vL = "Pass";
  145. } else {
  146. myprint("Loan DO NOT MATCH");
  147. vL = "Fail";
  148. }
  149. if (vE=="Pass" && vI=="Pass" && vL=="Pass"){
  150. return "Pass";
  151. } else {
  152. return "Fail";
  153. }
  154. }
  155. public void xlRead(String sPath) throws Exception{
  156. File myxl = new File(sPath);
  157. FileInputStream myStream = new FileInputStream(myxl);
  158. HSSFWorkbook myWB = new HSSFWorkbook(myStream);
  159. //HSSFSheet mySheet = new HSSFSheet(myWB);
  160. //HSSFSheet mySheet = myWB.getSheetAt(0); // Referring to 1st sheet
  161. HSSFSheet mySheet = myWB.getSheetAt(2); // Referring to 3rd sheet
  162. //int xRows = mySheet.getLastRowNum()+1;
  163. //int xCols = mySheet.getRow(0).getLastCellNum();
  164. xRows = mySheet.getLastRowNum()+1;
  165. xCols = mySheet.getRow(0).getLastCellNum();
  166. myprint("Rows are " + xRows);
  167. myprint("Cols are " + xCols);
  168. //String[][] xData = new String[xRows][xCols];
  169. xData = new String[xRows][xCols];
  170. for (int i = 0; i < xRows; i++) {
  171. HSSFRow row = mySheet.getRow(i);
  172. for (int j = 0; j < xCols; j++) {
  173. HSSFCell cell = row.getCell(j); // To read value from each col in each row
  174. String value = cellToString(cell);
  175. xData[i][j] = value;
  176. //System.out.print(value);
  177. //System.out.print("@@");
  178. }
  179. //System.out.println("");
  180. }
  181. }
  182. public static String cellToString(HSSFCell cell) {
  183. // This function will convert an object of type excel cell to a string value
  184. int type = cell.getCellType();
  185. Object result;
  186. switch (type) {
  187. case HSSFCell.CELL_TYPE_NUMERIC: //0
  188. result = cell.getNumericCellValue();
  189. break;
  190. case HSSFCell.CELL_TYPE_STRING: //1
  191. result = cell.getStringCellValue();
  192. break;
  193. case HSSFCell.CELL_TYPE_FORMULA: //2
  194. throw new RuntimeException("We can't evaluate formulas in Java");
  195. case HSSFCell.CELL_TYPE_BLANK: //3
  196. result = "-";
  197. break;
  198. case HSSFCell.CELL_TYPE_BOOLEAN: //4
  199. result = cell.getBooleanCellValue();
  200. break;
  201. case HSSFCell.CELL_TYPE_ERROR: //5
  202. throw new RuntimeException ("This cell has an error");
  203. default:
  204. throw new RuntimeException("We don't support this cell type: " + type);
  205. }
  206. return result.toString();
  207. }
  208. public void xlwrite(String xlPath, String[][] xldata) throws Exception {
  209. System.out.println("Inside XL Write");
  210. File outFile = new File(xlPath);
  211. HSSFWorkbook wb = new HSSFWorkbook();
  212. // Make a worksheet in the XL document created
  213. /*HSSFSheet osheet = wb.setSheetName(1,"TEST");*/
  214. HSSFSheet osheet = wb.createSheet("TESTRESULTS");
  215. // Create row at index zero ( Top Row)
  216. for (int myrow = 0; myrow < xRows; myrow++) {
  217. //System.out.println("Inside XL Write");
  218. HSSFRow row = osheet.createRow(myrow);
  219. // Create a cell at index zero ( Top Left)
  220. for (int mycol = 0; mycol < xCols; mycol++) {
  221. HSSFCell cell = row.createCell(mycol);
  222. // Lets make the cell a string type
  223. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  224. // Type some content
  225. cell.setCellValue(xldata[myrow][mycol]);
  226. //System.out.print("..." + xldata[myrow][mycol]);
  227. }
  228. //System.out.println("..................");
  229. // The Output file is where the xls will be created
  230. FileOutputStream fOut = new FileOutputStream(outFile);
  231. // Write the XL sheet
  232. wb.write(fOut);
  233. fOut.flush();
  234. // // Done Deal..
  235. fOut.close();
  236. }
  237. }
  238. }