Part 1: Hybrid Framework Development…

So far we have covered multiple topics in Selenium Automation. And the good thing is that we are going to use most of the topics covered into the creation of our Hybrid Framework. Sounds interesting, right?

What all functionality our Hybrid Framework will contain?

  • First and foremost it will be a Maven project. We will not download individual jars as we did earlier, rather we will add dependencies in our pom.xml file.
  • And of course, it will a Selenium, TestNG project.
  • We will include like Page Object Modeling, Page Factory concepts in our framework.
  • We will add Data Driven technique in our framework for test data input.
  • The framework will have Log4j logging.
  • In addition to TestNG reports, we will also include Extent Reports for advanced HTML reports.
  • We will create user defined methods for different actions.
  • And we will also add a new topic in our Hybrid Framework and that is the use of properties file in order to store some configurable parameters of our application.

Setting up this framework will be a long and tiring affair but for sure it’s going to be an interesting one. Our demo application will be New Tours Demo, which we used in our previous posts also.

Hybrid Framework Development:

  • Our first task is to create a Maven project in Eclipse. Go to File -> New -> Project and select Maven and click Next. Use default workspace location and quick-start archetype. Enter a Group ID, in our case its ‘com.testmadness.www’, Artifact ID as ‘HybridFramework’. Click Finish to complete the project creation. Once complete, Maven will automatically create the project structure as displayed below.

    Maven Project
  • Next step is to add the dependencies in our pom.xml file. For that we will need dependencies for Selenium, TestNG, Log4j, Apache POI and Extent Reports. We can get the dependencies either by doing a search in Google or by directly going to Maven Repository website. After successful update, our pom.xml will look as below:
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>com.testmadness.www</groupId>
    	<artifactId>HybridFramework</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    	<name>HybridFramework</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<maven.compiler.source>1.8</maven.compiler.source>
    		<maven.compiler.target>1.8</maven.compiler.target>
    		<selenium.version>3.4.0</selenium.version>
    		<testng.version>6.10</testng.version>
    		<log4j.version>1.2.17</log4j.version>
    		<extent.version>2.41.2</extent.version>
    		<selenium.browser>CHROME</selenium.browser>	
    <webdriver.safari.driver>/usr/bin/safaridriver</webdriver.safari.driver>
    <webdriver.firefox.driver>/Users/sejijohn/Documents/JARS/geckodriver</webdriver.firefox.driver>		<webdriver.chrome.driver>/Users/sejijohn/Documents/JARS/chromedriver 2</webdriver.chrome.driver>
    		<logfoldername>logfoldername</logfoldername>
    		<logfilename>logfilename</logfilename>
    	</properties>
    
    <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
              <execution>
                <phase>generate-resources</phase>
                <goals>
                  <goal>write-project-properties</goal>
                </goals>
                <configuration>
                  <outputFile>
                    src/main/resources/system.properties
                  </outputFile>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.seleniumhq.selenium</groupId>
    			<artifactId>selenium-java</artifactId>
    			<version>${selenium.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.testng</groupId>
    			<artifactId>testng</artifactId>
    			<version>${testng.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>log4j</groupId>
    			<artifactId>log4j</artifactId>
    			<version>${log4j.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>com.relevantcodes</groupId>
    			<artifactId>extentreports</artifactId>
    			<version>${extent.version}</version>
    		</dependency>
                    <dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi</artifactId>
    			<version>3.16</version>
    		</dependency>
                    <dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi-ooxml</artifactId>
    			<version>3.16</version>
    		</dependency>
    	</dependencies>
    </project>
    

In the above pom.xml file, the first part (represented in pink color) has the project details like, Group ID, Artifact ID which we entered in the initial stage of our project creation. Since we selected default archetype, the packaging is in jar.

The second part (represented in blue color) is where we are setting the properties. If we want to use Java 8 language features (-source 1.8) and also want the compiled classes to be compatible with JVM 1.8 (-target 1.8), we can add the two following two properties, maven.compiler.source, maven.compiler.target with the Java version mentioned in them. The different version properties that we set, for example, selenium.version can be used to set the version of different dependencies inside pom.xml. This is not mandatory as we can directly set the versions in the dependencies. However, by doing this, we can add extra flexibility to directly update the versions from properties rather updating in the dependencies. Other properties such as selenium.browser, webdriver.safari.driver, are used to pass configurable parameters to a .properties file, which we will create shortly. Please make sure to download latest Safari, Chrome, Firefox drivers, and store in a local location. From properties we will be passing the location of different drivers to our code. The next ones are logfoldername, logfilename properties which we are using to set the location of our log files in log4j.xml.

The third part (represented in purple color) is a plugin to update the properties from pom.xml file to a .properties file inside ‘src/main/resources’ folder.

The last part (represented in red color) is where we are adding the dependencies. As mentioned earlier, we can get the dependencies and their versions by doing a Google search.

  • After updating the pom.xml file, our next step is to create a .properties file. We will use this .properties file to configure the type of browser and as well as for setting the browser properties. Always remember, that we should place the .properties file inside ‘src/main/resources’ folder and not into ‘src/main/java’ directory (keeping the same subfolder structure). For that, we need to create a source folder named resources inside ‘src/main‘ directory.
  • To create a source folder, click on Project name, select New -> Source Folder. Provide the folder name as ‘src/main/resources’.

    Source Folder creation
  • After creating resources folder, our next step is to create a .properties file. Right click on ‘src/main/resources’ folder -> New -> Other. Select File and click Next. Name the file as ‘system.properties’ and click Finish. A blank .properties file will be created inside resources folder.

    system.properties files inside resources folder
  • Now that we have updated pom.xml file and created .properties file, our next step is to update our Maven project with these changes. To update project, click on Project Name -> Maven -> Update Project.

    Update Maven project
  • After successful update, the Java System Library is updated from default 1.5 version to 1.8 version based on maven.compiler.source, maven.compiler.target properties in the pom.xml file. Also the system.properties file inside resources folder is automatically updated with all the properties from pom.xml file.

    system.properties file

With this, we have completed the initial structure of our Hybrid Framework. Our next step is to add the source codes inside our Framework. We will cover this in our next topic. Till then, Live Well and Happy Learning!