PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/hybrid/Hybrid_Framework.java

https://github.com/satishija/Hybrid-Framework
Java | 437 lines | 380 code | 12 blank | 45 comment | 52 complexity | 98ccf2518e9afd156b6a3c56fb3d5070 MD5 | raw file
  1. package com.hybrid;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import org.apache.poi.hssf.usermodel.HSSFCell;
  6. import org.apache.poi.hssf.usermodel.HSSFRow;
  7. import org.apache.poi.hssf.usermodel.HSSFSheet;
  8. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  9. import org.junit.After;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import com.thoughtworks.selenium.*;
  13. public class Hybrid_Framework extends SeleneseTestCase{
  14. public String xTC[][], xTS[][], xTD[][]; // 2-D array to hold Test Cases and Steps. GLOBAL.
  15. public int xTC_rows, xTC_cols, xTS_rows, xTS_cols, xTD_rows, xTD_cols;
  16. public String vTDID, vExecute, vURL, vBrowser, vName, vUserName, vPswd, vEmail, vCity, vZip, vTechnology, vTitle;
  17. public String vKeyword, vIP1, vIP2;
  18. public String vResult, vError;
  19. public String xlPath;
  20. @Before
  21. public void setUp() throws Exception {
  22. xlPath = "C:/Selenium/Jul5/HF/Automation Plan.xls";
  23. System.out.println("@Before");
  24. xlR_TC(xlPath); // Read Test Cases
  25. xlR_TS(xlPath); // Read Test Steps
  26. xlR_TD(xlPath); // Read Test Data
  27. //selenium = new DefaultSelenium("localhost", 1236, "*iehta", "http://www.dice.com");
  28. selenium = new DefaultSelenium("localhost", 1236, "*chrome", "http://www.chasestudentloans.com/");
  29. selenium.start();
  30. selenium.setSpeed("1000"); // set sel speed of run
  31. selenium.windowMaximize(); // make browser max.
  32. selenium.windowFocus(); // focus on that window
  33. }
  34. @Test
  35. public void testBlock() throws Exception{
  36. String vTCID, vTSID;
  37. String vTC_flag;
  38. // Go to each Test Data row
  39. for (int k = 1; k < xTD_rows; k++) {
  40. // Test Data being initialized
  41. vTDID =xTD[k][0];
  42. vExecute =xTD[k][1];
  43. vURL = xTD[k][2];
  44. vBrowser =xTD[k][3];
  45. vName =xTD[k][4];
  46. vUserName =xTD[k][5];
  47. vPswd =xTD[k][6];
  48. vEmail =xTD[k][7];
  49. vCity =xTD[k][8];
  50. vZip =xTD[k][9];
  51. vTechnology =xTD[k][10];
  52. vTitle = xTD[k][11];
  53. if (vExecute.equals("Y")){
  54. // Go to each Test Case row
  55. for (int i = 1; i < xTC_rows; i++) {
  56. if(xTC[i][3].equals("Y")){ // Only if TC is set to Y
  57. vTCID = xTC[i][0]; // Get TCID
  58. System.out.println("Now executing TC# " + vTCID);
  59. vTC_flag = "Pass"; // default that TC has passed
  60. // Go to each Test Step row
  61. for (int j = 1; j < xTS_rows; j++) {
  62. vTSID = xTS[j][0]; // Get TSID
  63. if(vTCID.equals(vTSID)){ // Compare TC and TS ID's
  64. System.out.println("Now executing TS# " + xTS[j][2]);
  65. vKeyword = xTS[j][4];
  66. vIP1 = xTS[j][5];
  67. vIP2 = xTS[j][6];
  68. System.out.println("Keyword to execute is " + vKeyword);
  69. System.out.println("IP1 is " + vIP1 + " IP2 is " + vIP2);
  70. vResult = "Pass";
  71. vError = "None";
  72. vResult = keyword_executor(); // Main execution
  73. xTS[j][7] = vResult;
  74. xTS[j][8] = vError;
  75. if (vResult.equals("Fail")){
  76. vTC_flag = "Fail";
  77. selenium.captureScreenshot("C:/Selenium/Jul5/HF/Result/err_img_td_"+k+"_tc_"+i+"_ts_"+xTS[j][2]+".jpg"); // to a specific jpg file
  78. }
  79. }
  80. }
  81. xTC[i][4] = vTC_flag;
  82. }
  83. }
  84. }
  85. String xlPath_tcres = "C:/Selenium/Jul5/HF/Result/TC_Res_"+vTDID+"_.xls";
  86. String xlPath_tsres = "C:/Selenium/Jul5/HF/Result/TS_Res_"+vTDID+"_.xls";
  87. System.out.println("@After writing results into excel");
  88. xlwrite_tc(xlPath_tcres, xTC);
  89. xlwrite_ts(xlPath_tsres, xTS);
  90. }
  91. }
  92. @After
  93. public void tearDown() throws Exception{
  94. //public void afterBlock(){
  95. System.out.println("All done");
  96. selenium.stop();
  97. }
  98. public String keyword_executor() throws Exception{
  99. // Call the corresponding keyword to execute
  100. String vElse;
  101. vElse = "True";
  102. try { // Java, try the below code, if it fails, catch the error.
  103. get_IP1_IP2();
  104. if(vKeyword.equals("sOpen")){
  105. vElse = "False";
  106. return sOpen(vIP1, vIP2);
  107. }
  108. if(vKeyword.equals("sType")){
  109. sType(vIP1, vIP2);
  110. vElse = "False";
  111. return "Pass";
  112. }
  113. if(vKeyword.equals("sVerifyElementPresent")){
  114. vElse = "False";
  115. return sVerifyElementPresent(vIP1);
  116. }
  117. if(vKeyword.equals("sGetAndCompareText")){
  118. vElse = "False";
  119. return sGetAndCompareText(vIP1, vIP2);
  120. }
  121. if(vKeyword.equals("sGetValueAndCompare")){
  122. vElse = "False";
  123. return sGetValueAndCompare(vIP1, vIP2);
  124. }
  125. if(vKeyword.equals("sClickAndWait")){
  126. sClickAndWait(vIP1, vIP2);
  127. vElse = "False";
  128. return "Pass";
  129. }
  130. if(vKeyword.equals("sVerifyTextAny")){
  131. vElse = "False";
  132. return sVerifyTextAny(vIP1);
  133. }
  134. if (vElse.equals("True")){
  135. System.out.println("Keyword is missing" + vKeyword);
  136. return "False-Keyword Missing";
  137. }
  138. }
  139. catch (SeleniumException myerror){
  140. //System.out.println("Error occurred when executing keyword " + vKeyword);
  141. //System.out.println("Error is : " + myerror);
  142. vError = String.valueOf(myerror);
  143. return "Fail";
  144. }
  145. return "UNKNOWN ISSUE";
  146. }
  147. public void get_IP1_IP2(){
  148. // Assign test data variables to the inputs
  149. if (vIP1.equals("vURL")){
  150. vIP1 = vURL;
  151. }
  152. if (vIP1.equals("vUserName")){
  153. vIP1 = vUserName;
  154. }
  155. if (vIP1.equals("vPswd")){
  156. vIP1 = vPswd;
  157. }
  158. if (vIP1.equals("vCity")){
  159. vIP1 = vCity;
  160. }
  161. if (vIP1.equals("vZip")){
  162. vIP1 = vZip;
  163. }
  164. if (vIP1.equals("vTechnology")){
  165. vIP1 = vTechnology;
  166. }
  167. if (vIP1.equals("vTitle")){
  168. vIP1 = vTitle;
  169. }
  170. if (vIP2.equals("vURL")){
  171. vIP2 = vURL;
  172. }
  173. if (vIP2.equals("vUserName")){
  174. vIP2 = vUserName;
  175. }
  176. if (vIP2.equals("vPswd")){
  177. vIP2 = vPswd;
  178. }
  179. if (vIP2.equals("vCity")){
  180. vIP2 = vCity;
  181. }
  182. if (vIP2.equals("vZip")){
  183. vIP2 = vZip;
  184. }
  185. if (vIP2.equals("vTechnology")){
  186. vIP2 = vTechnology;
  187. }
  188. if (vIP2.equals("vTitle")){
  189. vIP2 = vTitle;
  190. }
  191. }
  192. public String sGetAndCompareText(String eID, String vStr1){
  193. String vStr2 = sGetText(eID);
  194. if (vStr1.equals(vStr2)){
  195. return "Pass";
  196. } else {
  197. return "Fail";
  198. }
  199. }
  200. public String sGetValueAndCompare(String eID, String vStr1){
  201. String vStr2 = selenium.getValue(eID);
  202. if (vStr1.equals(vStr2)){
  203. return "Pass";
  204. } else {
  205. return "Fail";
  206. }
  207. }
  208. public String sVerifyElementPresent(String eID){
  209. // Purpose: Verifies if an element is present
  210. // I/P : ID
  211. // O/P : pass or fail
  212. if (selenium.isElementPresent(eID)){
  213. return "Pass";
  214. } else {
  215. return "Fail";
  216. }
  217. }
  218. public String sOpen(String vURL, String vTitle){
  219. // Purpose: Navigates the browser to a URL. For root we use "/"
  220. // I/P : Destination URL
  221. // O/P : nothing
  222. selenium.open(vURL);
  223. if (selenium.getTitle().equals(vTitle)) {
  224. return "Pass";
  225. } else {
  226. return "Fail";
  227. }
  228. }
  229. public void sType(String eID, String sText){
  230. // Purpose: Puts a text in the edit box
  231. // I/P : Ele id and the text
  232. // O/P : nothing
  233. selenium.type(eID, sText);
  234. }
  235. public void sSelect(String eID, String sText){
  236. // Purpose: Selects an item from a drop down list
  237. // I/P : Ele id and the text
  238. // O/P : nothing
  239. //selenium.type(eID, "label="+sText);
  240. selenium.select(eID, sText);
  241. }
  242. public void sClick(String eID){
  243. // Purpose: click on Link, button, text, image ... doesnt matter
  244. // I/P : Ele id
  245. // O/P : nothing
  246. selenium.click(eID);
  247. }
  248. public void sWait(String eTime){
  249. // Purpose: wait for n secs
  250. // I/P : Amount of time in ms
  251. // O/P : nothing
  252. selenium.waitForPageToLoad(eTime);
  253. }
  254. public void sClickAndWait(String eID, String eTime){
  255. // Purpose: click and wait on Link, button, text, image ... doesnt matter
  256. // I/P : Ele id and time
  257. // O/P : nothing
  258. sClick(eID);
  259. sWait(eTime);
  260. }
  261. public String sGetText(String eID){
  262. // Purpose: get a text
  263. // I/P : eID
  264. // O/P : nothing
  265. return selenium.getText(eID);
  266. }
  267. public String sGetValue(String eID){
  268. // Purpose: gets the values inside elements
  269. // I/P : eID
  270. // O/P : nothing
  271. return selenium.getValue(eID);
  272. }
  273. public String sVerifyText(String eID, String sText){
  274. // Purpose: gets the values inside elements
  275. // I/P : eID
  276. // O/P : nothing
  277. if (selenium.getText(eID).equals(sText)) {
  278. return "Pass";
  279. } else {
  280. return "Fail";
  281. }
  282. }
  283. public String sVerifyTextAny(String sText){
  284. // Purpose: gets the values inside elements
  285. // I/P : eID
  286. // O/P : nothing
  287. if (selenium.isTextPresent(sText)) {
  288. return "Pass";
  289. } else {
  290. return "Fail";
  291. }
  292. }
  293. public void myprint(String mymessage){
  294. System.out.println(mymessage);
  295. System.out.println("~~~~~~~~~~~~~");
  296. }
  297. public void xlR_TC(String sPath) throws Exception{
  298. File myxl = new File(sPath);
  299. FileInputStream myStream = new FileInputStream(myxl);
  300. HSSFWorkbook myWB = new HSSFWorkbook(myStream);
  301. HSSFSheet mySheet = myWB.getSheetAt(1); // Referring to 2nd sheet
  302. xTC_rows = mySheet.getLastRowNum()+1;
  303. xTC_cols = mySheet.getRow(0).getLastCellNum();
  304. myprint("Rows are " + xTC_rows);
  305. myprint("Cols are " + xTC_cols);
  306. xTC = new String[xTC_rows][xTC_cols]; // Giving the size of the array
  307. for (int i = 0; i < xTC_rows; i++) {
  308. HSSFRow row = mySheet.getRow(i);
  309. for (int j = 0; j < xTC_cols; j++) {
  310. HSSFCell cell = row.getCell(j); // To read value from each col in each row
  311. String value = cellToString(cell);
  312. xTC[i][j] = value;
  313. System.out.print(value);
  314. System.out.print("@@");
  315. }
  316. System.out.println("");
  317. }
  318. }
  319. public void xlR_TS(String sPath) throws Exception{
  320. File myxl = new File(sPath);
  321. FileInputStream myStream = new FileInputStream(myxl);
  322. HSSFWorkbook myWB = new HSSFWorkbook(myStream);
  323. HSSFSheet mySheet = myWB.getSheetAt(2); // Referring to 3rd sheet
  324. xTS_rows = mySheet.getLastRowNum()+1;
  325. xTS_cols = mySheet.getRow(0).getLastCellNum();
  326. myprint("TS Rows are " + xTS_rows);
  327. myprint("TS Cols are " + xTS_cols);
  328. xTS = new String[xTS_rows][xTS_cols]; // Giving the size of the array
  329. for (int i = 0; i < xTS_rows; i++) {
  330. HSSFRow row = mySheet.getRow(i);
  331. for (int j = 0; j < xTS_cols; j++) {
  332. HSSFCell cell = row.getCell(j); // To read value from each col in each row
  333. String value = cellToString(cell);
  334. xTS[i][j] = value;
  335. System.out.print(value);
  336. System.out.print("@@");
  337. }
  338. System.out.println("");
  339. }
  340. }
  341. public void xlR_TD(String sPath) throws Exception{
  342. File myxl = new File(sPath);
  343. FileInputStream myStream = new FileInputStream(myxl);
  344. HSSFWorkbook myWB = new HSSFWorkbook(myStream);
  345. HSSFSheet mySheet = myWB.getSheetAt(3); // Referring to 2nd sheet
  346. xTD_rows = mySheet.getLastRowNum()+1;
  347. xTD_cols = mySheet.getRow(0).getLastCellNum();
  348. myprint("Rows are " + xTD_rows);
  349. myprint("Cols are " + xTD_cols);
  350. xTD = new String[xTD_rows][xTD_cols]; // Giving the size of the array
  351. for (int i = 0; i < xTD_rows; i++) {
  352. HSSFRow row = mySheet.getRow(i);
  353. for (int j = 0; j < xTD_cols; j++) {
  354. HSSFCell cell = row.getCell(j); // To read value from each col in each row
  355. String value = cellToString(cell);
  356. xTD[i][j] = value;
  357. System.out.print(value);
  358. System.out.print("@@");
  359. }
  360. System.out.println("");
  361. }
  362. }
  363. public static String cellToString(HSSFCell cell) {
  364. // This function will convert an object of type excel cell to a string value
  365. int type = cell.getCellType();
  366. Object result;
  367. switch (type) {
  368. case HSSFCell.CELL_TYPE_NUMERIC: //0
  369. result = cell.getNumericCellValue();
  370. break;
  371. case HSSFCell.CELL_TYPE_STRING: //1
  372. result = cell.getStringCellValue();
  373. break;
  374. case HSSFCell.CELL_TYPE_FORMULA: //2
  375. throw new RuntimeException("We can't evaluate formulas in Java");
  376. case HSSFCell.CELL_TYPE_BLANK: //3
  377. result = "-";
  378. break;
  379. case HSSFCell.CELL_TYPE_BOOLEAN: //4
  380. result = cell.getBooleanCellValue();
  381. break;
  382. case HSSFCell.CELL_TYPE_ERROR: //5
  383. throw new RuntimeException ("This cell has an error");
  384. default:
  385. throw new RuntimeException("We don't support this cell type: " + type);
  386. }
  387. return result.toString();
  388. }
  389. public void xlwrite_tc(String xlPath, String[][] xldata) throws Exception {
  390. System.out.println("Inside XL Write for Test Case");
  391. File outFile = new File(xlPath);
  392. HSSFWorkbook wb = new HSSFWorkbook();
  393. HSSFSheet osheet = wb.createSheet("TestCasesResults");
  394. for (int myrow = 0; myrow < xTC_rows; myrow++) {
  395. HSSFRow row = osheet.createRow(myrow);
  396. for (int mycol = 0; mycol < xTC_cols; mycol++) {
  397. HSSFCell cell = row.createCell(mycol);
  398. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  399. cell.setCellValue(xldata[myrow][mycol]);
  400. }
  401. FileOutputStream fOut = new FileOutputStream(outFile);
  402. wb.write(fOut);
  403. fOut.flush();
  404. fOut.close();
  405. }
  406. }
  407. public void xlwrite_ts(String xlPath, String[][] xldata) throws Exception {
  408. System.out.println("Inside XL Write for Test Step");
  409. File outFile = new File(xlPath);
  410. HSSFWorkbook wb = new HSSFWorkbook();
  411. HSSFSheet osheet = wb.createSheet("TestStepsResults");
  412. for (int myrow = 0; myrow < xTS_rows; myrow++) {
  413. HSSFRow row = osheet.createRow(myrow);
  414. for (int mycol = 0; mycol < xTS_cols; mycol++) {
  415. HSSFCell cell = row.createCell(mycol);
  416. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  417. cell.setCellValue(xldata[myrow][mycol]);
  418. }
  419. FileOutputStream fOut = new FileOutputStream(outFile);
  420. wb.write(fOut);
  421. fOut.flush();
  422. fOut.close();
  423. }
  424. }
  425. }