Excel Utility Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
package utility;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
private static XSSFRow Row;
//This method is to set the File path and to open the Excel file,
//Pass Excel Path and Sheetname as Arguments to this method public static void setExcelFile(String Path,String SheetName) throws Exception { try { // Open the Excel file FileInputStream ExcelFile = new FileInputStream(Path); // Access the required test data sheet ExcelWBook = new XSSFWorkbook(ExcelFile); ExcelWSheet = ExcelWBook.getSheet(SheetName); Log.info("Excel sheet opened"); } catch (Exception e){ throw (e); } } //This method is to read the test data from the Excel cell, in //this we are passing parameters as Row num and Col num public static String getCellData(int RowNum, int ColNum) throws Exception{ try{ Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum); String CellData = Cell.getStringCellValue(); return CellData; }catch (Exception e){ return""; } } //This method is to write in the Excel cell, Row num and Col num //are the parameters @SuppressWarnings("static-access") public static void setCellData(String Result, int RowNum, int ColNum) throws Exception { try{ Row = ExcelWSheet.getRow(RowNum); Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL); if (Cell == null) { Cell = Row.createCell(ColNum); Cell.setCellValue(Result); } else { Cell.setCellValue(Result); } // Constant variables Test Data path and Test Data file name FileOutputStream fileOut = new FileOutputStream(Constant.Path_TestData + Constant.File_TestData); ExcelWBook.write(fileOut); fileOut.flush(); fileOut.close(); }catch(Exception e){ throw (e); } } public static int getRowContains(String sTestCaseName, int colNum) throws Exception{ int i; try { int rowCount = ExcelUtils.getRowUsed(); for ( i=0 ; i<rowCount; i++){ if(ExcelUtils.getCellData(i,colNum).equalsIgnoreCase(sTestCaseName)){ break; } } return i; }catch (Exception e){ Log.error("Class ExcelUtil | Method getRowContains | Exception desc : " + e.getMessage()); throw(e); } } public static int getRowUsed() throws Exception { try{ int RowCount = ExcelWSheet.getLastRowNum(); Log.info("Total number of Row used return as < " + RowCount + " >."); return RowCount; }catch (Exception e){ Log.error("Class ExcelUtil | Method getRowUsed | Exception desc : "+e.getMessage()); System.out.println(e.getMessage()); throw (e); } } } |
Log Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package utility;
import org.apache.log4j.Logger;
public class Log {
// Initialize Log4j logs
private static Logger Log = Logger.getLogger(Log.class.getName());
// This is to print log for the beginning of the test case, as we usually run
//so many test cases as a test suite public static void startTestCase(String sTestCaseName){ Log.info("**************************************************"+ "**************************************"); Log.info("*****************************************"+ "***********************************************"); Log.info("$$$$$$$$$$$$$$$$$$$$$ "+sTestCaseName+ " $$$$$$$$$$$$$$$$$$$$$$$$$"); Log.info("*******************************************"+ "*********************************************"); Log.info("********************************************"+ "********************************************"); } //This is to print log for the ending of the test case public static void endTestCase(String sTestCaseName){ Log.info("XXXXXXXXXXXXXXXXXXXXXXX "+"-E---N---D-"+ " XXXXXXXXXXXXXXXXXXXXXX"); Log.info("X"); Log.info("X"); Log.info("X"); Log.info("X"); } // Need to create these methods, so that they can be called public static void info(String message) { Log.info(message); } public static void warn(String message) { Log.warn(message); } public static void error(String message) { Log.error(message); } public static void fatal(String message) { Log.fatal(message); } public static void debug(String message) { Log.debug(message); } } |
Utility Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
package utility;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Utils {
public static WebDriver driver = null;
public static WebDriver OpenBrowser(int iTestCaseRow) throws Exception{
String sBrowserName;
try{
sBrowserName = ExcelUtils.getCellData(iTestCaseRow, Constant.Col_Browser);
if(sBrowserName.equals("Mozilla")){
driver = new FirefoxDriver();
Log.info("New driver instantiated");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Log.info("Implicit wait applied on the driver for 10 seconds");
driver.get(Constant.URL);
Log.info("Web application launched successfully");
}
}catch (Exception e){
Log.error("Class Utils | Method OpenBrowser | Exception desc : "
+e.getMessage()); } return driver; } public static String getTestCaseName(String sTestCase)throws Exception{ String value = sTestCase; try{ int posi = value.indexOf("@"); value = value.substring(0, posi); posi = value.lastIndexOf("."); value = value.substring(posi + 1); return value; }catch (Exception e){ Log.error("Class Utils | Method getTestCaseName | Exception desc : " +e.getMessage()); throw (e); } } public static void mouseHoverAction(WebElement mainElement, String subElement){ Actions action = new Actions(driver); action.moveToElement(mainElement).perform(); if(subElement.equals("Accessories")){ action.moveToElement(driver.findElement(By.linkText("Accessories"))); Log.info("Accessories link is found under Product Category"); } if(subElement.equals("iMacs")){ action.moveToElement(driver.findElement(By.linkText("iMacs"))); Log.info("iMacs link is found under Product Category"); } if(subElement.equals("iPads")){ action.moveToElement(driver.findElement(By.linkText("iPads"))); Log.info("iPads link is found under Product Category"); } if(subElement.equals("iPhones")){ action.moveToElement(driver.findElement(By.linkText("iPhones"))); Log.info("iPhones link is found under Product Category"); } action.click(); action.perform(); Log.info("Click action is performed on the selected Product Type"); } public static void waitForElement(WebElement element){ WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.elementToBeClickable(element)); } public static void takeScreenshot(WebDriver driver, String sTestCaseName) throws Exception{ try{ File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File(Constant.Path_ScreenShot + sTestCaseName +".jpg")); } catch (Exception e){ Log.error("Class Utils | Method takeScreenshot | Exception occured while capturing ScreenShot : "+e.getMessage()); throw new Exception(); } } } |
Page Object Package
Base Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package pageObjects;
import org.openqa.selenium.WebDriver;
public class BaseClass {
public static WebDriver driver;
public static boolean bResult;
public BaseClass(WebDriver driver){
BaseClass.driver = driver;
BaseClass.bResult = true;
}
}
|
Home Page Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import utility.Log;
import utility.Utils;
public class Home_Page extends BaseClass{
private static WebElement element = null;
public Home_Page(WebDriver driver){
super(driver);
}
public static WebElement lnk_MyAccount() throws Exception{
try{
element = driver.findElement(By.xpath(".//*[@id='account']/a"));
Log.info("My Account link is found on the Home Page");
}catch (Exception e){
Log.error("My Acocunt link is not found on the Home Page");
throw(e);
}
return element;
}
public static WebElement lnk_LogOut() throws Exception{
try{
element = driver.findElement(By.id("account_logout"));
Log.info("Log Out link is found on the Home Page");
}catch (Exception e){
Log.error("Log Out link is not found on the Home Page");
throw(e);
}
return element;
}
public static class TopNavigation{
public static class Product_Type{
static WebElement mainElement;
public static void Accessories() throws Exception{
try{
mainElement = driver.findElement(By.linkText("Product Category"));
Log.info("Product category link is found under Top Navigation");
Utils.mouseHoverAction(mainElement, "Accessories");
}catch (Exception e){
Log.error("Accessories link is not found under Product Category");
throw(e);
}
}
public static void iMacs() throws Exception{
try{
mainElement = driver.findElement(By.linkText("Product Category"));
Log.info("Product category link is found under Top Navigation");
Utils.mouseHoverAction(mainElement, "iMacs");
}catch (Exception e){
Log.error("Accessories link is not found under Product Category");
throw(e);
}
}
public static void iPads() throws Exception{
try{
mainElement = driver.findElement(By.linkText("Product Category"));
Log.info("Product category link is found under Top Navigation");
Utils.mouseHoverAction(mainElement, "iPads");
}catch (Exception e){
Log.error("Accessories link is not found under Product Category");
throw(e);
}
}
public static void iPhones() throws Exception{
try{
mainElement = driver.findElement(By.linkText("Product Category"));
Log.info("Product category link is found under Top Navigation");
Utils.mouseHoverAction(mainElement, "iPhones");
}catch (Exception e){
Log.error("Accessories link is not found under Product Category");
throw(e);
}
}
}
}
}
|
No comments:
Post a Comment