Part 2: Hybrid Framework Development…

In Part 1: Hybrid Framework Development…, we have created overall structure of our framework. Our next step is to create source codes inside our project. Here also we will take a step by step approach in creating the source code.

  • First, delete the unwanted packages from the ‘src/main/java‘ and ‘src/test/java’ folders. Remember, Maven by default creates some test classes. In our case, we don’t need them as we will be creating our own.
  • Right click on the ‘src/main/java‘ folder and click New -> Package. Name the package as ‘com.testmadness.utils’ and click Finish to close the window.
  • Inside ‘com.testmadness.utils’ package, we will place all the utility classes which are basically common classes and not project specific. For example, we need some class for the Log4j, Extent Reports, accessing the property file etc..
  • First of all, we will create a ‘Constants.java’ file to store all the constant values. Right click on ‘com.testmadness.utils’ package and select New -> Class. Name the class as ‘Constants.java’. Copy the below code into ‘Constants.java’ file. In this class, we will be storing different constants that will be referred from different classes. Notice that all our constants are static variables.
    package com.testmadness.utils;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Constants {
    
    	// TestResults folder location
    	public static final String PATH_RESULTS = System.getProperty("user.dir") + "/TestResults/";
    
    	// Test Data Excel location
    	public static final String PATH_DATA = System.getProperty("user.dir") + "/src/main/java/com/testmadness/data/Data.xlsx";
    
    	// Test Data Excel Sheet name
    	public static final String SHEETNAME = "Sheet1";
    
    	// Test Data Excel File Name
    	public static final String FILE_NAME = "TestData.xlsx";
    
    	// Property file name
    	public static final String PROP_NAME = "system.properties";
    
    	// Property file location
    	public static final String PROP_LOCATION = "src/main/resources";
    
    	public static Date date = new Date();
    	public static SimpleDateFormat reportDate = new SimpleDateFormat("MM-dd-yyyy hh-mm-ss");
    	
    	//Log Files Constants
    	
    	public static String logFileName = "LOG_"+reportDate.format(date);
    	public static String dateTag = reportDate.format(date);
    	public static String logFolderName = "LOG_FOLDER_"+reportDate.format(date);
    
    	// Extent Report constants
    	public static final String filePath = System.getProperty("user.dir");
    	public static final String reportPath = filePath + "/RESULT_LOG";
    	public static final String imagePath = filePath + "/IMAGES";
    
    	public static final String reportFileName = reportPath + "/" + "TESTREPORT_" + reportDate.format(date) + "TC.html";
    	public static final String screenshotFileName = reportPath + "/SCREENSHOTS" + "TESTREPORT_"
    			+ reportDate.format(date) + "TC.html";
    	public static final String screenshotFilePath = screenshotFileName + "/SCREENSHOTS" + "TEST"
    			+ reportDate.format(date) + "/";
    	public static String sScreenshotFilepath = filePath + "/Screenshots/" + "IOLS_Screenshot_" + reportDate.format(date)
    			+ "/";
    	public static String sReportFileName = reportPath + "/" + "IOLSTestReport_" + reportDate.format(date) + ".html";
    
    }
    
  • Next, we will create a class to access the property file. Right click on ‘com.testmadness.utils’ package and select New -> Class. Name the class as ‘Config.java’. Copy the below code into ‘Config.java’ file.
    package com.testmadness.utils;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class Config {
    
    	private static Properties prop;
    	public static Properties getProp() {
    		if (prop == null) {
    			prop = new Properties();
    			InputStream input = null;
    			try {
    				//Two arguments passed for property file location
    				input = new FileInputStream(new File(Constants.PROP_LOCATION, Constants.PROP_NAME));
    				prop.load(input);
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    		return prop;
    	}
    }
    
  • Next, we will create Log class and log4j.xml files. We can directly copy the codes from Working with Log4j logging…. We need only codes from’Log.java‘ and ‘log4j.xml‘ files. Notice that both these files will be placed inside ‘com.testmadness.utils’ package. However ‘log4j.xml‘ file has small changes in order to get the log folder and log files names. Copy the below code and copy in ‘log4j.xml‘ file.
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    	<!-- Appender for printing in console -->
    	<appender name="console" class="org.apache.log4j.ConsoleAppender">
    		<layout class="org.apache.log4j.PatternLayout">
    		    <!-- %d{yyyy-MM-dd HH:mm:ss} refers to Date format 2017-03-23 15:54:44 INFO  Log:15 -->
    		    <!-- %-5p refers to Type of logging INFO-->
    		    <!-- %c{1}  -->
    			<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    		</layout>
    	</appender>
    	<!-- Appender for printing in file -->
    	<appender name="file" class="org.apache.log4j.RollingFileAppender">
    		<!-- Appending false -->
    		<param name="append" value="false" />
    		<!-- Maximum 10kb size. New file will be created and old file renamed -->
    		<param name="maxFileSize" value="10KB" />
    		<!-- Maximum 5 log files. Old ones will be deleted -->
    		<param name="maxBackupIndex" value="5" />
    		<!-- Location of log file -->
    		<param name="file" value="test-output/log/${logfoldername}/${logfilename}.log" />
    		<layout class="org.apache.log4j.PatternLayout">
    			<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    		</layout>
    	</appender>
    
    
    	<root>
    	<!-- Setting Logging level  -->
    		<level value="info" />
    		<!-- Setting logging level for console -->
    		<appender-ref ref="console" />
    		<!-- Setting logging level for file -->
    		<appender-ref ref="file" />
    	</root>
    </log4j:configuration>
    
  • For advanced HTML reports, we will use Extent Reports. Right click on ‘com.testmadness.utils’ package and select New -> Class. Name the class as ‘Reports.java’. Copy the code from Advanced HTML Reports… post. However, a small update can be made on the Report location as we can refer the path already mentioned in our Constants.class.
  • Our next task is to create a ‘ExcelUtil.class’ inside ‘com.testmadness.utils’ package. This class will help us to access the test data entered in an Excel sheet. Copy the code of ExcelUtil.class’ from Data Driven Framework Development… post.
  • So far our ‘com.testmadness.utils’ package has classes for accessing property file, Log4j, Excel sheet, Constants and for generating HTML reports.
  • The final structure of ‘com.testmadness.utils’ package will look like as displayed below.

    com.testmadness.www package

 

In the next post, we will focus mainly on creating Page based objects. So stay tuned!