├── .gitignore ├── LICENSE.txt ├── Readme.md ├── TestCases2.xlsx ├── TestSuite2.xlsx ├── config.properties ├── pom.xml ├── src └── main │ ├── java │ ├── actions │ │ ├── CheckFileHash.java │ │ ├── FileDownloadTest.java │ │ ├── FileDownloader.java │ │ ├── HashType.java │ │ └── MethodType.java │ ├── model │ │ ├── CapturedObjectPropModel.java │ │ ├── MethodParameters.java │ │ └── TestCase.java │ ├── testng.xml │ └── util │ │ ├── ExcelAction.java │ │ ├── ExcelLibrary.java │ │ ├── ExecuteTestCases.java │ │ ├── Logger.java │ │ ├── MainTestNG.java │ │ ├── ReadConfigProperty.java │ │ ├── ReadElementLocators.java │ │ ├── ReadExcelSuite.java │ │ ├── Report.java │ │ ├── SwingTest.java │ │ ├── TestListener.java │ │ └── WebDriverClass.java │ └── resources │ └── config.properties └── test └── test.java /.gitignore: -------------------------------------------------------------------------------- 1 | /.project 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright [2015] [Infosys Limited] 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | Selenium OSTAF 2 | =============== 3 | Selenium Open Source Testing Automation Framework(OSTAF) is a framework which helps the tester in automating test execution of web applications without 4 | recording or writing the selenium scripts. 5 | 6 | How to build 7 | ------------- 8 | Unzip the selenium OSTAF zip to a folder on local machine 9 | Open Command Prompt as administrator 10 | Change directory to the folder containing the OSTAF code base 11 | run the command "maven install" and click enter Key in keyboard 12 | 13 | ---User need to navigate the command prompt to the project path and then need to type mvn install and enter the build will happen. 14 | 15 | 16 | How to use 17 | ----------- 18 | The SeleniumOSTAF-0.0.1-SNAPSHOT jar generated from the build can be placed anywhere in the system and config.properties file 19 | need to be in the same directory or folder where the jar is placed. 20 | 21 | Tester needs to be fill the configuration details like 22 | 1. application URL 23 | 2. Browser name 24 | 3. Path of supporting driverserver.exe file of browser 25 | 4. Path of the excel sheet which has the list of test cases to be executed 26 | 5. Path of the excel sheet which has the test step deatils for each test case. 27 | 28 | Tester needs to fill two excel sheets, 29 | 30 | 1. The TestSuite.xslx in which tester needs to specify the testcases names and execution status with either YES or NO. 31 | 32 | 2. The TestCase.xslx in which first sheet is "TestCase" will have columns which describe each test cases with details as 33 | i. test case name 34 | ii. TestStepId (which user can give any name to define the particular step in the testcase) 35 | iii.TestSteps which user has to give unique step ids like TC1,TC2 etc 36 | iv. Page where tester need to give the defined name for each page of web application corresponding to the web element in the step 37 | v. ObjectLocators is the one defines the property of the web element 38 | vi. ActionType is the operation which needs to be performed on the specified web element (which has to be one of the defined keywords) 39 | vii.Data in which user needs to specify the sheetname followed by "." and the coloumn name whose next row is the data to be given to the web element 40 | 41 | Next Sheet is the "CapturedObjectProperties" sheet where we define the properties of the webelement. The sheet has the columns with details as 42 | i. First coloumn is "Page" in which user can give any name to the page in which required web element is present 43 | ii. Second one is the "NAME" which is also user given name to webelement but this name and the ObjectLocator in the first sheet should be same 44 | iii. Third coloumn is "Property" which define how user is identifying the element like ID, XPATH,CSS and NAME 45 | iv. Fourth one is "Value" which is corresponding to the property. 46 | After defining all the webelements user has to specify NA in all coloumns to identify the end of the page. User can give any number of sheets to provide data in the first page. 47 | 48 | This framework is licensed under Apache License, Version 2.0 49 | 50 | 51 | -------------------------------------------------------------------------------- /TestCases2.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Infosys/Selenium-Testing-Automation-Framework/4d6669a122fc3da254eee4afc52fbb30c3991bb7/TestCases2.xlsx -------------------------------------------------------------------------------- /TestSuite2.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Infosys/Selenium-Testing-Automation-Framework/4d6669a122fc3da254eee4afc52fbb30c3991bb7/TestSuite2.xlsx -------------------------------------------------------------------------------- /config.properties: -------------------------------------------------------------------------------- 1 | TestCase_SheetName=TestCaseSheet 2 | TestSuite_SHEETNAME1=RegressionSuite 3 | 4 | #To be modified by the tester 5 | TestSuiteName=D:\\Kavya\\ICAP_ERP\\SeleniumTemp\\TestSuite2.xlsx 6 | BROWSER_NAME=IE 7 | SCREENSHOTS=D:\\Kavya\\ICAP_ERP\\SeleniumTemp\\SCREENSHOTS\\OnFailure 8 | IEDRIVER=D:\\Kavya\\ICAP_ERP\\SeleniumTemp\\IEDriverServer_Win32_2.45.0\\IEDriverServer.exe 9 | TestCasePath=D:\\Kavya\\ICAP_ERP\\SeleniumTemp\\TestCases2.xlsx 10 | URL=http://sparshv2/Pages/Home.aspx -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.infosys.toolsgroup 4 | SeleniumOSTAF 5 | 0.0.1-SNAPSHOT 6 | Open Source Test Automation Framework 1.0 7 | 8 | 9 | commons-codec 10 | commons-codec 11 | 1.9 12 | 13 | 14 | commons-logging 15 | commons-logging 16 | 1.1.3 17 | 18 | 19 | com.google.inject 20 | guice 21 | 3.0 22 | 23 | 24 | junit 25 | junit 26 | 4.11 27 | 28 | 29 | log4j 30 | log4j 31 | 1.2.17 32 | 33 | 34 | org.apache.poi 35 | poi 36 | 3.11 37 | 38 | 39 | org.apache.poi 40 | poi-examples 41 | 3.11 42 | 43 | 44 | org.apache.poi 45 | poi-ooxml 46 | 3.11 47 | 48 | 49 | org.apache.poi 50 | poi-ooxml-schemas 51 | 3.11 52 | 53 | 54 | org.apache.poi 55 | poi-scratchpad 56 | 3.11 57 | 58 | 59 | org.uncommons 60 | reportng 61 | 1.1.3 62 | 63 | 64 | net.sf.saxon 65 | saxon 66 | 8.7 67 | 68 | 69 | org.seleniumhq.selenium 70 | selenium-server 71 | 2.45.0 72 | 73 | 74 | org.testng 75 | testng 76 | 6.8 77 | 78 | 79 | velocity 80 | velocity-dep 81 | 1.4 82 | 83 | 84 | org.apache.xmlbeans 85 | xmlbeans 86 | 2.6.0 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | org.apache.maven.plugins 97 | maven-compiler-plugin 98 | 3.3 99 | 100 | UTF-8 101 | 1.7 102 | 1.7 103 | 104 | 105 | 106 | org.apache.maven.plugins 107 | maven-jar-plugin 108 | 2.4 109 | 110 | 111 | 112 | true 113 | main.java.util.MainTestNG 114 | 115 | 116 | 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /src/main/java/actions/CheckFileHash.java: -------------------------------------------------------------------------------- 1 | package main.java.actions; 2 | /** 3 | * The CheckFileHash class is used to perform the hash check operations on the 4 | * given file 5 | * 6 | * @author Shilpashree_V 7 | * @version 1.0 8 | * @since February 2015 9 | * 10 | */ 11 | 12 | import org.apache.commons.codec.digest.DigestUtils; 13 | import org.apache.log4j.Logger; 14 | 15 | import java.io.File; 16 | import java.io.FileInputStream; 17 | import java.io.FileNotFoundException; 18 | import java.io.IOException; 19 | 20 | public class CheckFileHash { 21 | 22 | private static final Logger LOG = Logger.getLogger(CheckFileHash.class); 23 | private HashType typeOfHash = null; 24 | private String expectedFileHash = null; 25 | private File fileToCheck = null; 26 | 27 | /** 28 | * The File to perform a Hash check upon 29 | * 30 | * @param fileToCheck 31 | * @throws FileNotFoundException 32 | */ 33 | public void fileToCheck(File fileToCheck) throws FileNotFoundException { 34 | if (!fileToCheck.exists()) 35 | throw new FileNotFoundException(fileToCheck + " does not exist!"); 36 | 37 | this.fileToCheck = fileToCheck; 38 | } 39 | 40 | /** 41 | * Hash details used to perform the Hash check 42 | * 43 | * @param hash 44 | * @param hashType 45 | */ 46 | public void hashDetails(String hash, HashType hashType) { 47 | this.expectedFileHash = hash; 48 | this.typeOfHash = hashType; 49 | } 50 | 51 | /** 52 | * Performs a expectedFileHash check on a File. 53 | * 54 | * @return 55 | * @throws IOException 56 | */ 57 | public boolean hasAValidHash() throws IOException { 58 | 59 | if (this.fileToCheck == null) 60 | throw new FileNotFoundException("File to check has not been set!"); 61 | if (this.expectedFileHash == null || this.typeOfHash == null) 62 | throw new NullPointerException("Hash details have not been set!"); 63 | 64 | String actualFileHash = ""; 65 | boolean isHashValid = false; 66 | 67 | switch (this.typeOfHash) { 68 | case MD5: 69 | actualFileHash = DigestUtils.md5Hex(new FileInputStream( 70 | this.fileToCheck)); 71 | if (this.expectedFileHash.equals(actualFileHash)) 72 | isHashValid = true; 73 | break; 74 | case SHA1: 75 | actualFileHash = DigestUtils.shaHex(new FileInputStream( 76 | this.fileToCheck)); 77 | if (this.expectedFileHash.equals(actualFileHash)) 78 | isHashValid = true; 79 | break; 80 | } 81 | 82 | 83 | 84 | return isHashValid; 85 | } 86 | 87 | } -------------------------------------------------------------------------------- /src/main/java/actions/FileDownloadTest.java: -------------------------------------------------------------------------------- 1 | package main.java.actions; 2 | /** 3 | * The FileDownloaderTest class is used to test the FileDownloader class 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.io.File; 11 | import org.junit.Assert; 12 | import org.junit.Test; 13 | import org.openqa.selenium.By; 14 | import org.openqa.selenium.WebDriver; 15 | import org.openqa.selenium.WebElement; 16 | import org.openqa.selenium.ie.InternetExplorerDriver; 17 | 18 | public class FileDownloadTest { 19 | 20 | @Test 21 | public void downloadAFile() throws Exception { 22 | System.setProperty( 23 | "webdriver.ie.driver", 24 | "D:\\Softwares\\Selenium_Materials\\Selenium_Materials\\IEDriverServer_Win32_2.33.0\\IEDriverServer.exe"); 25 | WebDriver driver; 26 | driver = new InternetExplorerDriver(); 27 | 28 | driver.get("http://iscls1apps/INFYDIR/"); 29 | driver.findElement(By.id("_ctl0_ContentPlaceHolder1_txtSearch")) 30 | .sendKeys("shilpashree_v"); 31 | driver.findElement(By.id("_ctl0_ContentPlaceHolder1_lnkSearch")) 32 | .click(); 33 | Thread.sleep(20000); 34 | 35 | WebElement downloadLink = null; 36 | int count = 0; 37 | while (downloadLink == null) { 38 | downloadLink = driver.findElement(By 39 | .id("_ctl0_ContentPlaceHolder1_lnkDownload")); 40 | count++; 41 | if (count == 50) { 42 | 43 | break; 44 | } 45 | } 46 | // WebElement downloadLink = driver.findElement(By 47 | // .id("_ctl0_ContentPlaceHolder1_lnkDownload")); 48 | FileDownloader downloadTestFile = new FileDownloader(driver); 49 | String downloadedFileAbsoluteLocation = downloadTestFile 50 | .downloadFile(downloadLink); 51 | System.out.println("downloadTestFile.downloadFile(downloadLink);" 52 | + downloadTestFile.downloadFile(downloadLink)); 53 | 54 | Assert.assertTrue(new File(downloadedFileAbsoluteLocation).exists()); 55 | // assertThat(downloadTestFile.getHTTPStatusOfLastDownloadAttempt(), 56 | // is(equalTo(200))); 57 | } 58 | 59 | @Test 60 | public void checkValidMD5Hash() throws Exception { 61 | CheckFileHash fileToCheck = new CheckFileHash(); 62 | fileToCheck.fileToCheck(new File(System.getProperty("java.io.tmpdir"))); 63 | 64 | fileToCheck.hashDetails("617bfc4b78b03a0f61c98188376d2a6d", 65 | HashType.MD5); 66 | Assert.assertTrue(fileToCheck.hasAValidHash()); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/actions/FileDownloader.java: -------------------------------------------------------------------------------- 1 | package main.java.actions; 2 | /** 3 | * The FileDownloader class is used to download the file from the browser 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.io.File; 11 | import java.io.IOException; 12 | import java.net.URISyntaxException; 13 | import java.net.URL; 14 | import java.util.Set; 15 | 16 | import org.apache.commons.io.FileUtils; 17 | import org.apache.http.HttpResponse; 18 | import org.apache.http.client.HttpClient; 19 | import org.apache.http.client.methods.HttpGet; 20 | import org.apache.http.client.params.ClientPNames; 21 | import org.apache.http.client.protocol.ClientContext; 22 | import org.apache.http.impl.client.BasicCookieStore; 23 | import org.apache.http.impl.client.DefaultHttpClient; 24 | import org.apache.http.impl.cookie.BasicClientCookie; 25 | import org.apache.http.params.HttpParams; 26 | import org.apache.http.protocol.BasicHttpContext; 27 | import org.apache.log4j.Level; 28 | import org.apache.log4j.Logger; 29 | import org.openqa.selenium.Cookie; 30 | import org.openqa.selenium.WebDriver; 31 | import org.openqa.selenium.WebElement; 32 | 33 | public class FileDownloader { 34 | 35 | private static final Logger LOG = Logger.getLogger(FileDownloader.class); 36 | private WebDriver driver; 37 | private String localDownloadPath = System.getProperty("java.io.tmpdir"); 38 | 39 | private boolean followRedirects = true; 40 | private boolean mimicWebDriverCookieState = true; 41 | private int httpStatusOfLastDownloadAttempt = 0; 42 | 43 | public FileDownloader(WebDriver driverObject) { 44 | this.driver = driverObject; 45 | LOG.setLevel(Level.INFO); 46 | 47 | } 48 | 49 | /** 50 | * Specify if the FileDownloader class should follow redirects when trying 51 | * to download a file 52 | * 53 | * @param value 54 | */ 55 | public void followRedirectsWhenDownloading(boolean value) { 56 | this.followRedirects = value; 57 | } 58 | 59 | /** 60 | * Get the current location that files will be downloaded to. 61 | * 62 | * @return The filepath that the file will be downloaded to. 63 | */ 64 | public String localDownloadPath() { 65 | return this.localDownloadPath; 66 | } 67 | 68 | /** 69 | * Set the path that files will be downloaded to. 70 | * 71 | * @param filePath 72 | * The filepath that the file will be downloaded to. 73 | */ 74 | public void localDownloadPath(String filePath) { 75 | this.localDownloadPath = filePath; 76 | } 77 | 78 | /** 79 | * Download the file specified in the href attribute of a WebElement 80 | * 81 | * @param element 82 | * @return 83 | * @throws Exception 84 | */ 85 | public String downloadFile(WebElement element) throws Exception { 86 | return downloader(element, "href"); 87 | } 88 | 89 | /** 90 | * Download the image specified in the src attribute of a WebElement 91 | * 92 | * @param element 93 | * @return 94 | * @throws Exception 95 | */ 96 | public String downloadImage(WebElement element) throws Exception { 97 | return downloader(element, "src"); 98 | } 99 | 100 | /** 101 | * Gets the HTTP status code of the last download file attempt 102 | * 103 | * @return 104 | */ 105 | public int getHTTPStatusOfLastDownloadAttempt() { 106 | return this.httpStatusOfLastDownloadAttempt; 107 | } 108 | 109 | /** 110 | * Mimic the cookie state of WebDriver (Defaults to true) This will enable 111 | * you to access files that are only available when logged in. If set to 112 | * false the connection will be made as an anonymouse user 113 | * 114 | * @param value 115 | */ 116 | public void mimicWebDriverCookieState(boolean value) { 117 | this.mimicWebDriverCookieState = value; 118 | } 119 | 120 | /** 121 | * Load in all the cookies WebDriver currently knows about so that we can 122 | * mimic the browser cookie state 123 | * 124 | * @param seleniumCookieSet 125 | * @return 126 | */ 127 | private BasicCookieStore mimicCookieState(Set seleniumCookieSet) { 128 | BasicCookieStore mimicWebDriverCookieStore = new BasicCookieStore(); 129 | for (Cookie seleniumCookie : seleniumCookieSet) { 130 | BasicClientCookie duplicateCookie = new BasicClientCookie( 131 | seleniumCookie.getName(), seleniumCookie.getValue()); 132 | duplicateCookie.setDomain(seleniumCookie.getDomain()); 133 | duplicateCookie.setSecure(seleniumCookie.isSecure()); 134 | duplicateCookie.setExpiryDate(seleniumCookie.getExpiry()); 135 | duplicateCookie.setPath(seleniumCookie.getPath()); 136 | mimicWebDriverCookieStore.addCookie(duplicateCookie); 137 | } 138 | 139 | return mimicWebDriverCookieStore; 140 | } 141 | 142 | /** 143 | * Perform the file/image download. 144 | * 145 | * @param element 146 | * @param attribute 147 | * @return 148 | * @throws IOException 149 | * @throws NullPointerException 150 | */ 151 | private String downloader(WebElement element, String attribute) 152 | throws IOException, NullPointerException, URISyntaxException { 153 | 154 | String fileToDownloadLocation = element.getAttribute(attribute); 155 | 156 | if (fileToDownloadLocation.trim().equals("")) 157 | throw new NullPointerException( 158 | "The element you have specified does not link to anything!"); 159 | 160 | URL fileToDownload = new URL(fileToDownloadLocation); 161 | File downloadedFile = new File(this.localDownloadPath 162 | + fileToDownload.getFile().replaceFirst("/|\\\\", "")); 163 | if (downloadedFile.canWrite() == false) { 164 | downloadedFile.setWritable(true); 165 | } 166 | 167 | HttpClient client = new DefaultHttpClient(); 168 | BasicHttpContext localContext = new BasicHttpContext(); 169 | 170 | 171 | if (this.mimicWebDriverCookieState) { 172 | localContext.setAttribute(ClientContext.COOKIE_STORE, 173 | mimicCookieState(this.driver.manage().getCookies())); 174 | } 175 | 176 | HttpGet httpget = new HttpGet(fileToDownload.toURI()); 177 | HttpParams httpRequestParameters = httpget.getParams(); 178 | httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, 179 | this.followRedirects); 180 | httpget.setParams(httpRequestParameters); 181 | 182 | 183 | HttpResponse response = client.execute(httpget, localContext); 184 | this.httpStatusOfLastDownloadAttempt = response.getStatusLine() 185 | .getStatusCode(); 186 | 187 | 188 | FileUtils.copyInputStreamToFile(response.getEntity().getContent(), 189 | downloadedFile); 190 | response.getEntity().getContent().close(); 191 | 192 | 193 | return downloadedFile.getAbsolutePath(); 194 | } 195 | 196 | } -------------------------------------------------------------------------------- /src/main/java/actions/HashType.java: -------------------------------------------------------------------------------- 1 | package main.java.actions; 2 | /** 3 | * The HashType class is an enum class for hash type 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | public enum HashType { 11 | MD5, SHA1; 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/actions/MethodType.java: -------------------------------------------------------------------------------- 1 | package main.java.actions; 2 | /** 3 | * The MethodType class is used to identify the method specified in the testcase 4 | * excel and to perform the same action 5 | * 6 | * @author Shilpashree_V 7 | * @version 1.0 8 | * @since February 2015 9 | * 10 | */ 11 | import java.awt.AWTException; 12 | import java.awt.Robot; 13 | import java.awt.Toolkit; 14 | import java.awt.datatransfer.StringSelection; 15 | import java.awt.event.KeyEvent; 16 | import java.io.File; 17 | import java.io.IOException; 18 | import java.lang.reflect.Field; 19 | import java.lang.reflect.InvocationTargetException; 20 | import java.lang.reflect.Method; 21 | import java.util.ArrayList; 22 | import java.util.Arrays; 23 | import java.util.List; 24 | import java.util.Set; 25 | import java.util.concurrent.TimeUnit; 26 | import main.java.model.MethodParameters; 27 | import org.openqa.selenium.Alert; 28 | import org.openqa.selenium.By; 29 | import org.openqa.selenium.JavascriptExecutor; 30 | import org.openqa.selenium.WebElement; 31 | import org.openqa.selenium.interactions.Actions; 32 | import org.openqa.selenium.support.ui.ExpectedConditions; 33 | import org.openqa.selenium.support.ui.Select; 34 | import org.openqa.selenium.support.ui.WebDriverWait; 35 | import org.testng.Assert; 36 | import org.testng.Reporter; 37 | import main.java.util.MainTestNG; 38 | import main.java.util.ReadElementLocators; 39 | import main.java.util.WebDriverClass; 40 | 41 | public class MethodType { 42 | 43 | List listOfElements = new ArrayList(); 44 | WebElement element; 45 | ReadElementLocators read = new ReadElementLocators(); 46 | 47 | String alertText = null; 48 | String titleOfPage = null; 49 | 50 | /** 51 | * @param methodType 52 | * @param objectLocators 53 | * @param actionType 54 | * @param data 55 | * 56 | * Object locators 57 | */ 58 | public void methodExecutor(String methodType, String objectLocators, 59 | String actionType, String data) { 60 | MethodParameters mModel = new MethodParameters(); 61 | mModel.setMethodType(methodType); 62 | mModel.setObjectLocators(objectLocators); 63 | mModel.setActionType(actionType); 64 | mModel.setData(data); 65 | 66 | MainTestNG.LOGGER 67 | .info("methodType= " + methodType + "objectLocators=" 68 | + objectLocators + "actionType=" + actionType 69 | + "data= " + data); 70 | 71 | switch (methodType) { 72 | 73 | case "ID": 74 | findElementById(objectLocators); 75 | mModel.setElement(listOfElements); 76 | findMethod(methodType, objectLocators, actionType, data, mModel); 77 | break; 78 | case "NAME": 79 | findElementByName(objectLocators); 80 | mModel.setElement(listOfElements); 81 | findMethod(methodType, objectLocators, actionType, data, mModel); 82 | break; 83 | case "XPATH": 84 | findElementByXpath(objectLocators); 85 | mModel.setElement(listOfElements); 86 | findMethod(methodType, objectLocators, actionType, data, mModel); 87 | break; 88 | case "CSS": 89 | findElementByCssSelector(objectLocators); 90 | mModel.setElement(listOfElements); 91 | findMethod(methodType, objectLocators, actionType, data, mModel); 92 | break; 93 | default: 94 | if (actionType.contains(":")) { 95 | String[] actsplit = actionType.split(":"); 96 | mModel.setActionType(actsplit[1]); 97 | actionType = actsplit[0]; 98 | System.out.println(actsplit[1]); 99 | System.out.println(actsplit[0]); 100 | } 101 | findMethod(methodType, objectLocators, actionType, data, mModel); 102 | break; 103 | } 104 | 105 | } 106 | 107 | /** 108 | *Identifying the method at run time 109 | */ 110 | public void findMethod(String methodType, String objectLocators, 111 | String actionType, String data, MethodParameters model) { 112 | Class cl = null; 113 | try { 114 | cl = (Class) Class.forName("main.java.actions.MethodType"); 115 | main.java.actions.MethodType clName = (MethodType) cl.newInstance(); 116 | Method[] methods = cl.getMethods(); 117 | Method methodName = findMethods(actionType, methods); 118 | methodName.invoke(clName, (Object) model); 119 | 120 | } catch (InvocationTargetException e) { 121 | Throwable cause = e.getCause(); 122 | 123 | MainTestNG.LOGGER 124 | .info("exception occured in finding methods, method name is incorrect" 125 | + e); 126 | } catch (Exception e) { 127 | 128 | MainTestNG.LOGGER 129 | .info("exception occured in finding methods, method name is incorrect" 130 | + e); 131 | } 132 | 133 | } 134 | 135 | /** 136 | *Find Element By CSS 137 | */ 138 | private void findElementByCssSelector(String objectLocators) { 139 | 140 | WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); 141 | wait.until(ExpectedConditions.visibilityOfElementLocated(By 142 | .cssSelector(objectLocators))); 143 | 144 | List list1 = WebDriverClass.getInstance().findElements( 145 | By.cssSelector(objectLocators)); 146 | listOfElements = list1; 147 | 148 | } 149 | 150 | /** 151 | *Find Element By ID 152 | */ 153 | public void findElementById(String objectLocators) { 154 | 155 | WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); 156 | List list1 = wait.until(ExpectedConditions 157 | .presenceOfAllElementsLocatedBy(By.id(objectLocators))); 158 | 159 | listOfElements = list1; 160 | 161 | } 162 | 163 | /** 164 | *Find Element By Xpath 165 | */ 166 | public void findElementByXpath(String objectLocators) { 167 | 168 | WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); 169 | 170 | wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By 171 | .xpath(objectLocators))); 172 | 173 | List list1 = wait 174 | 175 | .until(ExpectedConditions.visibilityOfAllElements(WebDriverClass 176 | .getDriver().findElements(By.xpath(objectLocators)))); 177 | 178 | listOfElements = list1; 179 | 180 | } 181 | 182 | /** 183 | *Find Element By Name 184 | */ 185 | public void findElementByName(String objectLocators) { 186 | MainTestNG.LOGGER.info("findElementByName==" + objectLocators); 187 | 188 | WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); 189 | wait.until(ExpectedConditions.visibilityOfAllElements(WebDriverClass 190 | .getDriver().findElements(By.name(objectLocators)))); 191 | MainTestNG.LOGGER.info("element found==" + objectLocators); 192 | 193 | List list1 = WebDriverClass.getInstance().findElements( 194 | By.name(objectLocators)); 195 | MainTestNG.LOGGER.info("list size" + list1.size()); 196 | listOfElements = list1; 197 | 198 | } 199 | 200 | /** 201 | *Find corresponding method name in existing methods 202 | */ 203 | public static Method findMethods(String methodName, Method[] methods) { 204 | 205 | for (int i = 0; i < methods.length; i++) { 206 | if (methodName.equalsIgnoreCase(methods[i].getName().toString())) { 207 | return methods[i]; 208 | } 209 | } 210 | return null; 211 | } 212 | 213 | /** 214 | *Click on button/checkbox/radio button 215 | */ 216 | public void click(MethodParameters model) { 217 | 218 | WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); 219 | wait.until( 220 | ExpectedConditions.elementToBeClickable(model.getElement().get( 221 | 0))).click(); 222 | 223 | MainTestNG.LOGGER.info("click method started" 224 | + model.getObjectLocators()); 225 | MainTestNG.LOGGER.info("click method completed"); 226 | } 227 | 228 | /** 229 | *Click on Submit button 230 | */ 231 | public void submit(MethodParameters model) { 232 | MainTestNG.LOGGER.info("submit method started" 233 | + model.getObjectLocators()); 234 | model.getElement().get(0).submit(); 235 | MainTestNG.LOGGER.info("submit method end"); 236 | } 237 | 238 | /** 239 | *Enter data into text field/text area 240 | */ 241 | public void enterText(MethodParameters model) { 242 | 243 | MainTestNG.LOGGER 244 | .info(" inside enterText(), data to entered into the text==" 245 | + model.getData()); 246 | System.out.println("model.getElement().get(0)==" 247 | + model.getElement().get(0)); 248 | model.getElement().get(0).sendKeys(model.getData()); 249 | MainTestNG.LOGGER.info("enterText() exit"); 250 | } 251 | 252 | /** 253 | *Read the value present in the text field 254 | */ 255 | public void readTextFieldValue(MethodParameters model) { 256 | MainTestNG.LOGGER.info("inside readTextFieldValue()" 257 | + model.getObjectLocators()); 258 | model.getElement().get(0).getAttribute("value"); 259 | MainTestNG.LOGGER.info("end of readTextFieldValue"); 260 | } 261 | 262 | /** 263 | *Alert accept meaning click on OK button 264 | */ 265 | public void alertAccept(MethodParameters model) { 266 | 267 | WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); 268 | wait.until(ExpectedConditions.alertIsPresent()); 269 | 270 | MainTestNG.LOGGER.info("inside alertAccept()"); 271 | 272 | wait1(2000); 273 | 274 | Alert alert = WebDriverClass.getInstance().switchTo().alert(); 275 | wait1(2000); 276 | 277 | alert.accept(); 278 | MainTestNG.LOGGER.info("completed alertAccept()"); 279 | } 280 | 281 | /** 282 | *Alert dismiss meaning click on Cancel button 283 | */ 284 | public void alertDismiss(MethodParameters model) { 285 | WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); 286 | wait.until(ExpectedConditions.alertIsPresent()); 287 | 288 | MainTestNG.LOGGER.info("inside alertDismiss()"); 289 | wait1(2000); 290 | model.getElement().get(0).click(); 291 | Alert alert = WebDriverClass.getInstance().switchTo().alert(); 292 | wait1(2000); 293 | alert.dismiss(); 294 | } 295 | 296 | /** 297 | *Get the title of the page and verify the title 298 | */ 299 | public void verifyTitleOfPage(MethodParameters model) { 300 | MainTestNG.LOGGER.info("inside verifyTitleOfPage()" + "title==" 301 | + WebDriverClass.getInstance().getTitle() + "data from excel=" 302 | + model.getData()); 303 | 304 | wait1(2000); 305 | String actual = WebDriverClass.getInstance().getTitle().toString(); 306 | String expected = model.getData().toString(); 307 | Assert.assertEquals(actual, expected); 308 | MainTestNG.LOGGER 309 | .info("assert verification successful verifyTitleOfPage()"); 310 | 311 | } 312 | /** 313 | *Make the driver to wait for specified amount of time 314 | */ 315 | public void wait1(long i) { 316 | try { 317 | Thread.sleep(i); 318 | } catch (InterruptedException e) { 319 | MainTestNG.LOGGER.severe("InvalidFormatException" + e); 320 | } 321 | } 322 | 323 | /** 324 | *Select from the drop down list,if the drop down element tag is "SELECT" then use this method 325 | */ 326 | public void selectDropDownByVisibleText(MethodParameters model) { 327 | wait1(2000); 328 | MainTestNG.LOGGER.info("inside selectDropDownByVisibleText"); 329 | 330 | WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); 331 | wait.pollingEvery(2, TimeUnit.SECONDS).until( 332 | ExpectedConditions.elementToBeClickable(model.getElement().get( 333 | 0))); 334 | Select sel = new Select(model.getElement().get(0)); 335 | sel.selectByVisibleText(model.getData()); 336 | wait1(2000); 337 | } 338 | 339 | /** 340 | *Select the value from a dropdown list by its index 341 | */ 342 | public void selectDropDownByIndex(MethodParameters model) { 343 | MainTestNG.LOGGER.info("inside selectDropDownByIndex"); 344 | Select sel = new Select(model.getElement().get(0)); 345 | sel.selectByIndex(Integer.parseInt(model.getData())); 346 | } 347 | 348 | /** 349 | *Select the value from a dropdown list by its value 350 | */ 351 | public void selectDropDownByValue(MethodParameters model) { 352 | MainTestNG.LOGGER.info("inside selectDropDownByValue"); 353 | Select sel = new Select(model.getElement().get(0)); 354 | sel.selectByValue(model.getData()); 355 | } 356 | 357 | /** 358 | *Switch To frame( html inside another html) 359 | */ 360 | public void switchToFrame(MethodParameters model) { 361 | MainTestNG.LOGGER.info("inside switchToFrame"); 362 | WebDriverClass.getInstance().switchTo() 363 | .frame(model.getElement().get(0)); 364 | 365 | } 366 | 367 | /** 368 | *Switch back to previous frame or html 369 | */ 370 | public void switchOutOfFrame(MethodParameters model) { 371 | MainTestNG.LOGGER.info("inside switchOutOfFrame"); 372 | WebDriverClass.getInstance().switchTo().defaultContent(); 373 | 374 | } 375 | 376 | /** 377 | *Select the multiple value from a dropdown list 378 | */ 379 | public void selectFromListDropDown(MethodParameters model) { 380 | MainTestNG.LOGGER.info("inside selectFromListDropDown"); 381 | wait1(2000); 382 | for (WebElement element1 : model.getElement()) { 383 | 384 | if (element1.getText().equals(model.getData())) { 385 | element1.click(); 386 | break; 387 | } 388 | } 389 | 390 | wait1(2000); 391 | } 392 | 393 | /** 394 | *Navigate to next page 395 | */ 396 | public void moveToNextPage(MethodParameters model) { 397 | WebDriverClass.getInstance().navigate().forward(); 398 | } 399 | 400 | /** 401 | *Navigate to previous page 402 | */ 403 | public void moveToPreviousPage(MethodParameters model) { 404 | WebDriverClass.getInstance().navigate().back(); 405 | } 406 | 407 | /** 408 | *Maximize the window 409 | */ 410 | public void maximizeWindow(MethodParameters model) { 411 | WebDriverClass.getInstance().manage().window().maximize(); 412 | } 413 | 414 | /** 415 | *Reads the text present in the web element 416 | */ 417 | public void readText(MethodParameters model) { 418 | MainTestNG.LOGGER 419 | .info("getText() method called and value of getText==*************" 420 | + model.getElement().get(0).getText()); 421 | model.getElement().get(0).getText(); 422 | MainTestNG.LOGGER.info("readText completed"); 423 | } 424 | /** 425 | *Quit the application 426 | */ 427 | public void quit(MethodParameters model) { 428 | WebDriverClass.getInstance().quit(); 429 | } 430 | 431 | /** 432 | *Closes the driver 433 | */ 434 | public void close(MethodParameters model) { 435 | WebDriverClass.getInstance().close(); 436 | } 437 | 438 | /** 439 | *Checks that the element is displayed in the current web page 440 | */ 441 | public void isDisplayed(MethodParameters model) { 442 | model.getElement().get(0).isDisplayed(); 443 | } 444 | 445 | /** 446 | *Checks that the element is enabled in the current web page 447 | */ 448 | public void isEnabled(MethodParameters model) { 449 | model.getElement().get(0).isEnabled(); 450 | } 451 | 452 | /** 453 | *Selects a radio button 454 | */ 455 | public void selectRadioButton(MethodParameters model) { 456 | model.getElement().get(0).click(); 457 | } 458 | 459 | /** 460 | *Refresh the current web page 461 | */ 462 | public void refreshPage(MethodParameters model) { 463 | WebDriverClass.getInstance().navigate().refresh(); 464 | } 465 | 466 | /** 467 | *Switch back to the parent window 468 | */ 469 | public void switchToParentWindow(MethodParameters model) { 470 | String parentWindow = WebDriverClass.getInstance().getWindowHandle(); 471 | WebDriverClass.getInstance().switchTo().window(parentWindow); 472 | } 473 | 474 | /** 475 | *Switche to the child window 476 | */ 477 | public void switchToChildWindow(MethodParameters model) { 478 | 479 | model.getElement().get(0).click(); 480 | 481 | String parent = WebDriverClass.getInstance().getWindowHandle(); 482 | Set windows = WebDriverClass.getInstance().getWindowHandles(); 483 | 484 | try { 485 | if (windows.size() > 1) { 486 | for (String child : windows) { 487 | if (!child.equals(parent)) { 488 | 489 | if (WebDriverClass.getInstance().switchTo() 490 | .window(child).getTitle() 491 | .equals(model.getData())) { 492 | 493 | WebDriverClass.getInstance().switchTo() 494 | .window(child); 495 | } 496 | 497 | } 498 | } 499 | } 500 | } catch (Exception e) { 501 | 502 | throw new RuntimeException("Exception", e); 503 | } 504 | 505 | } 506 | 507 | /** 508 | *Scrolls down the page till the element is visible 509 | */ 510 | public void scrollElementIntoView(MethodParameters model) { 511 | wait1(1000); 512 | 513 | MainTestNG.LOGGER.info("scrollElementIntoView started"); 514 | ((JavascriptExecutor) WebDriverClass.getDriver()) 515 | .executeScript("arguments[0].scrollIntoView(true);", model 516 | .getElement().get(0)); 517 | wait1(1000); 518 | 519 | } 520 | 521 | /** 522 | *Scrolls down the page till the element is visible and clicks on the 523 | *element 524 | */ 525 | public void scrollElementIntoViewClick(MethodParameters model) { 526 | Actions action = new Actions(WebDriverClass.getDriver()); 527 | action.moveToElement(model.getElement().get(0)).click().perform(); 528 | } 529 | 530 | /** 531 | *Reads the url of current web page 532 | */ 533 | public void readUrlOfPage(MethodParameters model) { 534 | WebDriverClass.getInstance().getCurrentUrl(); 535 | } 536 | 537 | 538 | /** 539 | *Navigates to the specified url 540 | */ 541 | public void navigateToURL(MethodParameters model) { 542 | WebDriverClass.getInstance().navigate().to(model.getData()); 543 | } 544 | 545 | public static WebElement waitForElement(By by) { 546 | int count = 0; 547 | WebDriverWait wait = null; 548 | while (!(wait.until(ExpectedConditions.presenceOfElementLocated(by)) 549 | .isDisplayed())) { 550 | wait = new WebDriverWait(WebDriverClass.getInstance(), 60); 551 | wait.pollingEvery(5, TimeUnit.MILLISECONDS); 552 | wait.until(ExpectedConditions.visibilityOfElementLocated(by)) 553 | .isDisplayed(); 554 | wait.until(ExpectedConditions.presenceOfElementLocated(by)) 555 | .isDisplayed(); 556 | count++; 557 | if (count == 100) { 558 | break; 559 | } 560 | return wait.until(ExpectedConditions.presenceOfElementLocated(by)); 561 | } 562 | return wait.until(ExpectedConditions.presenceOfElementLocated(by)); 563 | } 564 | 565 | /** 566 | *Provide Login name for window authentication 567 | */ 568 | public static void windowAuthenticationLoginName(MethodParameters model) { 569 | 570 | Alert alert = WebDriverClass.getDriver().switchTo().alert(); 571 | alert.sendKeys(model.getData()); 572 | } 573 | 574 | /** 575 | *Provide password for window authentication 576 | */ 577 | public static void windowAuthenticationPassword(MethodParameters model) { 578 | Robot robot; 579 | try { 580 | robot = new Robot(); 581 | robot.keyPress(KeyEvent.VK_TAB); 582 | String letter = model.getData(); 583 | for (int i = 0; i < letter.length(); i++) { 584 | boolean upperCase = Character.isUpperCase(letter.charAt(i)); 585 | String KeyVal = Character.toString(letter.charAt(i)); 586 | String variableName = "VK_" + KeyVal.toUpperCase(); 587 | Class clazz = KeyEvent.class; 588 | Field field = clazz.getField(variableName); 589 | int keyCode = field.getInt(null); 590 | 591 | if (upperCase){ 592 | robot.keyPress(KeyEvent.VK_SHIFT); 593 | } 594 | 595 | robot.keyPress(keyCode); 596 | robot.keyRelease(keyCode); 597 | 598 | if (upperCase){ 599 | robot.keyRelease(KeyEvent.VK_SHIFT); 600 | } 601 | } 602 | robot.keyPress(KeyEvent.VK_ENTER); 603 | } catch (AWTException e) { 604 | 605 | MainTestNG.LOGGER.severe(e.getMessage()); 606 | 607 | } catch (NoSuchFieldException e) { 608 | 609 | MainTestNG.LOGGER.severe(e.getMessage()); 610 | } catch (SecurityException e) { 611 | 612 | MainTestNG.LOGGER.severe(e.getMessage()); 613 | } catch (IllegalArgumentException e) { 614 | 615 | MainTestNG.LOGGER.severe(e.getMessage()); 616 | } catch (IllegalAccessException e) { 617 | 618 | MainTestNG.LOGGER.severe(e.getMessage()); 619 | } 620 | } 621 | 622 | /** 623 | * @param model 624 | * Lets say there is header menu bar, on hovering the mouse, drop down should be displayed 625 | */ 626 | public void dropDownByMouseHover(MethodParameters model) { 627 | Actions action = new Actions(WebDriverClass.getInstance()); 628 | 629 | action.moveToElement(model.getElement().get(0)).perform(); 630 | WebElement subElement = WebDriverClass.getInstance().findElement( 631 | By.xpath(model.getData())); 632 | action.moveToElement(subElement); 633 | action.click().build().perform(); 634 | 635 | } 636 | 637 | /** 638 | *verifies the data present in the text field 639 | */ 640 | public void verifyTextFieldData(MethodParameters model) { 641 | Assert.assertEquals(model.getElement().get(0).getAttribute("value"), 642 | model.getData()); 643 | } 644 | 645 | 646 | /** 647 | * @param model 648 | * Read title of the page and verify it 649 | */ 650 | public void readTitleOfPage(MethodParameters model) { 651 | if (!(titleOfPage == null)) { 652 | titleOfPage = null; 653 | } 654 | titleOfPage = WebDriverClass.getInstance().getTitle(); 655 | } 656 | 657 | 658 | /** 659 | * @param model 660 | * File upload in IE browser. 661 | */ 662 | public void fileUploadinIE(MethodParameters model) { 663 | model.getElement().get(0).click(); 664 | StringSelection ss = new StringSelection(model.getData()); 665 | Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); 666 | Robot r; 667 | try { 668 | r = new Robot(); 669 | 670 | r.keyPress(KeyEvent.VK_ENTER); 671 | 672 | r.keyRelease(KeyEvent.VK_ENTER); 673 | 674 | r.keyPress(KeyEvent.VK_CONTROL); 675 | r.keyPress(KeyEvent.VK_V); 676 | r.keyRelease(KeyEvent.VK_V); 677 | r.keyRelease(KeyEvent.VK_CONTROL); 678 | 679 | r.keyPress(KeyEvent.VK_ENTER); 680 | r.keyRelease(KeyEvent.VK_ENTER); 681 | 682 | } catch (AWTException e) { 683 | 684 | MainTestNG.LOGGER.severe(e.getMessage()); 685 | } 686 | 687 | } 688 | 689 | 690 | /** 691 | * @param model 692 | * Verify the alert text 693 | */ 694 | public void verifyalertText(MethodParameters model) { 695 | model.getElement().get(0).click(); 696 | wait1(1000); 697 | Alert alert = WebDriverClass.getInstance().switchTo().alert(); 698 | wait1(1000); 699 | if (!(alertText == null)) { 700 | alertText = null; 701 | } 702 | alertText = alert.getText(); 703 | Assert.assertEquals(alertText.toString(), model.getData()); 704 | alert.accept(); 705 | } 706 | 707 | 708 | /** 709 | * @param model 710 | * SSL errors that appear on IE browser can be resolved 711 | */ 712 | public void certificateErrorsIE(MethodParameters model) { 713 | 714 | WebDriverClass 715 | .getDriver() 716 | .navigate() 717 | .to("javascript:document.getElementById('overridelink').click()"); 718 | } 719 | 720 | /** 721 | * @param model 722 | * Not tested 723 | */ 724 | public void DragAndDrop(MethodParameters model) { 725 | String[] actType = model.getActionType().split("$"); 726 | 727 | WebElement sourceElement = WebDriverClass.getDriver().findElement( 728 | By.xpath(actType[0])); 729 | WebElement destinationElement = WebDriverClass.getDriver().findElement( 730 | By.xpath(actType[1])); 731 | 732 | Actions action = new Actions(WebDriverClass.getDriver()); 733 | action.dragAndDrop(sourceElement, destinationElement).build().perform(); 734 | 735 | } 736 | 737 | /** 738 | * @param model 739 | * clear the content of the text field 740 | */ 741 | public void clear(MethodParameters model) { 742 | wait1(2000); 743 | WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 60); 744 | wait.until(ExpectedConditions.visibilityOf(model.getElement().get(0))); 745 | model.getElement().get(0).clear(); 746 | } 747 | 748 | /** 749 | *Makes the driver to sleep for specified time 750 | */ 751 | public void sleep(MethodParameters model) { 752 | try { 753 | Integer i = Integer.parseInt(model.getData()); 754 | System.out.println(i); 755 | Thread.sleep(i); 756 | 757 | } catch (InterruptedException e) { 758 | 759 | MainTestNG.LOGGER.info("InterruptedException" + e.getMessage()); 760 | } 761 | } 762 | 763 | /** 764 | *Verifies the Text present in the element 765 | */ 766 | public void verifyText(MethodParameters model) { 767 | MainTestNG.LOGGER.info("model.getElement().get(0).getText()**********" 768 | + model.getElement().get(0).getText()); 769 | MainTestNG.LOGGER.info("model.getData()**********" + model.getData()); 770 | 771 | Assert.assertEquals(model.getData(), model.getElement().get(0) 772 | .getText().toString()); 773 | MainTestNG.LOGGER.info("verify text completed"); 774 | } 775 | 776 | /** 777 | *Verifies that the particular file is exists or not 778 | */ 779 | public void verifyFileExists(MethodParameters model) { 780 | try { 781 | File file = new File(model.getData()); 782 | if (file.exists() && !(file.isDirectory() && file.isFile())) { 783 | Assert.assertEquals(file.getAbsoluteFile(), model.getData()); 784 | } 785 | } catch (Exception e) { 786 | throw e; 787 | } 788 | 789 | } 790 | 791 | /** 792 | *Downloads a file from IE browser 793 | */ 794 | public void downloadFileIE(MethodParameters model) { 795 | FileDownloader downloadTestFile = new FileDownloader( 796 | WebDriverClass.getDriver()); 797 | String downloadedFileAbsoluteLocation; 798 | try { 799 | downloadedFileAbsoluteLocation = downloadTestFile 800 | .downloadFile(model.getElement().get(0)); 801 | 802 | Assert.assertTrue(new File(downloadedFileAbsoluteLocation).exists()); 803 | } catch (Exception e) { 804 | 805 | MainTestNG.LOGGER.info("exception occured"); 806 | } 807 | 808 | } 809 | 810 | 811 | /** 812 | * @param model 813 | * Not tested 814 | */ 815 | public void webTableClick(MethodParameters model) { 816 | 817 | String[] actType = model.getActionType().split("\\$"); 818 | 819 | WebElement mytable = WebDriverClass.getDriver().findElement( 820 | By.xpath(actType[0])); 821 | 822 | List rowstable = mytable.findElements(By.tagName("tr")); 823 | 824 | int rows_count = rowstable.size(); 825 | 826 | for (int row = 0; row < rows_count; row++) { 827 | 828 | List Columnsrow = rowstable.get(row).findElements( 829 | By.tagName("td")); 830 | 831 | int columnscount = Columnsrow.size(); 832 | 833 | for (int column = 0; column < columnscount; column++) { 834 | 835 | String celtext = Columnsrow.get(column).getText(); 836 | celtext.getClass(); 837 | } 838 | } 839 | } 840 | 841 | /** 842 | * @param model 843 | * Select date from date picker 844 | */ 845 | public void selectDateFromCalendar(MethodParameters model) { 846 | 847 | String[] actionType = model.getActionType().split("$$"); 848 | 849 | String[] data = model.getData().split("/"); 850 | 851 | List monthList = Arrays.asList("January", "February", "March", 852 | "April", "May", "June", "July", "August", "September", 853 | "October", "November", "December"); 854 | 855 | int expMonth; 856 | int expYear; 857 | String expDate = null; 858 | // Calendar Month and Year 859 | String calMonth = null; 860 | String calYear = null; 861 | boolean dateNotFound; 862 | 863 | // WebDriverClass.getDriver() 864 | // .findElement(By.xpath(".//*[@id='ui-datepicker-div']")).click(); 865 | 866 | WebDriverClass.getDriver().findElement(By.xpath(actionType[0])).click(); 867 | 868 | dateNotFound = true; 869 | 870 | // Set your expected date, month and year. 871 | expDate = data[0]; 872 | expMonth = Integer.parseInt(data[1]); 873 | expYear = Integer.parseInt(data[2]); 874 | 875 | // This loop will be executed continuously till dateNotFound Is true. 876 | while (dateNotFound) { 877 | // Retrieve current selected month name from date picker popup. 878 | calMonth = WebDriverClass.getDriver() 879 | .findElement(By.className("ui-datepicker-month")).getText(); 880 | 881 | // Retrieve current selected year name from date picker popup. 882 | calYear = WebDriverClass.getDriver() 883 | .findElement(By.className("ui-datepicker-year")).getText(); 884 | 885 | /* 886 | * If current selected month and year are same as expected month and 887 | * year then go Inside this condition. 888 | */ 889 | if (monthList.indexOf(calMonth) + 1 == expMonth 890 | && (expYear == Integer.parseInt(calYear))) { 891 | /* 892 | * Call selectDate function with date to select and set 893 | * dateNotFound flag to false. 894 | */ 895 | selectDate(expDate); 896 | dateNotFound = false; 897 | } 898 | // If current selected month and year are less than expected month 899 | // and year then go Inside this condition. 900 | else if (monthList.indexOf(calMonth) + 1 < expMonth 901 | && (expYear == Integer.parseInt(calYear)) 902 | || expYear > Integer.parseInt(calYear)) { 903 | 904 | // Click on next button of date picker. 905 | /* 906 | * WebDriverClass .getDriver() .findElement( 907 | * By.xpath(".//*[@id='ui-datepicker-div']/div[2]/div/a/span")) 908 | * .click(); 909 | */ 910 | 911 | WebDriverClass.getDriver().findElement(By.xpath(actionType[1])) 912 | .click(); 913 | } 914 | // If current selected month and year are greater than expected 915 | // month and year then go Inside this condition. 916 | else if (monthList.indexOf(calMonth) + 1 > expMonth 917 | && (expYear == Integer.parseInt(calYear)) 918 | || expYear < Integer.parseInt(calYear)) { 919 | 920 | // Click on previous button of date picker. 921 | 922 | /* 923 | * WebDriverClass .getDriver() .findElement( 924 | * By.xpath(".//*[@id='ui-datepicker-div']/div[1]/div/a/span")) 925 | * .click(); 926 | */ 927 | 928 | WebDriverClass.getDriver().findElement(By.xpath(actionType[2])) 929 | .click(); 930 | } 931 | } 932 | wait1(3000); 933 | } 934 | 935 | /** 936 | *Selects the Date 937 | */ 938 | public void selectDate(String date) { 939 | WebElement datePicker = WebDriverClass.getDriver().findElement( 940 | By.id("ui-datepicker-div")); 941 | List noOfColumns = datePicker 942 | .findElements(By.tagName("td")); 943 | 944 | // Loop will rotate till expected date not found. 945 | for (WebElement cell : noOfColumns) { 946 | // Select the date from date picker when condition match. 947 | if (cell.getText().equals(date)) { 948 | cell.findElement(By.linkText(date)).click(); 949 | break; 950 | } 951 | } 952 | 953 | } 954 | 955 | /** 956 | *Double clicks on the particular element 957 | */ 958 | public void doubleClick(MethodParameters model) { 959 | Actions action = new Actions(WebDriverClass.getDriver()); 960 | action.doubleClick((WebElement) model.getElement()).perform(); 961 | 962 | } 963 | 964 | /** 965 | *Mouse hovering on the element is performed 966 | */ 967 | public void singleMouseHover(MethodParameters model) { 968 | Actions action = new Actions(WebDriverClass.getDriver()); 969 | action.moveToElement((WebElement) model.getElement()).perform(); 970 | 971 | } 972 | 973 | /** 974 | *Right clicks on the element 975 | */ 976 | public void rightClick(MethodParameters model) { 977 | Actions action = new Actions(WebDriverClass.getDriver()); 978 | action.contextClick((WebElement) model.getElement()).perform(); 979 | 980 | } 981 | 982 | /** 983 | *Select the check boxes 984 | */ 985 | public void selectCheckBox(MethodParameters model) { 986 | boolean res = true; 987 | 988 | while (!model.getElement().get(0).isSelected()) { 989 | model.getElement().get(0).click(); 990 | if (model.getElement().get(0).isSelected()) { 991 | res = false; 992 | break; 993 | } 994 | 995 | } 996 | 997 | } 998 | /** 999 | *Un-check the check box 1000 | */ 1001 | public void deselectCheckBox(MethodParameters model) { 1002 | boolean res = true; 1003 | 1004 | while (model.getElement().get(0).isSelected()) { 1005 | model.getElement().get(0).click(); 1006 | if (!model.getElement().get(0).isSelected()) { 1007 | res = false; 1008 | break; 1009 | } 1010 | 1011 | } 1012 | 1013 | } 1014 | 1015 | /** 1016 | *Un-check the all check boxes 1017 | */ 1018 | public void deselectAllCheckbox(MethodParameters model) { 1019 | List list = model.getElement(); 1020 | 1021 | for (WebElement element : list) { 1022 | if (element.isSelected()) { 1023 | element.click(); 1024 | } 1025 | } 1026 | } 1027 | 1028 | /** 1029 | *Selects all the check boxes 1030 | */ 1031 | public void selectAllCheckbox(MethodParameters model) { 1032 | List list = model.getElement(); 1033 | 1034 | for (WebElement element : list) { 1035 | if (!element.isSelected()) { 1036 | element.click(); 1037 | } 1038 | } 1039 | } 1040 | 1041 | /** 1042 | *Verifies that the particular check box is selected 1043 | */ 1044 | public void verifyCheckBoxSelected(MethodParameters model) { 1045 | 1046 | Assert.assertTrue(model.getElement().get(0).isSelected()); 1047 | 1048 | } 1049 | 1050 | /** 1051 | *Verifies whether all the check box is selected 1052 | */ 1053 | public void verifyAllCheckBoxSelected(MethodParameters model) { 1054 | for (WebElement element : model.getElement()) { 1055 | Assert.assertTrue(element.isSelected(), "check box is selected"); 1056 | } 1057 | 1058 | } 1059 | 1060 | /** 1061 | *Verifies that all the check boxes is not selected 1062 | */ 1063 | public void verifyAllCheckBoxNotSelected(MethodParameters model) { 1064 | for (WebElement element : model.getElement()) { 1065 | Assert.assertFalse(element.isSelected(), "check box not selected"); 1066 | } 1067 | 1068 | } 1069 | 1070 | /** 1071 | * @param model 1072 | * File download from Auto It 1073 | */ 1074 | public void filedownloadAUTOIT(MethodParameters model){ 1075 | try { 1076 | Runtime.getRuntime().exec(model.getData()); 1077 | } catch (IOException e) { 1078 | // TODO Auto-generated catch block 1079 | e.printStackTrace(); 1080 | } 1081 | } 1082 | 1083 | } -------------------------------------------------------------------------------- /src/main/java/model/CapturedObjectPropModel.java: -------------------------------------------------------------------------------- 1 | package main.java.model; 2 | /** 3 | * The CapturedObjectPropModel class is used as a bean class to get the Captured 4 | * objects properties in the excel sheet 5 | * 6 | * @author Shilpashree_V 7 | * @version 1.0 8 | * @since February 2015 9 | * 10 | */ 11 | public class CapturedObjectPropModel { 12 | String page=null; 13 | String name=null; 14 | String property=null; 15 | String value=null; 16 | 17 | public void setPage(String page){ 18 | this.page=page; 19 | } 20 | 21 | public String getPage(){ 22 | return page; 23 | } 24 | 25 | public void setName(String name){ 26 | this.name=name; 27 | } 28 | 29 | public String getName(){ 30 | return name; 31 | } 32 | 33 | public void setProperty(String property){ 34 | this.property=property; 35 | } 36 | 37 | public String getProperty(){ 38 | return property; 39 | } 40 | 41 | public void setValue(String value){ 42 | this.value=value; 43 | } 44 | 45 | public String getValue(){ 46 | return value; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/model/MethodParameters.java: -------------------------------------------------------------------------------- 1 | package main.java.model; 2 | /** 3 | * The MethodParameters class is used as a bean class to get the details for actions 4 | * performed on the element 5 | * 6 | * @author Shilpashree_V 7 | * @version 1.0 8 | * @since February 2015 9 | * 10 | */ 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | import org.openqa.selenium.WebElement; 15 | 16 | public class MethodParameters { 17 | 18 | List elements = new ArrayList(); 19 | 20 | String methodType = null; 21 | String actionType = null; 22 | String data = null; 23 | String objectLocators = null; 24 | 25 | public List getElement() { 26 | return elements; 27 | } 28 | 29 | public void setElement(List element) { 30 | this.elements = element; 31 | } 32 | 33 | public String getMethodType() { 34 | return methodType; 35 | } 36 | 37 | public void setMethodType(String methodType) { 38 | this.methodType = methodType; 39 | } 40 | 41 | public String getActionType() { 42 | return actionType; 43 | } 44 | 45 | public void setActionType(String actionType) { 46 | this.actionType = actionType; 47 | } 48 | 49 | public String getData() { 50 | return data; 51 | } 52 | 53 | public void setData(String data) { 54 | this.data = data; 55 | } 56 | 57 | public String getObjectLocators() { 58 | return objectLocators; 59 | } 60 | 61 | public void setObjectLocators(String objectLocators) { 62 | this.objectLocators = objectLocators; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/model/TestCase.java: -------------------------------------------------------------------------------- 1 | package main.java.model; 2 | /** 3 | * The TestCase class is used as a bean class to get the details of each test case 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class TestCase { 14 | String testCaseName = null; 15 | List testStepId = new ArrayList(); 16 | List testSteps = new ArrayList(); 17 | List methodType = new ArrayList(); 18 | List ojectNameFromPropertiesFile = new ArrayList(); 19 | List actionType = new ArrayList(); 20 | String onFail = null; 21 | List testData = new ArrayList(); 22 | 23 | public String getTestCaseName() { 24 | return testCaseName; 25 | } 26 | 27 | public void setTestCaseName(String testCaseName) { 28 | this.testCaseName = testCaseName; 29 | } 30 | 31 | public void addTestSteps(String steps) { 32 | testSteps.add(steps); 33 | } 34 | 35 | public List getTestSteps() { 36 | return testSteps; 37 | } 38 | 39 | public void setMethodType(String methodype) { 40 | methodType.add(methodype); 41 | } 42 | 43 | public List getMethodType() { 44 | return methodType; 45 | } 46 | 47 | public void setObjectNameFromPropertiesFile(String name) { 48 | ojectNameFromPropertiesFile.add(name); 49 | } 50 | 51 | public List getObjectNameFromPropertiesFile() { 52 | return ojectNameFromPropertiesFile; 53 | } 54 | 55 | public void setActionType(String actiontype) { 56 | actionType.add(actiontype); 57 | } 58 | 59 | public List getActionType() { 60 | return actionType; 61 | } 62 | 63 | public void setTestData(String testdata) { 64 | testData.add(testdata); 65 | } 66 | 67 | public List getTestData() { 68 | return testData; 69 | } 70 | 71 | public List getTestStepId() { 72 | return testStepId; 73 | } 74 | 75 | public void setTestStepId(String tststepId) { 76 | testStepId.add(tststepId); 77 | } 78 | 79 | public String getOnFail() { 80 | return onFail; 81 | } 82 | 83 | public void setOnFail(String onFail) { 84 | this.onFail = onFail; 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/testng.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/util/ExcelAction.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The ExcelAction class is used to store the data from the excel into map 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.io.IOException; 11 | import java.util.ArrayList; 12 | import java.util.HashMap; 13 | import java.util.List; 14 | import java.util.Map; 15 | import main.java.model.CapturedObjectPropModel; 16 | import main.java.model.TestCase; 17 | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 18 | import org.openqa.selenium.WebDriver; 19 | import org.testng.Reporter; 20 | import main.java.actions.MethodType; 21 | 22 | public class ExcelAction { 23 | WebDriver driver; 24 | static ExcelLibrary excel = new ExcelLibrary(); 25 | static ReadConfigProperty config = new ReadConfigProperty(); 26 | static Map testCaseSheet = new HashMap(); 27 | 28 | static Map readFromConfigFile = new HashMap(); 29 | static Map testSuiteSheet = new HashMap(); 30 | static Map testDataSheet = new HashMap(); 31 | static Map capObjPropSheet = new HashMap(); 32 | 33 | static List listOfTestCases = new ArrayList(); 34 | int numberOfTimeExecution = 0; 35 | MethodType methodtype = new MethodType(); 36 | 37 | String testcasepth = "TestCasePath"; 38 | 39 | public static void main(String[] args) { 40 | ExcelAction action = new ExcelAction(); 41 | action.readCapturedObjectProperties(); 42 | action.readLocators("PAGE", "SEARCH_BOX"); 43 | } 44 | 45 | 46 | /** 47 | * Read test data sheet 48 | */ 49 | public void readTestDataSheet() { 50 | 51 | String sheetName; 52 | String pathOFFile = config.getConfigValues(testcasepth); 53 | List list = ExcelLibrary 54 | .getNumberOfSheetsinTestDataSheet(config 55 | .getConfigValues(testcasepth)); 56 | for (int i = 0; i < list.size(); i++) { 57 | sheetName = list.get(i); 58 | Map temp1 = new HashMap(); 59 | 60 | try { 61 | Reporter.log("sheetName" + sheetName + "----" 62 | + "sheetName, pathOFFile" + pathOFFile); 63 | List listColumnNames = ExcelLibrary.getColumnNames( 64 | sheetName, pathOFFile, 65 | ExcelLibrary.getColumns(sheetName, pathOFFile)); 66 | // iterate through columns in sheet 67 | for (int j = 0; j < listColumnNames.size(); j++) { 68 | // get Last Row for each Column 69 | int row = 1; 70 | List listColumnValues = new ArrayList(); 71 | do { 72 | listColumnValues.add(ExcelLibrary.readCell(row, j, 73 | sheetName, pathOFFile)); 74 | row++; 75 | } while ((ExcelLibrary.readCell(row, j, sheetName, 76 | pathOFFile)) != null); 77 | temp1.put((String) listColumnNames.get(j), listColumnValues); 78 | } 79 | listColumnNames.clear(); 80 | } catch (InvalidFormatException | IOException e) { 81 | // check after run 82 | MainTestNG.LOGGER.info("InvalidFormatException,IOException"+e); 83 | } 84 | testDataSheet.put(sheetName, temp1); 85 | } 86 | } 87 | 88 | /** 89 | * Iterate over each row in testcase sheet and pass the data to execute method in MethodType.java 90 | */ 91 | public void testSuiteIterate(String tcName) { 92 | MainTestNG.LOGGER.info("testSuiteIterate() called " + tcName); 93 | String key = tcName; 94 | 95 | TestCase temp = (TestCase) testCaseSheet.get(key); 96 | List testStepId = temp.getTestStepId(); 97 | Reporter.log("size====" + testStepId.size()); 98 | List dataColValues = null; 99 | int noOfExecution = 0; 100 | for (int i = 0; i < testStepId.size(); i++) { 101 | if (!(temp.getTestData().get(i).isEmpty())) { 102 | if (temp.getTestData().get(i).contains(".")) { 103 | 104 | String data = temp.getTestData().get(i); 105 | String[] testDataArray = data.split("\\."); 106 | 107 | dataColValues = getColumnValue(testDataArray); 108 | 109 | noOfExecution = dataColValues.size(); 110 | 111 | break; 112 | } 113 | } else { 114 | noOfExecution = 0; 115 | } 116 | } 117 | MainTestNG.LOGGER 118 | .info("columnValue addedd newly numberOfTimesExecution===" 119 | + dataColValues); 120 | MainTestNG.LOGGER.info("testCaseExecution==" + noOfExecution); 121 | 122 | if (noOfExecution != 0) { 123 | for (int execution = 0; execution < noOfExecution; execution++) { 124 | for (int i = 0; i < testStepId.size(); i++) { 125 | 126 | String methodType = temp.getMethodType().get(i); 127 | String objectLocators = temp 128 | .getObjectNameFromPropertiesFile().get(i); 129 | String actionType = temp.getActionType().get(i); 130 | 131 | // Data Sheet logic 132 | if (!(temp.getTestData().get(i).isEmpty())) { 133 | if (temp.getTestData().get(i).contains(".")) { 134 | 135 | String data = temp.getTestData().get(i); 136 | String[] testDataArray = data.split("\\."); 137 | 138 | List columnValue = getColumnValue(testDataArray); 139 | 140 | Reporter.log("column valueee======" + columnValue); 141 | Reporter.log("column value size===========" 142 | + columnValue.size()); 143 | try { 144 | Reporter.log("testCaseExecution======================" 145 | + noOfExecution); 146 | List list=readLocators(methodType,objectLocators); 147 | 148 | methodType=list.get(0); 149 | objectLocators=list.get(1); 150 | MainTestNG.LOGGER.info("methodType="+methodType); 151 | MainTestNG.LOGGER.info("objectLocators as name="+objectLocators); 152 | 153 | methodtype.methodExecutor(methodType, 154 | objectLocators, actionType, columnValue 155 | .get(execution).toString()); 156 | 157 | } catch (IndexOutOfBoundsException e) { 158 | String s = e.getMessage(); 159 | throw new IndexOutOfBoundsException( 160 | "data column is blank..Please enter value in datasheet" 161 | + s); 162 | } 163 | 164 | } 165 | 166 | if (execution == noOfExecution) { 167 | break; 168 | } 169 | } else { 170 | driver = WebDriverClass.getInstance(); 171 | 172 | List list=readLocators(methodType,objectLocators); 173 | methodType=list.get(0); 174 | objectLocators=list.get(1); 175 | MainTestNG.LOGGER.info("methodType="+methodType); 176 | methodtype.methodExecutor(methodType, objectLocators, 177 | actionType, null); 178 | } 179 | } 180 | if (execution == noOfExecution) { 181 | break; 182 | } 183 | } 184 | 185 | } else { 186 | for (int i = 0; i < testStepId.size(); i++) { 187 | 188 | String methodType = temp.getMethodType().get(i); 189 | String objectLocators = temp.getObjectNameFromPropertiesFile() 190 | .get(i); 191 | String actionType = temp.getActionType().get(i); 192 | 193 | driver = WebDriverClass.getInstance(); 194 | 195 | List list=readLocators(methodType,objectLocators); 196 | methodType=list.get(0); 197 | objectLocators=list.get(1); 198 | MainTestNG.LOGGER.info("methodType="+methodType); 199 | MainTestNG.LOGGER.info("objectLocators="+objectLocators); 200 | 201 | methodtype.methodExecutor(methodType, objectLocators, 202 | actionType, null); 203 | } 204 | } 205 | } 206 | 207 | private List getColumnValue(String[] testDataArray) { 208 | 209 | Map dataSheet = (HashMap) testDataSheet 210 | .get(testDataArray[0]); 211 | List coulmnValue = (ArrayList) dataSheet.get(testDataArray[1]); 212 | return coulmnValue; 213 | } 214 | 215 | /** 216 | * populate data to testSuitedata and listOfTestCases to be executed 217 | */ 218 | public void readTestSuite() { 219 | readFromConfigFile = config.readConfigFile(); 220 | 221 | for (String suiteName : readFromConfigFile.values()) { 222 | 223 | String testSuiteFilePath = config.getConfigValues("TestSuiteName"); 224 | System.out.println(testSuiteFilePath); 225 | List suiteSheets = ExcelLibrary 226 | .getNumberOfSheetsinSuite(testSuiteFilePath); 227 | System.out.println(suiteSheets.size()); 228 | 229 | for (int i = 0; i < suiteSheets.size(); i++) { 230 | String sheetName = suiteSheets.get(i); 231 | System.out.println(sheetName); 232 | if (suiteName.trim().equalsIgnoreCase(sheetName)) { 233 | Map temp1 = new HashMap(); 234 | try { 235 | for (int row = 1; row <= ExcelLibrary.getRows( 236 | sheetName, testSuiteFilePath); row++) { 237 | 238 | String testCaseName = ExcelLibrary.readCell(row, 0, 239 | suiteName.trim(), testSuiteFilePath); 240 | String testCaseState = ExcelLibrary.readCell(row, 241 | 1, suiteName.trim(), testSuiteFilePath); 242 | 243 | if (("YES").equalsIgnoreCase(testCaseState)) { 244 | listOfTestCases.add(testCaseName); 245 | } 246 | temp1.put(testCaseName, testCaseState); 247 | 248 | } 249 | Reporter.log("listOfTestCases=============*****************" 250 | + listOfTestCases); 251 | testSuiteSheet.put(suiteName, temp1); 252 | } catch (InvalidFormatException | IOException e) { 253 | 254 | MainTestNG.LOGGER.info("e"+e); 255 | 256 | } 257 | } 258 | } 259 | } 260 | 261 | } 262 | 263 | /** 264 | * Read the content of the excel testcase sheet and store the data in model and store this model in hashmap 265 | */ 266 | public void readTestCaseInExcel() { 267 | 268 | String testsheetnme = "TestCase_SheetName"; 269 | String testCasePath = config.getConfigValues(testcasepth); 270 | String testCaseSheetName = config.getConfigValues(testsheetnme); 271 | 272 | TestCase tc = null; 273 | try { 274 | for (int row = 1; row <= ExcelLibrary.getRows(testCaseSheetName, 275 | testCasePath); row++) { 276 | 277 | if (!(ExcelLibrary.readCell(row, 0, testCaseSheetName, 278 | testCasePath).isEmpty())) { 279 | 280 | tc = new TestCase(); 281 | tc.setTestCaseName(ExcelLibrary.readCell(row, 0, 282 | testCaseSheetName, testCasePath)); 283 | tc.setTestStepId(ExcelLibrary.readCell(row, 1, 284 | testCaseSheetName, testCasePath)); 285 | tc.setMethodType(ExcelLibrary.readCell(row, 3, 286 | testCaseSheetName, testCasePath)); 287 | tc.setObjectNameFromPropertiesFile(ExcelLibrary.readCell( 288 | row, 4, testCaseSheetName, testCasePath)); 289 | tc.setActionType(ExcelLibrary.readCell(row, 5, 290 | testCaseSheetName, testCasePath)); 291 | tc.setOnFail(ExcelLibrary.readCell(row, 6, 292 | testCaseSheetName, testCasePath)); 293 | tc.setTestData(ExcelLibrary.readCell(row, 7, 294 | testCaseSheetName, testCasePath)); 295 | testCaseSheet.put(ExcelLibrary.readCell(row, 0, 296 | testCaseSheetName, testCasePath), tc); 297 | 298 | } else { 299 | 300 | tc.setTestStepId(ExcelLibrary.readCell(row, 1, 301 | testCaseSheetName, testCasePath)); 302 | tc.setMethodType(ExcelLibrary.readCell(row, 3, 303 | testCaseSheetName, testCasePath)); 304 | tc.setObjectNameFromPropertiesFile(ExcelLibrary.readCell( 305 | row, 4, testCaseSheetName, testCasePath)); 306 | tc.setActionType(ExcelLibrary.readCell(row, 5, 307 | testCaseSheetName, testCasePath)); 308 | tc.setOnFail(ExcelLibrary.readCell(row, 6, 309 | testCaseSheetName, testCasePath)); 310 | tc.setTestData(ExcelLibrary.readCell(row, 7, 311 | testCaseSheetName, testCasePath)); 312 | } 313 | } 314 | } catch (InvalidFormatException e) { 315 | 316 | MainTestNG.LOGGER.info(e.getMessage()); 317 | } catch (IOException e) { 318 | 319 | MainTestNG.LOGGER.info(e.getMessage()); 320 | } 321 | } 322 | 323 | public void clean() { 324 | excel.clean(); 325 | 326 | } 327 | 328 | /** 329 | * Capture object properties in excel sheet 330 | */ 331 | public void readCapturedObjectProperties() { 332 | String testSheetName = "CapturedObjectProperties"; 333 | String testCasePath = config.getConfigValues(testcasepth); 334 | MainTestNG.LOGGER.info("testCasePath=="+testCasePath); 335 | try { 336 | int totrows = ExcelLibrary.getRows(testSheetName, testCasePath); 337 | MainTestNG.LOGGER.info("total rows=" + totrows); 338 | 339 | String prevPagename=""; 340 | Map pageInfo = null; 341 | for (int j = 1; j <= totrows; j++) { 342 | String pagename = ExcelLibrary.readCell(j, 0, testSheetName, 343 | testCasePath); 344 | 345 | 346 | if(prevPagename.equals(pagename)){ 347 | 348 | String page=ExcelLibrary.readCell(j, 0,testSheetName, testCasePath); 349 | String name=ExcelLibrary.readCell(j, 1, 350 | testSheetName, testCasePath); 351 | String property=ExcelLibrary.readCell(j, 2, 352 | testSheetName, testCasePath); 353 | String value=ExcelLibrary.readCell(j, 3, 354 | testSheetName, testCasePath); 355 | 356 | CapturedObjectPropModel capModel = new CapturedObjectPropModel(); 357 | capModel.setPage(page); 358 | capModel.setName(name); 359 | capModel.setProperty(property); 360 | capModel.setValue(value); 361 | MainTestNG.LOGGER.info(capModel.getPage()+" "+capModel.getName()+" "+capModel.getValue()+" "+capModel.getProperty()); 362 | pageInfo.put(name, capModel); 363 | 364 | 365 | }else{ 366 | if(prevPagename!=null){ 367 | capObjPropSheet.put(prevPagename, pageInfo); 368 | } 369 | pageInfo=new HashMap(); 370 | String page=ExcelLibrary.readCell(j, 0, 371 | testSheetName, testCasePath); 372 | String name=ExcelLibrary.readCell(j, 1, 373 | testSheetName, testCasePath); 374 | String property=ExcelLibrary.readCell(j, 2, 375 | testSheetName, testCasePath); 376 | String value=ExcelLibrary.readCell(j, 3, 377 | testSheetName, testCasePath); 378 | 379 | CapturedObjectPropModel capModel = new CapturedObjectPropModel(); 380 | capModel.setPage(pagename); 381 | capModel.setName(name); 382 | capModel.setProperty(property); 383 | capModel.setValue(value); 384 | pageInfo.put(name, capModel); 385 | prevPagename=pagename; 386 | } 387 | 388 | 389 | if(prevPagename!=null){ 390 | capObjPropSheet.put(prevPagename, pageInfo); 391 | } 392 | } 393 | 394 | 395 | } catch (InvalidFormatException e) { 396 | 397 | MainTestNG.LOGGER.info("InvalidFormatException="+e); 398 | 399 | } catch (IOException e) { 400 | 401 | e.printStackTrace(); 402 | } 403 | } 404 | 405 | /** 406 | * Capture object Locators in excel sheet 407 | */ 408 | public List readLocators(String page,String name) { 409 | MainTestNG.LOGGER.info(page); 410 | MainTestNG.LOGGER.info(name); 411 | Map temp=(Map) capObjPropSheet.get(page); 412 | List locators=new ArrayList<>(); 413 | MainTestNG.LOGGER.info("objects"+capObjPropSheet.get(page)); 414 | if(capObjPropSheet.get(page) != null){ 415 | 416 | MainTestNG.LOGGER.info("name"+temp.get(name)); 417 | CapturedObjectPropModel c=(CapturedObjectPropModel) temp.get(name); 418 | MainTestNG.LOGGER.info(c.getName()); 419 | MainTestNG.LOGGER.info("c.getPage()="+c.getPage()); 420 | 421 | if(c.getPage().equals(page) && c.getName().equals(name)){ 422 | locators.add(c.getProperty()); 423 | locators.add(c.getValue()); 424 | MainTestNG.LOGGER.info("locators"+locators); 425 | } 426 | } 427 | MainTestNG.LOGGER.info("size"+locators.size()); 428 | return locators; 429 | } 430 | } 431 | -------------------------------------------------------------------------------- /src/main/java/util/ExcelLibrary.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The ExcelLibrary class is used to read the data from the excel sheet 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.io.File; 11 | import java.io.FileNotFoundException; 12 | import java.io.IOException; 13 | import java.util.ArrayList; 14 | import java.util.HashMap; 15 | import java.util.Hashtable; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 20 | import org.apache.poi.ss.usermodel.Cell; 21 | import org.apache.poi.ss.usermodel.DataFormatter; 22 | import org.apache.poi.ss.usermodel.Row; 23 | import org.apache.poi.ss.usermodel.Sheet; 24 | import org.apache.poi.ss.usermodel.Workbook; 25 | import org.apache.poi.ss.usermodel.WorkbookFactory; 26 | import org.testng.Reporter; 27 | 28 | public class ExcelLibrary { 29 | static Map workbooktable = new HashMap(); 30 | public static Map dict = new Hashtable(); 31 | public static List list = new ArrayList(); 32 | static ReadConfigProperty config = new ReadConfigProperty(); 33 | 34 | /** 35 | * To get the excel sheet workbook 36 | */ 37 | public static Workbook getWorkbook(String path) { 38 | Workbook workbook = null; 39 | if (workbooktable.containsKey(path)) { 40 | workbook = workbooktable.get(path); 41 | } else { 42 | 43 | try { 44 | 45 | 46 | File file = new File(path); 47 | 48 | workbook = WorkbookFactory.create(file); 49 | 50 | workbooktable.put(path, workbook); 51 | 52 | } catch (FileNotFoundException e) { 53 | e.printStackTrace(); 54 | MainTestNG.LOGGER.info("FileNotFoundException" + e); 55 | } catch (InvalidFormatException e) { 56 | e.printStackTrace(); 57 | MainTestNG.LOGGER.info("InvalidFormatException" + e); 58 | } catch (IOException e) { 59 | e.printStackTrace(); 60 | MainTestNG.LOGGER.info("IOException" + e); 61 | } catch (Exception e) { 62 | e.printStackTrace(); 63 | } 64 | 65 | } 66 | return workbook; 67 | } 68 | 69 | /** 70 | * To get the number of sheets in excel suite 71 | */ 72 | public static List getNumberOfSheetsinSuite(String testPath) { 73 | List listOfSheets = new ArrayList(); 74 | 75 | Workbook workbook = getWorkbook(testPath); 76 | 77 | for (int i = 0; i < workbook.getNumberOfSheets(); i++) { 78 | listOfSheets.add(workbook.getSheetName(i)); 79 | } 80 | 81 | return listOfSheets; 82 | } 83 | 84 | /** 85 | * To get the number of sheets in test data sheet 86 | */ 87 | public static List getNumberOfSheetsinTestDataSheet(String testPath) { 88 | List listOfSheets = new ArrayList(); 89 | 90 | Workbook workbook = getWorkbook(testPath); 91 | for (int i = 0; i < workbook.getNumberOfSheets(); i++) { 92 | if (!(workbook.getSheetName(i)).equalsIgnoreCase(config 93 | .getConfigValues("TestCase_SheetName"))) { 94 | listOfSheets.add(workbook.getSheetName(i)); 95 | 96 | } 97 | } 98 | return listOfSheets; 99 | 100 | } 101 | 102 | /** 103 | * Get the total rows present in excel sheet 104 | */ 105 | public static int getRows(String testSheetName, String pathOfFile) 106 | throws InvalidFormatException, IOException { 107 | Workbook workbook = getWorkbook(pathOfFile); 108 | Reporter.log("getting total number of rows"); 109 | 110 | Sheet sheet = workbook.getSheet(testSheetName); 111 | 112 | return sheet.getLastRowNum(); 113 | 114 | } 115 | 116 | /** 117 | * Get the total columns inside excel sheet 118 | */ 119 | public static int getColumns(String testSheetName, String pathOfFile) 120 | throws InvalidFormatException, IOException { 121 | Workbook workbook = getWorkbook(pathOfFile); 122 | Reporter.log("getting total number of columns"); 123 | Sheet sheet = workbook.getSheet(testSheetName); 124 | return sheet.getRow(0).getLastCellNum(); 125 | 126 | } 127 | 128 | /** 129 | * Get the column names inside excel sheet 130 | */ 131 | public static List getColumnNames(String testSheetName, String pathOfFile, 132 | int j) throws InvalidFormatException, IOException { 133 | Workbook workbook = getWorkbook(pathOfFile); 134 | Sheet sheet = workbook.getSheet(testSheetName); 135 | 136 | for (int i = 0; i <= j; i++) { 137 | if (sheet.getRow(0).getCell(i) != null) { 138 | list.add(sheet.getRow(0).getCell(i).getStringCellValue() 139 | .toString()); 140 | } 141 | } 142 | 143 | return list; 144 | 145 | } 146 | 147 | /** 148 | * Get the total number of rows for each column inside excel sheet 149 | */ 150 | public static void getNumberOfRowsPerColumn(String testSheetName, 151 | String pathOfFile, int j) throws InvalidFormatException, 152 | IOException { 153 | Workbook workbook = getWorkbook(pathOfFile); 154 | Sheet sheet = workbook.getSheet(testSheetName); 155 | int totColumns = sheet.getRow(0).getLastCellNum(); 156 | for (int i = 0; i <= totColumns; i++) { 157 | if (sheet.getRow(0).getCell(i) != null) { 158 | list.add(sheet.getRow(0).getCell(i).getStringCellValue() 159 | .toString()); 160 | } 161 | } 162 | } 163 | 164 | /** 165 | * Read the content of the cell 166 | */ 167 | public static String readCell(int rowNum, int colNum, String testSheetName, 168 | String pathOfFile) { 169 | Workbook workbook; 170 | String cellValue = null; 171 | 172 | workbook = getWorkbook(pathOfFile); 173 | Sheet sheet = workbook.getSheet(testSheetName); 174 | Row row = sheet.getRow(rowNum); 175 | if (row != null) { 176 | Cell cell = row.getCell(colNum); 177 | if (cell != null) { 178 | DataFormatter dataFormatter = new DataFormatter(); 179 | String data = dataFormatter.formatCellValue(cell); 180 | cellValue = data; 181 | } 182 | } 183 | return cellValue; 184 | } 185 | 186 | /** 187 | * To clear the worktable and list 188 | */ 189 | public void clean() { 190 | workbooktable.clear(); 191 | list.clear(); 192 | } 193 | 194 | } -------------------------------------------------------------------------------- /src/main/java/util/ExecuteTestCases.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The ExecuteTestCases class is used to execute the test cases 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.awt.event.WindowEvent; 11 | import java.lang.reflect.Field; 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | import org.openqa.selenium.WebDriver; 16 | import org.openqa.selenium.chrome.ChromeDriver; 17 | import org.openqa.selenium.firefox.FirefoxDriver; 18 | import org.openqa.selenium.firefox.FirefoxProfile; 19 | import org.openqa.selenium.ie.InternetExplorerDriver; 20 | import org.testng.Assert; 21 | import org.testng.ITest; 22 | import org.testng.ITestResult; 23 | import org.testng.Reporter; 24 | import org.testng.annotations.AfterClass; 25 | import org.testng.annotations.AfterMethod; 26 | import org.testng.annotations.AfterSuite; 27 | import org.testng.annotations.BeforeClass; 28 | import org.testng.annotations.BeforeMethod; 29 | import org.testng.annotations.DataProvider; 30 | import org.testng.annotations.Test; 31 | import org.testng.internal.BaseTestMethod; 32 | 33 | /** 34 | * @author Shilpashree_V 35 | * From MainTesnNG, this class will be called 36 | */ 37 | public class ExecuteTestCases implements ITest { 38 | 39 | public static final String FIREFOX = "firefox"; 40 | public static final String IE = "IE"; 41 | public static final String CHROME = "chrome"; 42 | String cbrowserName = "BROWSER_NAME"; 43 | String curl = "URL"; 44 | String cdriver = "IEDRIVER"; 45 | ReadConfigProperty config = new ReadConfigProperty(); 46 | ReadElementLocators read = new ReadElementLocators(); 47 | static ExcelLibrary lib = new ExcelLibrary(); 48 | 49 | static WebDriver driver = null; 50 | static ExcelAction act = new ExcelAction(); 51 | 52 | static List list; 53 | protected String mTestCaseName = ""; 54 | /** 55 | * In this class, this is the first method to be executed. 56 | * Reading testsuite , test case sheet and data sheet and storing the values in Hashmap 57 | **/ 58 | @BeforeClass 59 | public void setup() { 60 | MainTestNG.LOGGER.info(ExecuteTestCases.class.getName() 61 | + " setup() method called"); 62 | 63 | 64 | act.readTestSuite(); 65 | act.readTestCaseInExcel(); 66 | act.readTestDataSheet(); 67 | act.readCapturedObjectProperties(); 68 | 69 | /** 70 | * Selecting which browser to be executed 71 | **/ 72 | String browserName = config.getConfigValues(cbrowserName); 73 | selectBrowser(browserName); 74 | 75 | MainTestNG.LOGGER.info(config.getConfigValues(curl)); 76 | /** 77 | * launching the url 78 | **/ 79 | driver.get(config.getConfigValues(curl)); 80 | WebDriverClass.setDriver(driver); 81 | } 82 | 83 | /** 84 | *Select the browser on which you want to execute tests 85 | **/ 86 | private void selectBrowser(String browserName) { 87 | 88 | switch (browserName) { 89 | 90 | case FIREFOX: 91 | 92 | firefoxProfile(); 93 | break; 94 | 95 | case IE: 96 | ie(); 97 | break; 98 | 99 | case CHROME: 100 | chrome(); 101 | break; 102 | 103 | default: 104 | firefoxProfile(); 105 | break; 106 | } 107 | 108 | } 109 | 110 | private void chrome() { 111 | 112 | System.setProperty("webdriver.chrome.driver", 113 | config.getConfigValues(cdriver)); 114 | 115 | driver = new ChromeDriver(); 116 | driver.manage().window().maximize(); 117 | } 118 | 119 | private void ie() { 120 | System.setProperty("webdriver.ie.driver", 121 | config.getConfigValues(cdriver)); 122 | 123 | driver = new InternetExplorerDriver(); 124 | driver.manage().window().maximize(); 125 | MainTestNG.LOGGER.info("IE launch"); 126 | 127 | } 128 | 129 | /** 130 | * Firefox profile will help in automatic download of files 131 | */ 132 | private void firefoxProfile() { 133 | 134 | FirefoxProfile profile = new FirefoxProfile(); 135 | profile.setPreference("browser.download.folderList", 1); 136 | profile.setPreference("browser.download.manager.showWhenStarting", 137 | false); 138 | 139 | profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 140 | "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); 141 | 142 | driver = new FirefoxDriver(profile); 143 | driver.manage().window().maximize(); 144 | 145 | } 146 | 147 | /** 148 | * To override the test case name in the report 149 | */ 150 | @BeforeMethod(alwaysRun = true) 151 | public void testData(Object[] testData) { 152 | String testCase = ""; 153 | if (testData != null && testData.length > 0) { 154 | String testName = null; 155 | // Check if test method has actually received required parameters 156 | for (Object tstname : testData) { 157 | if (tstname instanceof String) { 158 | testName = (String) tstname; 159 | break; 160 | } 161 | } 162 | testCase = testName; 163 | } 164 | this.mTestCaseName = testCase; 165 | 166 | } 167 | 168 | public String getTestName() { 169 | return this.mTestCaseName; 170 | } 171 | 172 | public void setTestName(String name) { 173 | 174 | this.mTestCaseName = name; 175 | 176 | } 177 | 178 | /** 179 | * All the test cases execution will start from here, which will take the input from the data provider 180 | */ 181 | @Test(dataProvider = "dp") 182 | public void testSample1(String testName) { 183 | MainTestNG.LOGGER.info(ExecuteTestCases.class.getName() 184 | + " @Test method called" + " " + testName); 185 | 186 | try { 187 | this.setTestName(testName); 188 | MainTestNG.LOGGER.info("testSuiteIterate() calling"); 189 | act.testSuiteIterate(testName); 190 | Reporter.log("ex" + getTestName()); 191 | 192 | } catch (Exception ex) { 193 | Assert.fail(); 194 | stack(ex); 195 | 196 | Reporter.log("exception in execute testCase====" + ex); 197 | 198 | } 199 | } 200 | 201 | @AfterMethod 202 | public void setResultTestName(ITestResult result) { 203 | try { 204 | BaseTestMethod bm = (BaseTestMethod) result.getMethod(); 205 | Field f = bm.getClass().getSuperclass() 206 | .getDeclaredField("m_methodName"); 207 | f.setAccessible(true); 208 | f.set(bm, mTestCaseName); 209 | Reporter.log(bm.getMethodName()); 210 | this.mTestCaseName = " "; 211 | 212 | } catch (Exception ex) { 213 | stack(ex); 214 | Reporter.log("ex" + ex.getMessage()); 215 | } 216 | } 217 | 218 | /** 219 | * Parameterization in testng 220 | */ 221 | @DataProvider(name = "dp") 222 | public Object[][] regression() { 223 | List list = (ArrayList) ExcelAction.listOfTestCases; 224 | Object[][] data = new Object[list.size()][1]; 225 | MainTestNG.LOGGER.info(ExecuteTestCases.class.getName() 226 | + " TestCases to be executed" + " " + data); 227 | for (int i = 0; i < data.length; i++) { 228 | data[i][0] = (String) list.get(i); 229 | } 230 | return data; 231 | } 232 | 233 | @AfterClass 234 | public void cleanup() { 235 | act.clean(); 236 | lib.clean(); 237 | SwingTest.frame.dispose(); 238 | SwingTest.frame.dispatchEvent(new WindowEvent(SwingTest.frame, 239 | WindowEvent.WINDOW_CLOSING)); 240 | 241 | 242 | } 243 | 244 | public static void stack(Exception e) { 245 | e.printStackTrace(); 246 | MainTestNG.LOGGER.info(ExecuteTestCases.class.getName() 247 | + " Exception occured" + " " + e.getCause()); 248 | } 249 | } 250 | -------------------------------------------------------------------------------- /src/main/java/util/Logger.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The Logger class is used to log 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | public class Logger { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/util/MainTestNG.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The MainTestNG class is used to execute the jar 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.io.IOException; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | import java.util.logging.FileHandler; 14 | import java.util.logging.Formatter; 15 | import java.util.logging.Handler; 16 | import java.util.logging.Logger; 17 | import java.util.logging.SimpleFormatter; 18 | import org.testng.TestListenerAdapter; 19 | import org.testng.TestNG; 20 | import org.testng.xml.XmlClass; 21 | import org.testng.xml.XmlSuite; 22 | import org.testng.xml.XmlTest; 23 | 24 | 25 | 26 | /** 27 | * @author Shilpashree_V 28 | * This is the class from where the execution gets started. 29 | */ 30 | 31 | //TestSuite 32 | public class MainTestNG { 33 | static Handler filehandler; 34 | static Formatter formatter = null; 35 | public static final Logger LOGGER = Logger.getLogger(MainTestNG.class 36 | .getName()); 37 | ReadConfigProperty config = new ReadConfigProperty(); 38 | static SwingTest swing; 39 | 40 | static String dir = "user.dir"; 41 | 42 | public static void main(String[] args) { 43 | /** 44 | * @author Shilpashree_V 45 | * license certificate generator-contact KK/Raghavendra for cert file generation 46 | */ 47 | 48 | 49 | 50 | try { 51 | 52 | filehandler = new FileHandler("./log.txt"); 53 | } catch (SecurityException e) { 54 | MainTestNG.LOGGER.info(e.getMessage()); 55 | } catch (IOException e) { 56 | MainTestNG.LOGGER.info(e.getMessage()); 57 | } 58 | 59 | LOGGER.addHandler(filehandler); 60 | formatter = new SimpleFormatter(); 61 | filehandler.setFormatter(formatter); 62 | LOGGER.info("Logger Name: " + LOGGER.getName()); 63 | 64 | ReadConfigProperty.configpath = System.getProperty(dir); 65 | 66 | /** 67 | * @author Shilpashree_V 68 | * SwingTest UI for properties file (Config.properties) 69 | */ 70 | //swing = new SwingTest(); 71 | MainTestNG test=new MainTestNG(); 72 | 73 | /** 74 | * @author Shilpashree_V 75 | * testNG execution starts here 76 | */ 77 | test.testng(); 78 | 79 | } 80 | 81 | 82 | /** 83 | * @author Shilpashree_V 84 | * adding listners, setting test-output folder 85 | * Mentioning the TestSuite Name 86 | */ 87 | public void testng() { 88 | //RegressionSuite 89 | System.out.println("testng"); 90 | TestListenerAdapter tla=new TestListenerAdapter(); 91 | TestNG myTestNG = new TestNG(); 92 | XmlSuite mySuite = new XmlSuite(); 93 | mySuite.setName("Sample Suite"); 94 | mySuite.addListener("org.uncommons.reportng.HTMLReporter"); 95 | mySuite.addListener("org.uncommons.reportng.JUnitXMLReporter"); 96 | 97 | mySuite.addListener("main.java.util.TestListener"); 98 | 99 | myTestNG.setOutputDirectory("test-output"); 100 | 101 | 102 | XmlTest myTest = new XmlTest(mySuite); 103 | myTest.setName("Sample Test"); 104 | List myClasses = new ArrayList(); 105 | myClasses.add(new XmlClass("main.java.util.ExecuteTestCases")); 106 | myTest.setXmlClasses(myClasses); 107 | List myTests = new ArrayList(); 108 | myTests.add(myTest); 109 | mySuite.setTests(myTests); 110 | List mySuites = new ArrayList(); 111 | 112 | mySuites.add(mySuite); 113 | myTestNG.setXmlSuites(mySuites); 114 | myTestNG.setUseDefaultListeners(true); 115 | /** 116 | * @author Shilpashree_V 117 | * testng run method 118 | */ 119 | myTestNG.run(); 120 | 121 | /** 122 | * @author Shilpashree_V 123 | * Report generation--Generating XSLT report from ReportNG report 124 | */ 125 | Report.report(); 126 | } 127 | 128 | } 129 | -------------------------------------------------------------------------------- /src/main/java/util/ReadConfigProperty.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The ReadConfigProperty class is used to read the configuration details from 4 | * config file 5 | * 6 | * @author Shilpashree_V 7 | * @version 1.0 8 | * @since February 2015 9 | * 10 | */ 11 | import java.io.BufferedReader; 12 | import java.io.File; 13 | import java.io.FileInputStream; 14 | import java.io.FileNotFoundException; 15 | import java.io.IOException; 16 | import java.io.InputStreamReader; 17 | import java.util.ArrayList; 18 | import java.util.HashMap; 19 | import java.util.List; 20 | import java.util.Map; 21 | import java.util.Properties; 22 | 23 | public class ReadConfigProperty { 24 | static List contentFromConfigFile = new ArrayList(); 25 | static List listOfSuiteNames = new ArrayList(); 26 | static Map map = new HashMap(); 27 | public static String configpath; 28 | Properties properties = null; 29 | 30 | public String getConfigValues(String elementValue) { 31 | 32 | if (properties == null) { 33 | try { 34 | 35 | File file = new File(configpath + "\\config.properties"); 36 | 37 | 38 | FileInputStream fileInput = new FileInputStream(file); 39 | properties = new Properties(); 40 | properties.load(fileInput); 41 | 42 | } catch (IOException e) { 43 | 44 | MainTestNG.LOGGER.severe("IOException"+e.getMessage()); 45 | } 46 | } 47 | return properties.getProperty(elementValue); 48 | } 49 | 50 | public Map readConfigFile() { 51 | String line; 52 | if (properties == null) { 53 | try { 54 | File file = new File(configpath + "\\config.properties"); 55 | 56 | FileInputStream fileInput = new FileInputStream(file); 57 | 58 | BufferedReader buffReader = new BufferedReader( 59 | new InputStreamReader(fileInput)); 60 | while ((line = buffReader.readLine()) != null) { 61 | 62 | String[] temp3 = line.split("="); 63 | if (temp3.length > 1) { 64 | String key = temp3[0]; 65 | String value = temp3[1]; 66 | 67 | if (key.contains("TestSuite_SHEETNAME")) { 68 | map.put(key, value); 69 | } 70 | 71 | } 72 | 73 | } 74 | } catch (FileNotFoundException e) { 75 | e.printStackTrace(); 76 | 77 | MainTestNG.LOGGER.getName(); 78 | MainTestNG.LOGGER.severe(e.getLocalizedMessage()); 79 | MainTestNG.LOGGER.severe(e.getMessage()); 80 | } catch (IOException e) { 81 | e.printStackTrace(); 82 | MainTestNG.LOGGER.getName(); 83 | MainTestNG.LOGGER.severe(e.getLocalizedMessage()); 84 | MainTestNG.LOGGER.severe(e.getMessage()); 85 | } 86 | 87 | } 88 | return map; 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/util/ReadElementLocators.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The ReadElementLocators class is used to read the element locators 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.io.File; 11 | import java.io.FileInputStream; 12 | import java.io.IOException; 13 | 14 | import java.util.Properties; 15 | 16 | public class ReadElementLocators { 17 | Properties properties = null; 18 | ReadConfigProperty config = new ReadConfigProperty(); 19 | 20 | /*public String getElementLocatorsValue(String elementValue) { 21 | if (properties == null) { 22 | try { 23 | 24 | String propFileName = config 25 | .getConfigValues("ELEMENT_LOCATORS"); 26 | File file = new File(propFileName); 27 | FileInputStream fileInput = new FileInputStream(file); 28 | properties = new Properties(); 29 | properties.load(fileInput); 30 | 31 | } catch (IOException e) { 32 | 33 | MainTestNG.LOGGER.severe(e.getMessage()); 34 | } 35 | } 36 | return properties.getProperty(elementValue); 37 | }*/ 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/util/ReadExcelSuite.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The ReadExcelSuite class is used to read excel suite 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.io.File; 11 | import java.io.IOException; 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 16 | import org.apache.poi.ss.usermodel.Cell; 17 | import org.apache.poi.ss.usermodel.Row; 18 | import org.apache.poi.ss.usermodel.Sheet; 19 | import org.apache.poi.ss.usermodel.Workbook; 20 | import org.apache.poi.ss.usermodel.WorkbookFactory; 21 | 22 | public class ReadExcelSuite { 23 | public static void main(String[] args) { 24 | ReadExcelSuite read = new ReadExcelSuite(); 25 | read.getNumberOfSheetsinSuite(); 26 | } 27 | 28 | public List getNumberOfSheetsinSuite() { 29 | List listOfSheets = new ArrayList(); 30 | 31 | try { 32 | Workbook workbook = WorkbookFactory 33 | .create(new File( 34 | "D:\\Selenium Framework development\\TestSuitePrototype.xlsx")); 35 | for (int i = 0; i < workbook.getNumberOfSheets(); i++) { 36 | 37 | } 38 | 39 | } catch (InvalidFormatException | IOException e) { 40 | 41 | MainTestNG.LOGGER.severe(e.getMessage()); 42 | } 43 | return listOfSheets; 44 | 45 | } 46 | 47 | public static String readCell(int rowNum, int ColNum) { 48 | Workbook workbook; 49 | String cellValue = null; 50 | try { 51 | workbook = WorkbookFactory.create(new File( 52 | "D:\\Selenium Framework development\\TestCaseSheet.xlsx")); 53 | Sheet sheet = workbook.getSheetAt(0); 54 | Row row = sheet.getRow(rowNum); 55 | Cell cell = row.getCell(ColNum); 56 | cellValue = cell.getStringCellValue(); 57 | } catch (InvalidFormatException | IOException e) { 58 | 59 | MainTestNG.LOGGER.severe("InvalidFormatException"); 60 | } 61 | return cellValue; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/util/Report.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The Report class is used to generate the report 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.io.FileNotFoundException; 11 | import java.io.FileOutputStream; 12 | 13 | import javax.xml.stream.util.StreamReaderDelegate; 14 | import javax.xml.transform.*; 15 | import javax.xml.transform.stream.StreamResult; 16 | import javax.xml.transform.stream.StreamSource; 17 | 18 | public class Report { 19 | static void report(){ 20 | 21 | 22 | try { 23 | 24 | System.setProperty("javax.xml.transform.TransformerFactory", 25 | "net.sf.saxon.TransformerFactoryImpl"); 26 | 27 | TransformerFactory transFact= TransformerFactory.newInstance(); 28 | Transformer trans=transFact.newTransformer(new StreamSource("xml/testng-results.xsl") ); 29 | 30 | trans.setParameter("testNgXslt.outputDir", ReadConfigProperty.configpath+"\\test-output"); 31 | 32 | trans.setParameter("testNgXslt.cssFile", "xml/custom.css"); 33 | trans.setParameter("testNgXslt.showRuntimeTotals", "true"); 34 | trans.setParameter("testNgXslt.sortTestCaseLinks", "true"); 35 | trans.setParameter("testNgXslt.testDetailsFilter", "FAIL,PASS,SKIP,CONF,BY_CLASS"); 36 | 37 | FileOutputStream os=new FileOutputStream(ReadConfigProperty.configpath+"\\test-output"+"\\report.html"); 38 | trans.transform(new StreamSource(ReadConfigProperty.configpath+"\\test-output"+"\\testng-results.xml"), new StreamResult(os)); 39 | 40 | } catch (TransformerConfigurationException e) { 41 | MainTestNG.LOGGER.info("TransformerConfigurationException"+e); 42 | 43 | } catch (FileNotFoundException e) { 44 | MainTestNG.LOGGER.info("FileNotFoundException"+e); 45 | } catch (TransformerException e) { 46 | MainTestNG.LOGGER.info("TransformerException"+e); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/util/SwingTest.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The SwingTest class is used to provide UI capabilities 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.awt.Color; 11 | import java.awt.Dimension; 12 | import java.awt.GridBagConstraints; 13 | import java.awt.GridBagLayout; 14 | import java.awt.Insets; 15 | import java.awt.event.ActionEvent; 16 | import java.awt.event.ActionListener; 17 | import java.awt.event.WindowEvent; 18 | import java.io.FileInputStream; 19 | import java.io.FileOutputStream; 20 | import java.io.IOException; 21 | import java.util.Properties; 22 | 23 | import javax.swing.BorderFactory; 24 | import javax.swing.JButton; 25 | import javax.swing.JComboBox; 26 | import javax.swing.JFileChooser; 27 | import javax.swing.JFrame; 28 | import javax.swing.JLabel; 29 | import javax.swing.JOptionPane; 30 | import javax.swing.JPanel; 31 | import javax.swing.JTextField; 32 | import javax.swing.SwingUtilities; 33 | import javax.swing.UIManager; 34 | import javax.swing.UnsupportedLookAndFeelException; 35 | 36 | /** 37 | * @author Shilpashree_V 38 | * UI for config.properties file 39 | */ 40 | @SuppressWarnings("serial") 41 | public class SwingTest extends JPanel { 42 | MainTestNG test = new MainTestNG(); 43 | 44 | String configFile = "config.properties"; 45 | 46 | String browse = "Browse"; 47 | public static final int MODE_OPEN = 1; 48 | public static final int MODE_SAVE = 2; 49 | JButton ltestcasebrowse, ltestsuitebrowse, belementLoc, bbrowserDriver, 50 | bClear, bClose, bscreenshot, bGenerate; 51 | JTextField ftestcase, ftestsuite, felementLoc, fbrowserDriver, fscreenshot, 52 | fURL; 53 | JComboBox fbrowserName; 54 | static JFrame frame = new JFrame(); 55 | JFileChooser fileChooser = new JFileChooser(); 56 | GridBagConstraints grid = new GridBagConstraints(); 57 | JPanel controlPanel = new JPanel(); 58 | 59 | public SwingTest() { 60 | 61 | try { 62 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 63 | 64 | } catch (ClassNotFoundException e) { 65 | MainTestNG.LOGGER.info("Exception" + e.getCause()); 66 | } catch (Exception e) { 67 | MainTestNG.LOGGER.info("Exception" + e.getMessage()); 68 | } 69 | SwingUtilities.updateComponentTreeUI(this); 70 | 71 | frame.setSize(600, 400); 72 | frame.setLocationRelativeTo(null); 73 | frame.setTitle("ISAF"); 74 | 75 | controlPanel.setBorder(BorderFactory.createLineBorder(Color.red)); 76 | controlPanel.setBackground(Color.white); 77 | 78 | controlPanel.setLayout(new GridBagLayout()); 79 | 80 | grid.gridx = 0; 81 | grid.gridy = 0; 82 | grid.insets = new Insets(5, 20, 5, 5); 83 | 84 | JLabel ltestcase = new JLabel("Testcase: "); 85 | 86 | controlPanel.add(ltestcase, grid); 87 | 88 | ftestcase = new JTextField(); 89 | ftestcase.setPreferredSize(new Dimension(250, 20)); 90 | ftestcase.setMinimumSize(new Dimension(250, 20)); 91 | ftestcase.setMaximumSize(new Dimension(250, 20)); 92 | grid.gridx = 1; 93 | grid.gridy = 0; 94 | grid.insets = new Insets(5, 20, 5, 5); 95 | ftestcase 96 | .setToolTipText("Path of excel file in which testcases are written and file type should be .xlsx"); 97 | controlPanel.add(ftestcase, grid); 98 | ltestcase.setLabelFor(ftestcase); 99 | 100 | ltestcasebrowse = new JButton(browse); 101 | addbutton(ltestcasebrowse, ftestcase); 102 | grid.gridx = 2; 103 | grid.gridy = 0; 104 | grid.insets = new Insets(5, 20, 5, 5); 105 | controlPanel.add(ltestcasebrowse, grid); 106 | 107 | JLabel ltestsuite = new JLabel("Testsuite: "); 108 | grid.gridx = 0; 109 | grid.gridy = 1; 110 | grid.insets = new Insets(5, 20, 5, 5); 111 | controlPanel.add(ltestsuite, grid); 112 | 113 | ftestsuite = new JTextField(); 114 | ftestsuite.setPreferredSize(new Dimension(250, 20)); 115 | ftestsuite.setMinimumSize(new Dimension(250, 20)); 116 | ftestsuite.setMaximumSize(new Dimension(250, 20)); 117 | ftestsuite 118 | .setToolTipText("Path of excel file- TestSuite and file type should be .xlsx"); 119 | grid.gridx = 1; 120 | grid.gridy = 1; 121 | grid.insets = new Insets(5, 20, 5, 5); 122 | 123 | controlPanel.add(ftestsuite, grid); 124 | ltestsuite.setLabelFor(ftestsuite); 125 | 126 | ltestsuitebrowse = new JButton(browse); 127 | addbutton(ltestsuitebrowse, ftestsuite); 128 | grid.gridx = 2; 129 | grid.gridy = 1; 130 | controlPanel.add(ltestsuitebrowse, grid); 131 | 132 | JLabel lelementLoc = new JLabel("Element Locators: "); 133 | grid.gridx = 0; 134 | grid.gridy = 2; 135 | controlPanel.add(lelementLoc, grid); 136 | 137 | felementLoc = new JTextField(); 138 | felementLoc.setPreferredSize(new Dimension(250, 20)); 139 | felementLoc.setMinimumSize(new Dimension(250, 20)); 140 | felementLoc.setMaximumSize(new Dimension(250, 20)); 141 | grid.gridx = 1; 142 | grid.gridy = 2; 143 | felementLoc 144 | .setToolTipText("Path of elemet locators properties file and file type should be .properties"); 145 | controlPanel.add(felementLoc, grid); 146 | lelementLoc.setLabelFor(felementLoc); 147 | 148 | belementLoc = new JButton(browse); 149 | addbutton(belementLoc, felementLoc); 150 | grid.gridx = 2; 151 | grid.gridy = 2; 152 | controlPanel.add(belementLoc, grid); 153 | 154 | JLabel lbrowserName = new JLabel("Browser Name"); 155 | grid.gridx = 0; 156 | grid.gridy = 3; 157 | controlPanel.add(lbrowserName, grid); 158 | 159 | String[] brNamesList = { "firefox", "IE", "chrome" }; 160 | fbrowserName = new JComboBox(brNamesList); 161 | fbrowserName.setBackground(Color.WHITE); 162 | 163 | 164 | fbrowserName.setPreferredSize(new Dimension(250, 20)); 165 | grid.gridx = 1; 166 | grid.gridy = 3; 167 | 168 | fbrowserName.setToolTipText("Enter browser Eg: firefox,IE,chrome"); 169 | controlPanel.add(fbrowserName, grid); 170 | 171 | JLabel lbrowserDriver = new JLabel("Browser Driver: "); 172 | grid.gridx = 0; 173 | grid.gridy = 4; 174 | controlPanel.add(lbrowserDriver, grid); 175 | 176 | fbrowserDriver = new JTextField(); 177 | fbrowserDriver.setPreferredSize(new Dimension(250, 20)); 178 | grid.gridx = 1; 179 | grid.gridy = 4; 180 | fbrowserDriver 181 | .setToolTipText("exe of specific browser server (IEDriverServer.exe, ChormeDriverServer.exe) and file type should be .exe"); 182 | controlPanel.add(fbrowserDriver, grid); 183 | lbrowserDriver.setLabelFor(fbrowserDriver); 184 | 185 | bbrowserDriver = new JButton(browse); 186 | addbutton(bbrowserDriver, fbrowserDriver); 187 | grid.gridx = 2; 188 | grid.gridy = 4; 189 | controlPanel.add(bbrowserDriver, grid); 190 | 191 | JLabel lscreenshot = new JLabel("Screenshot: "); 192 | grid.gridx = 0; 193 | grid.gridy = 5; 194 | controlPanel.add(lscreenshot, grid); 195 | 196 | fscreenshot = new JTextField(); 197 | fscreenshot.setPreferredSize(new Dimension(250, 20)); 198 | fscreenshot 199 | .setToolTipText("Screenshots on failure of testcase to be saved"); 200 | grid.gridx = 1; 201 | grid.gridy = 5; 202 | 203 | controlPanel.add(fscreenshot, grid); 204 | lscreenshot.setLabelFor(fscreenshot); 205 | 206 | bscreenshot = new JButton(browse); 207 | addbutton(bscreenshot, fscreenshot); 208 | grid.gridx = 2; 209 | grid.gridy = 5; 210 | controlPanel.add(bscreenshot, grid); 211 | 212 | JLabel lURL = new JLabel("URL: "); 213 | grid.gridx = 0; 214 | grid.gridy = 6; 215 | controlPanel.add(lURL, grid); 216 | 217 | fURL = new JTextField(); 218 | fURL.setPreferredSize(new Dimension(250, 20)); 219 | fURL.setToolTipText("URL of the application to be tested"); 220 | grid.gridx = 1; 221 | grid.gridy = 6; 222 | 223 | controlPanel.add(fURL, grid); 224 | 225 | lURL.setLabelFor(fURL); 226 | 227 | execute(); 228 | 229 | bClear = new JButton("Clear"); 230 | clearButton(bClear); 231 | grid.gridx = 1; 232 | grid.gridy = 15; 233 | controlPanel.add(bClear, grid); 234 | 235 | frame.getContentPane().add(controlPanel); 236 | 237 | frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 238 | frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE); 239 | 240 | frame.dispose(); 241 | frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); 242 | frame.setVisible(true); 243 | } 244 | 245 | private void execute() { 246 | 247 | bGenerate = new JButton("Execute"); 248 | generatebutton(bGenerate); 249 | grid.gridx = 3; 250 | grid.gridy = 15; 251 | controlPanel.add(bGenerate, grid); 252 | 253 | } 254 | 255 | private void clearButton(JButton bClear) { 256 | bClear.addActionListener(new ActionListener() { 257 | 258 | @Override 259 | public void actionPerformed(ActionEvent e) { 260 | ftestcase.setText(""); 261 | ftestsuite.setText(""); 262 | fbrowserDriver.setText(""); 263 | fscreenshot.setText(""); 264 | felementLoc.setText(""); 265 | fURL.setText(""); 266 | 267 | } 268 | }); 269 | 270 | } 271 | 272 | private void addbutton(JButton ltestcasebrowse2, final JTextField textField) { 273 | ltestcasebrowse2.addActionListener(new ActionListener() { 274 | 275 | @Override 276 | public void actionPerformed(ActionEvent e) { 277 | buttonActionPerformed(e, textField); 278 | 279 | } 280 | 281 | }); 282 | } 283 | 284 | private void generatebutton(final JButton b) { 285 | b.addActionListener(new ActionListener() { 286 | 287 | @Override 288 | public void actionPerformed(ActionEvent e) { 289 | generatePropFile(e, b); 290 | 291 | } 292 | 293 | }); 294 | } 295 | 296 | private void buttonActionPerformed(ActionEvent evt, JTextField textField) { 297 | if (textField.equals(fscreenshot)) { 298 | fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 299 | 300 | textField.setText(fileChooser.getCurrentDirectory() 301 | .getAbsolutePath()); 302 | } 303 | if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { 304 | textField.setText(fileChooser.getSelectedFile().getAbsolutePath()); 305 | } 306 | 307 | } 308 | 309 | private void generatePropFile(ActionEvent evt, JButton b) { 310 | 311 | boolean status = true; 312 | 313 | if (!ftestcase.getText().endsWith(".xlsx") 314 | || ftestcase.getText().length() == 0) { 315 | status = false; 316 | 317 | JOptionPane.showMessageDialog(ftestcase, 318 | "Field is blank or invalid file type"); 319 | 320 | } else if (!ftestsuite.getText().endsWith(".xlsx") 321 | || ftestcase.getText().length() == 0) { 322 | status = false; 323 | 324 | JOptionPane 325 | .showMessageDialog(ftestsuite, 326 | "testsuite: invalid file type.Please add file with extension .xlsx"); 327 | } else if (!felementLoc.getText().endsWith(".properties")) { 328 | status = false; 329 | 330 | JOptionPane 331 | .showMessageDialog( 332 | felementLoc, 333 | "Element Locators: invalid file type.Please add file with extension .properties"); 334 | } else if (!fbrowserDriver.getText().endsWith(".exe")) { 335 | status = false; 336 | 337 | JOptionPane 338 | .showMessageDialog(fbrowserDriver, 339 | "BrowserDriver: invalid file type.Please add file with extension .exe"); 340 | } else if (fURL.getText().length() == 0) { 341 | status = false; 342 | 343 | JOptionPane.showMessageDialog(fURL, "Please enter the url"); 344 | } 345 | 346 | String testcase = "TestCasePath"; 347 | String testsuite = "TestSuiteName"; 348 | String locators = "ELEMENT_LOCATORS"; 349 | String browserdriver = "IEDRIVER"; 350 | String url = "URL"; 351 | String screenshot = "SCREENSHOTS"; 352 | String browsername = "BROWSER_NAME"; 353 | if (ftestcase.getText().length() == 0) { 354 | 355 | FileInputStream in; 356 | try { 357 | in = new FileInputStream(configFile); 358 | Properties props = new Properties(); 359 | props.load(in); 360 | 361 | if (props.getProperty(testcase) != null) { 362 | ftestcase.setText(props.getProperty(testcase)); 363 | } 364 | if (props.getProperty(testsuite) != null 365 | && ftestsuite.getText().length() == 0) { 366 | ftestsuite.setText(props.getProperty(testsuite)); 367 | } 368 | if (props.getProperty(locators) != null 369 | && felementLoc.getText().length() == 0) { 370 | felementLoc.setText(props.getProperty(locators)); 371 | } 372 | 373 | if (props.getProperty(browserdriver) != null 374 | && fbrowserDriver.getText().length() == 0) { 375 | fbrowserDriver.setText(props.getProperty(browserdriver)); 376 | } 377 | 378 | fURL.setText(props.getProperty(url)); 379 | fscreenshot.setText(props.getProperty(screenshot)); 380 | frame.validate(); 381 | frame.repaint(); 382 | in.close(); 383 | 384 | } catch (IOException e1) { 385 | MainTestNG.LOGGER.info("IOException"+ e1.getMessage()); 386 | 387 | } 388 | 389 | } else { 390 | try { 391 | 392 | FileInputStream in = new FileInputStream(configFile); 393 | Properties props = new Properties(); 394 | props.load(in); 395 | in.close(); 396 | 397 | FileOutputStream out = new FileOutputStream(configFile); 398 | 399 | props.setProperty(testcase, ftestcase.getText()); 400 | 401 | if (ftestsuite.getText().length() != 0) { 402 | props.setProperty(testsuite, ftestsuite.getText()); 403 | } 404 | 405 | if (felementLoc.getText().length() != 0) { 406 | props.setProperty(locators, felementLoc.getText()); 407 | } 408 | if (fbrowserName.getSelectedItem().toString() != null) { 409 | props.setProperty(browsername, fbrowserName 410 | .getSelectedItem().toString()); 411 | } 412 | 413 | if (fbrowserDriver.getText().length() != 0) { 414 | 415 | props.setProperty(browserdriver, fbrowserDriver.getText()); 416 | 417 | } 418 | if (fURL.getText().length() != 0) { 419 | props.setProperty(url, fURL.getText()); 420 | } 421 | if (fscreenshot.getText().length() != 0) { 422 | props.setProperty(screenshot, fscreenshot.getText()); 423 | } 424 | 425 | props.store(out, null); 426 | out.close(); 427 | 428 | } catch (Exception z) { 429 | MainTestNG.LOGGER.info("e"+z); 430 | 431 | } 432 | 433 | } 434 | 435 | if (status) { 436 | test.testng(); 437 | } 438 | 439 | } 440 | 441 | public String getSelectedFilePath() { 442 | return ftestcase.getText(); 443 | } 444 | 445 | public JFileChooser getFileChooser() { 446 | return this.fileChooser; 447 | } 448 | } 449 | -------------------------------------------------------------------------------- /src/main/java/util/TestListener.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The TestListener class is used to help in generating html Report 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.io.File; 11 | import java.io.IOException; 12 | import java.text.DateFormat; 13 | import java.text.SimpleDateFormat; 14 | import java.util.Date; 15 | 16 | import org.apache.commons.io.FileUtils; 17 | import org.openqa.selenium.OutputType; 18 | import org.openqa.selenium.TakesScreenshot; 19 | import org.openqa.selenium.WebDriver; 20 | import org.testng.ITestContext; 21 | import org.testng.ITestListener; 22 | import org.testng.ITestResult; 23 | import org.testng.Reporter; 24 | 25 | import main.java.util.WebDriverClass; 26 | 27 | /** 28 | * @author Shilpashree_V 29 | * 30 | */ 31 | public class TestListener implements ITestListener { 32 | WebDriver driver = null; 33 | 34 | /* (non-Javadoc) 35 | * @see org.testng.ITestListener#onTestFailure(org.testng.ITestResult) 36 | * capture the screenshot of a page on failure 37 | */ 38 | @Override 39 | public void onTestFailure(ITestResult result) { 40 | 41 | printTestResults(result); 42 | String methodName = result.getName().toString().trim(); 43 | takeScreenShot(methodName); 44 | 45 | } 46 | 47 | 48 | /** 49 | * @param methodName 50 | * Capturing the screenshot of a page 51 | */ 52 | public void takeScreenShot(String methodName) { 53 | 54 | driver = WebDriverClass.getDriver(); 55 | File scrFile = ((TakesScreenshot) driver) 56 | .getScreenshotAs(OutputType.FILE); 57 | 58 | try { 59 | DateFormat dateFormat = new SimpleDateFormat( 60 | "dd_MMM_yyyy__hh_mm_ssaa"); 61 | Date date = new Date(); 62 | 63 | FileUtils.copyFile(scrFile, new File(ReadConfigProperty.configpath 64 | + "\\SCREENSHOT\\OnFailure" + "\\" + methodName + "__" 65 | + dateFormat.format(date) + ".png")); 66 | 67 | } catch (IOException e) { 68 | MainTestNG.LOGGER.severe("Io Exception occured"); 69 | } 70 | } 71 | 72 | @Override 73 | public void onFinish(ITestContext arg0) { 74 | Reporter.log("About to end executing Test " + arg0.getName(), true); 75 | 76 | } 77 | 78 | @Override 79 | public void onStart(ITestContext arg0) { 80 | Reporter.log("About to begin executing Test " + arg0.getName(), true); 81 | 82 | } 83 | 84 | @Override 85 | public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) { 86 | Reporter.log("About to end executing Test " + arg0.getName(), true); 87 | } 88 | 89 | @Override 90 | public void onTestSkipped(ITestResult arg0) { 91 | Reporter.log("About to end executing Test " + arg0.getName(), true); 92 | } 93 | 94 | @Override 95 | public void onTestStart(ITestResult arg0) { 96 | Reporter.log("About to end executing Test " + arg0.getName(), true); 97 | } 98 | 99 | @Override 100 | public void onTestSuccess(ITestResult arg0) { 101 | printTestResults(arg0); 102 | 103 | } 104 | 105 | private void printTestResults(ITestResult result) { 106 | 107 | if (result.getParameters().length != 0) { 108 | 109 | String params = null; 110 | 111 | for (Object parameter : result.getParameters()) { 112 | 113 | params += parameter.toString() + ","; 114 | 115 | } 116 | 117 | } 118 | 119 | String status = null; 120 | 121 | switch (result.getStatus()) { 122 | 123 | case ITestResult.SUCCESS: 124 | 125 | status = "Pass"; 126 | 127 | break; 128 | 129 | case ITestResult.FAILURE: 130 | 131 | status = "Failed"; 132 | 133 | break; 134 | 135 | case ITestResult.SKIP: 136 | 137 | status = "Skipped"; 138 | 139 | } 140 | 141 | Reporter.log("Test Status: " + status, true); 142 | 143 | } 144 | 145 | } -------------------------------------------------------------------------------- /src/main/java/util/WebDriverClass.java: -------------------------------------------------------------------------------- 1 | package main.java.util; 2 | /** 3 | * The WebDriverClass class is used to generate a single instance of driver to 4 | * launch the browser 5 | * 6 | * @author Shilpashree_V 7 | * @version 1.0 8 | * @since February 2015 9 | * 10 | */ 11 | import java.util.concurrent.TimeUnit; 12 | 13 | import org.openqa.selenium.WebDriver; 14 | 15 | public class WebDriverClass { 16 | static WebDriver driver; 17 | 18 | private WebDriverClass() { 19 | WebDriverClass.getDriver(); 20 | } 21 | 22 | public static WebDriver getDriver() { 23 | driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); 24 | 25 | return driver; 26 | } 27 | 28 | public static void setDriver(WebDriver driver) { 29 | WebDriverClass.driver = driver; 30 | } 31 | 32 | /** 33 | * @return 34 | * Getting the instance of the driver 35 | */ 36 | public static WebDriver getInstance() { 37 | if (driver == null) { 38 | 39 | driver = (WebDriver) new WebDriverClass(); 40 | 41 | return driver; 42 | } else { 43 | 44 | return driver; 45 | } 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/resources/config.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 22 10:15:32 IST 2015 2 | URL= 3 | TestSuiteName= 4 | TestCasePath= 5 | TestSuite_SHEETNAME1=RegressionSuite 6 | ELEMENT_LOCATORS= 7 | TestCase_SheetName=TestCaseSheet 8 | IEDRIVER= 9 | SCREENSHOTS= 10 | -------------------------------------------------------------------------------- /test/test.java: -------------------------------------------------------------------------------- 1 | package test; 2 | /** 3 | * The test class is used to test the Report class 4 | * 5 | * @author Shilpashree_V 6 | * @version 1.0 7 | * @since February 2015 8 | * 9 | */ 10 | import java.io.FileNotFoundException; 11 | import java.io.FileOutputStream; 12 | 13 | import javax.xml.stream.util.StreamReaderDelegate; 14 | import javax.xml.transform.*; 15 | import javax.xml.transform.stream.StreamResult; 16 | import javax.xml.transform.stream.StreamSource; 17 | 18 | public class test { 19 | public static void report(){ 20 | 21 | 22 | try { 23 | 24 | System.setProperty("javax.xml.transform.TransformerFactory", 25 | "net.sf.saxon.TransformerFactoryImpl"); 26 | 27 | TransformerFactory transFact= TransformerFactory.newInstance(); 28 | Transformer trans=transFact.newTransformer(new StreamSource("xml/testng-results.xsl") ); 29 | 30 | trans.setParameter("testNgXslt.outputDir", "D:/newEclipseWorksspace/SeleniumTemp/test-output"); 31 | 32 | trans.setParameter("testNgXslt.cssFile", "D:/newEclipseWorksspace/SeleniumTemp/xml/custom.css"); 33 | trans.setParameter("testNgXslt.showRuntimeTotals", "true"); 34 | trans.setParameter("testNgXslt.sortTestCaseLinks", "true"); 35 | trans.setParameter("testNgXslt.testDetailsFilter", "FAIL,PASS,SKIP,CONF,BY_CLASS"); 36 | 37 | FileOutputStream os=new FileOutputStream("test-output/report.html"); 38 | trans.transform(new StreamSource("xml/testng-results.xml"), new StreamResult(os)); 39 | 40 | } catch (TransformerConfigurationException e) { 41 | // TODO Auto-generated catch block 42 | e.printStackTrace(); 43 | } catch (FileNotFoundException e) { 44 | // TODO Auto-generated catch block 45 | e.printStackTrace(); 46 | } catch (TransformerException e) { 47 | // TODO Auto-generated catch block 48 | e.printStackTrace(); 49 | } 50 | } 51 | } 52 | --------------------------------------------------------------------------------