PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/hybrid/Hybrid_Framework_WebDriver.java

https://github.com/satishija/Hybrid-Framework
Java | 432 lines | 380 code | 43 blank | 9 comment | 61 complexity | b9a0a9cf93f4f1526915cc708570f503 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.openqa.selenium.By;
  10. import org.openqa.selenium.NoSuchElementException;
  11. import org.openqa.selenium.WebDriver;
  12. import org.openqa.selenium.firefox.FirefoxDriver;
  13. import org.openqa.selenium.htmlunit.HtmlUnitDriver;
  14. import org.openqa.selenium.ie.InternetExplorerDriver;
  15. import com.thoughtworks.selenium.SeleniumException;
  16. public class Hybrid_Framework_WebDriver {
  17. // Declared global variables can be accessed from any method in the class.
  18. public static int xTCRows, xTCCols;
  19. public static int xTDRows, xTDCols;
  20. public static String[][] xTCdata;
  21. public static String[][] xTDdata;
  22. public static int xTSRows, xTSCols;
  23. public static String[][] xTSdata;
  24. public static String vBrowser, vURL, vText, vGetText, vAttribute, vEmail, vPswd;
  25. public static String vTechnology, vCity, vZIP;
  26. public static String vKeyword, vIP1, vIP2, vIP3;
  27. public static long vSleep;
  28. public static WebDriver myD = new HtmlUnitDriver();
  29. public static void main(String[] args) throws Exception {
  30. // My variables or Test Datas
  31. String eXPath;
  32. String xlPath, xlTC_res, xlTS_res;
  33. String xlTC_res_temp, xlTS_res_temp;
  34. String vTS_res, vTC_res, vTD_res, vTS_error ;
  35. String vT_flag;
  36. // Read the Test Steps XL sheet.
  37. xlPath = "C:/Selenium/Aug18/KDF/Automation Plan 1.1.xls";
  38. xlTC_res= "C:/Selenium/Aug18/KDF/Results_TC";
  39. xlTS_res= "C:/Selenium/Aug18/KDF/Results_TS";
  40. xlTSRead(xlPath);
  41. xlTCRead(xlPath);
  42. xlTDRead(xlPath);
  43. // Intializing my data and objects
  44. vAttribute = "value";
  45. vSleep = 2000L;
  46. for (int k=1;k<xTDRows; k++){ // Go to each Test data row
  47. if (xTDdata[k][1].equals("Y")) { // check if the execute flag is Y
  48. // Then load the variables
  49. vBrowser = xTDdata[k][3];
  50. vURL = xTDdata[k][2];
  51. vEmail = xTDdata[k][7];
  52. vPswd = xTDdata[k][6];
  53. vTechnology = xTDdata[k][10];
  54. vCity = xTDdata[k][8];
  55. vZIP = xTDdata[k][9];
  56. for (int j = 1; j<xTCRows; j++) { //Go to each row in the TC Sheet and see if the execute flag is Y
  57. if (xTCdata[j][4].equals("Y")) { // If y then go to each row in TS Sheet
  58. vTC_res = "Pass";
  59. for (int i = 1; i <xTSRows; i++){
  60. if (xTCdata[j][1].equals(xTSdata[i][0])){ // and see if TCID's match
  61. System.out.print(xTSdata[i][0]);
  62. vKeyword = xTSdata[i][4];
  63. vIP1 = xTSdata[i][5];
  64. vIP2 = xTSdata[i][6];
  65. vIP3 = xTSdata[i][7];
  66. System.out.println("---" + vKeyword + "````" + vIP1 + "````" + vIP2 + "````" + vIP3);
  67. // Identify the variable names and assign corresponding values
  68. getIP();
  69. // Identify the keyword and execute the corresponding function
  70. vTS_res = "Pass";
  71. vTS_error = "No Error";
  72. vT_flag = "Pass";
  73. try {
  74. vT_flag = keyword_executor();
  75. if (vT_flag.equals("Fail")) {
  76. vTS_res = "Fail";
  77. vTS_error = "Element check failed";
  78. }
  79. }
  80. catch (NoSuchElementException e) {
  81. vTS_res = "Fail";
  82. vTS_error = "Element missing " + e;
  83. if (vT_flag.equals("Fail")) {
  84. vTS_res = "Fail";
  85. vTS_error = "Element check failed";
  86. }
  87. }
  88. xTSdata[i][8] = vTS_res; //Take the TS result back to the XL array
  89. xTSdata[i][9] = vTS_error;
  90. if (vTS_res.equals("Fail")) { // Set TC to fail if any Test Step fails.
  91. vTC_res = "Fail";
  92. }
  93. }
  94. }
  95. xTCdata[j][4] = vTC_res;
  96. }
  97. }
  98. xlTC_res_temp = xlTC_res + xTDdata[k][0] + ".xls";
  99. xlTS_res_temp = xlTS_res + xTDdata[k][0] + ".xls";
  100. xlwrite_tc(xlTC_res_temp, xTCdata);
  101. xlwrite_ts(xlTS_res_temp, xTSdata);
  102. }
  103. }
  104. System.out.println("Test Run Complete");
  105. }
  106. public static void navigate_to(WebDriver mD, String fURL) {
  107. mD.navigate().to(fURL);
  108. }
  109. public static void send_keys(WebDriver mD, String fxPath, String fText) {
  110. mD.findElement(By.xpath(fxPath)).sendKeys(fText);
  111. }
  112. public static void click_element(WebDriver mD, String fxPath) {
  113. mD.findElement(By.xpath(fxPath)).click();
  114. }
  115. public static void click_link(WebDriver mD, String fText) {
  116. mD.findElement(By.linkText(fText)).click();
  117. }
  118. public static String get_text(WebDriver mD, String fxPath) {
  119. return mD.findElement(By.xpath(fxPath)).getText();
  120. }
  121. public static String verify_text(WebDriver mD, String fxPath, String fText) {
  122. String fTextOut = mD.findElement(By.xpath(fxPath)).getText();
  123. if (fTextOut.equals(fText)){
  124. return "Pass";
  125. } else {
  126. return "Fail";
  127. }
  128. }
  129. public static String element_present(WebDriver mD, String fxPath){
  130. if (mD.findElement(By.xpath(fxPath)).isDisplayed()){
  131. return "Pass";
  132. } else {
  133. return "Fail";
  134. }
  135. }
  136. public static String link_present(WebDriver mD, String fText){
  137. if (mD.findElement(By.linkText(fText)).isDisplayed()){
  138. return "Pass";
  139. } else {
  140. return "Fail";
  141. }
  142. }
  143. public static String get_attribute(WebDriver mD, String fxPath, String fAttribute) {
  144. return mD.findElement(By.xpath(fxPath)).getAttribute(fAttribute);
  145. }
  146. public static String verify_attribute(WebDriver mD, String fxPath, String fAttribute, String fText) {
  147. String fTextOut = mD.findElement(By.xpath(fxPath)).getAttribute(fAttribute);
  148. if (fTextOut.equals(fText)){
  149. return "Pass";
  150. } else {
  151. return "Fail";
  152. }
  153. }
  154. public static void close_browser(WebDriver mD){
  155. mD.close();
  156. }
  157. public static void wait_time(Long fWait) throws Exception{
  158. Thread.sleep(fWait);
  159. }
  160. public static void xlTSRead(String sPath) throws Exception{
  161. File myxl = new File(sPath);
  162. FileInputStream myStream = new FileInputStream(myxl);
  163. HSSFWorkbook myWB = new HSSFWorkbook(myStream);
  164. HSSFSheet mySheet = myWB.getSheetAt(2); // Referring to 1st sheet
  165. xTSRows = mySheet.getLastRowNum()+1;
  166. xTSCols = mySheet.getRow(0).getLastCellNum();
  167. System.out.println("Rows are " + xTSRows);
  168. System.out.println("Cols are " + xTSCols);
  169. xTSdata = new String[xTSRows][xTSCols];
  170. for (int i = 0; i < xTSRows; i++) {
  171. HSSFRow row = mySheet.getRow(i);
  172. for (int j = 0; j < xTSCols; j++) {
  173. HSSFCell cell = row.getCell(j); // To read value from each col in each row
  174. String value = cellToString(cell);
  175. xTSdata[i][j] = value;
  176. }
  177. }
  178. }
  179. public static void xlTCRead(String sPath) throws Exception{
  180. File myxl = new File(sPath);
  181. FileInputStream myStream = new FileInputStream(myxl);
  182. HSSFWorkbook myWB = new HSSFWorkbook(myStream);
  183. HSSFSheet mySheet = myWB.getSheetAt(1); // Referring to 1st sheet
  184. xTCRows = mySheet.getLastRowNum()+1;
  185. xTCCols = mySheet.getRow(0).getLastCellNum();
  186. System.out.println("Rows are " + xTCRows);
  187. System.out.println("Cols are " + xTCCols);
  188. xTCdata = new String[xTCRows][xTCCols];
  189. for (int i = 0; i < xTCRows; i++) {
  190. HSSFRow row = mySheet.getRow(i);
  191. for (int j = 0; j < xTCCols; j++) {
  192. HSSFCell cell = row.getCell(j); // To read value from each col in each row
  193. String value = cellToString(cell);
  194. xTCdata[i][j] = value;
  195. }
  196. }
  197. }
  198. public static void xlTDRead(String sPath) throws Exception{
  199. File myxl = new File(sPath);
  200. FileInputStream myStream = new FileInputStream(myxl);
  201. HSSFWorkbook myWB = new HSSFWorkbook(myStream);
  202. HSSFSheet mySheet = myWB.getSheetAt(3); // Referring to 4th sheet
  203. xTDRows = mySheet.getLastRowNum()+1;
  204. xTDCols = mySheet.getRow(0).getLastCellNum();
  205. System.out.println("Rows are " + xTDRows);
  206. System.out.println("Cols are " + xTDCols);
  207. xTDdata = new String[xTDRows][xTDCols];
  208. for (int i = 0; i < xTDRows; i++) {
  209. HSSFRow row = mySheet.getRow(i);
  210. for (int j = 0; j < xTDCols; j++) {
  211. HSSFCell cell = row.getCell(j); // To read value from each col in each row
  212. String value = cellToString(cell);
  213. xTDdata[i][j] = value;
  214. }
  215. }
  216. }
  217. public static String cellToString(HSSFCell cell) {
  218. // This function will convert an object of type excel cell to a string value
  219. int type = cell.getCellType();
  220. Object result;
  221. switch (type) {
  222. case HSSFCell.CELL_TYPE_NUMERIC: //0
  223. result = cell.getNumericCellValue();
  224. break;
  225. case HSSFCell.CELL_TYPE_STRING: //1
  226. result = cell.getStringCellValue();
  227. break;
  228. case HSSFCell.CELL_TYPE_FORMULA: //2
  229. throw new RuntimeException("We can't evaluate formulas in Java");
  230. case HSSFCell.CELL_TYPE_BLANK: //3
  231. result = "-";
  232. break;
  233. case HSSFCell.CELL_TYPE_BOOLEAN: //4
  234. result = cell.getBooleanCellValue();
  235. break;
  236. case HSSFCell.CELL_TYPE_ERROR: //5
  237. throw new RuntimeException ("This cell has an error");
  238. default:
  239. throw new RuntimeException("We don't support this cell type: " + type);
  240. }
  241. return result.toString();
  242. }
  243. public static void xlwrite_tc(String xlPath, String[][] xldata) throws Exception {
  244. System.out.println("Inside XL Write");
  245. File outFile = new File(xlPath);
  246. HSSFWorkbook wb = new HSSFWorkbook();
  247. HSSFSheet osheet = wb.createSheet("TestCases_Res");
  248. for (int myrow = 0; myrow < xTCRows; myrow++) {
  249. HSSFRow row = osheet.createRow(myrow);
  250. for (int mycol = 0; mycol < xTCCols; mycol++) {
  251. HSSFCell cell = row.createCell(mycol);
  252. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  253. cell.setCellValue(xldata[myrow][mycol]);
  254. }
  255. FileOutputStream fOut = new FileOutputStream(outFile);
  256. wb.write(fOut);
  257. fOut.flush();
  258. fOut.close();
  259. }
  260. }
  261. public static void xlwrite_ts(String xlPath, String[][] xldata) throws Exception {
  262. System.out.println("Inside XL Write");
  263. File outFile = new File(xlPath);
  264. HSSFWorkbook wb = new HSSFWorkbook();
  265. HSSFSheet osheet = wb.createSheet("TestSteps_Res");
  266. for (int myrow = 0; myrow < xTSRows; myrow++) {
  267. HSSFRow row = osheet.createRow(myrow);
  268. for (int mycol = 0; mycol < xTSCols; mycol++) {
  269. HSSFCell cell = row.createCell(mycol);
  270. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  271. cell.setCellValue(xldata[myrow][mycol]);
  272. }
  273. FileOutputStream fOut = new FileOutputStream(outFile);
  274. wb.write(fOut);
  275. fOut.flush();
  276. fOut.close();
  277. }
  278. }
  279. public static void getIP(){
  280. if (vIP1.equals("vURL")){
  281. vIP1 = vURL;
  282. }
  283. if (vIP1.equals("vTechnology")){
  284. vIP1 = vTechnology;
  285. }
  286. if (vIP1.equals("vZIP")){
  287. vIP1 = vZIP;
  288. }
  289. if (vIP1.equals("vCity")){
  290. vIP1 = vCity;
  291. }
  292. if (vIP1.equals("vEmail")){
  293. vIP1 = vEmail;
  294. }
  295. if (vIP1.equals("vPswd")){
  296. vIP1 = vPswd;
  297. }
  298. if (vIP2.equals("vURL")){
  299. vIP2 = vURL;
  300. }
  301. if (vIP2.equals("vTechnology")){
  302. vIP2 = vTechnology;
  303. }
  304. if (vIP2.equals("vZIP")){
  305. vIP2 = vZIP;
  306. }
  307. if (vIP2.equals("vCity")){
  308. vIP2 = vCity;
  309. }
  310. if (vIP2.equals("vEmail")){
  311. vIP2 = vEmail;
  312. }
  313. if (vIP2.equals("vPswd")){
  314. vIP2 = vPswd;
  315. }
  316. if (vIP3.equals("vURL")){
  317. vIP3 = vURL;
  318. }
  319. if (vIP3.equals("vTechnology")){
  320. vIP3 = vTechnology;
  321. }
  322. if (vIP3.equals("vZIP")){
  323. vIP3 = vZIP;
  324. }
  325. if (vIP3.equals("vCity")){
  326. vIP3 = vCity;
  327. }
  328. if (vIP3.equals("vEmail")){
  329. vIP3 = vEmail;
  330. }
  331. if (vIP3.equals("vPswd")){
  332. vIP3 = vPswd;
  333. }
  334. }
  335. public static String keyword_executor() throws Exception {
  336. String vtemp_return;
  337. vtemp_return = "Pass";
  338. if (vKeyword.equals("navigate_to")){
  339. // Initializing my Browser that we wish to work with
  340. if (vBrowser.equals("IE")) {
  341. myD = new InternetExplorerDriver();
  342. }
  343. if (vBrowser.equals("FF")) {
  344. myD = new FirefoxDriver();
  345. }
  346. if (vBrowser.equals("HTML")) {
  347. myD = new HtmlUnitDriver();
  348. }
  349. navigate_to(myD, vIP1);
  350. }
  351. if (vKeyword.equals("send_keys")){
  352. send_keys(myD, vIP1, vIP2);
  353. }
  354. if (vKeyword.equals("click_element")){
  355. click_element(myD, vIP1);
  356. }
  357. if (vKeyword.equals("click_link")){
  358. click_link(myD, vIP1);
  359. }
  360. if (vKeyword.equals("get_text")){
  361. get_text(myD, vIP1);
  362. }
  363. if (vKeyword.equals("verify_text")){
  364. System.out.println("Text present " + vIP2 + " is a " + verify_text(myD, vIP1, vIP2));
  365. vtemp_return = verify_text(myD, vIP1, vIP2);
  366. }
  367. if (vKeyword.equals("get_attribute")){
  368. get_attribute(myD, vIP1, vIP2);
  369. }
  370. if (vKeyword.equals("verify_attribute")){
  371. vtemp_return = verify_attribute(myD, vIP1, vIP2, vIP3);
  372. }
  373. if (vKeyword.equals("close_browser")){
  374. close_browser(myD);
  375. }
  376. if (vKeyword.equals("wait_time")){
  377. wait_time(vSleep);
  378. }
  379. if (vKeyword.equals("element_present")){
  380. System.out.println("Element " + vIP1 + " is " + element_present(myD, vIP1));
  381. vtemp_return = element_present(myD, vIP1);
  382. }
  383. if (vKeyword.equals("link_present")){
  384. System.out.println("Link " + vIP1 + " is " + link_present(myD, vIP1));
  385. vtemp_return = link_present(myD, vIP1);
  386. }
  387. return vtemp_return;
  388. }
  389. }