Create a selenium java Framework and Project.

Readers can directly use this framework, this is compatible with all types of framework.

You can download the framework using following link:

https://drive.google.com/open?id=1B4ASNArSaUo8p4tB-Hf6959K0pBL5bta

  1. Create a Sample selenium java Framework and Project.

 

 Step 1. Install TestNg Using Eclipse Marketplace.

 Step 2. Go To

                File-> New -> Other ->

-> Maven ->Maven Project

-> Click Next ->Click on Create a Simple project

-> Click Next

-> Enter Group ID (reverse-domain-packages for groupId)

-> Enter Aritifact ID (project name as artifactId)

-> Click Finish

Now , once Finish You will see the Folder Structure like this.

Step 3: Write Pom.xml then press CTRL+ S

<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.companyname.projectname</groupId>

<artifactId>ProjectName.Auromation</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>My Poject portal Automation Test Suite</name>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.target>1.8</maven.compiler.target>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.test.skip>true</maven.test.skip>

<jdk.version>1.8</jdk.version>

</properties>

<dependencies>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.12</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poiooxml</artifactId>

<version>3.12</version>

</dependency>

<dependency>

<groupId>com.google.inject</groupId>

<artifactId>guice</artifactId>

<version>3.0</version>

</dependency>

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-server</artifactId>

<version>2.53.1</version>

</dependency>

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>2.53.1</version>

</dependency>

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>6.9.10</version>

</dependency>

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

<version>4.5.2</version>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

<dependency>

<groupId>com.aventstack</groupId>

<artifactId>extentreports</artifactId>

<version>3.0.3</version>

</dependency>

</dependencies>

<build>

<resources>

<resource>

<directory>${basedir}/</directory>

<includes>

<include>*.jar</include>

</includes>

<targetPath>lib/</targetPath>

</resource>

</resources>

<plugins>

<plugin>

<artifactId>maven-resources-plugin</artifactId>

<version>2.6</version>

<executions>

<execution>

<id>copy-resources</id>

<phase>process-classes</phase>

<goals>

<goal>copy-resources</goal>

</goals>

<configuration>               

<outputDirectory>${basedir}/target/classes</outputDirectory>

<encoding>UTF-8</encoding>

<resources>

<resource>

<directory>${basedir}/src/test/resources</directory>

<includes>                                                        <include>**/*</include>

</includes>

</resource>

</resources>

</configuration>

</execution>

</executions>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-dependency-plugin</artifactId>

<executions>

<execution>

<id>copy</id>

<phase>install</phase>

<goals>

<goal>copy-dependencies</goal>

</goals>

<configuration>                                    

  <outputDirectory>${project.build.directory}/${project.artifactId}/lib</outputDirectory>   

<overWriteReleases>false</overWriteReleases>

<overWriteSnapshots>false</overWriteSnapshots>

<overWriteIfNewer>true</overWriteIfNewer>

<source>1.8</source>

<target>1.8</target>

</configuration>

</execution>

</executions>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-dependency-plugin</artifactId>

<executions>

<execution>

<id>copy</id>

<phase>install</phase>

<goals>

<goal>copy-dependencies</goal>

</goals>

<configuration>         

<outputDirectory>${project.build.directory}/${project.artifactId}/lib</outputDirectory>   

<overWriteReleases>false</overWriteReleases>         

<overWriteSnapshots>false</overWriteSnapshots>                            

<overWriteIfNewer>true</overWriteIfNewer>

<source>1.8</source>

<target>1.8</target>

</configuration>

</execution>

</executions>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>mavensurefireplugin</artifactId>

<version>2.19.1</version>

<configuration>

<suiteXmlFiles>

<suiteXmlFile>demo.xml</suiteXmlFile>

</suiteXmlFiles>

</configuration>

</plugin>

</plugins>

</build>

</project>

Step 4: Create following package and folders and add all the required drivers .exe file to folder as mentioned in snapshot.

Step 5: Create following Java Class inside CommonFunction package

a. create CommonMethod.java Class: You can write all of your wait Helper methods, takescreenShot and regex methods in this Class.

Step 6 :  Download All the driver.exe and put it under src/test/resources as below as shown in above image.

Step 7: a Create Log.java class under Utils package.

              b.Create constants.java under Utils package:  You can put all your Constant variable inside this class.

Step 8. Create SampleLogin.java Class under myaccount package

  1. The following code is based on Pagefactory which advance model of page object model.

 

package com.projectname.myaccount;

import org.apache.log4j.Logger;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.How;

import org.openqa.selenium.support.PageFactory;

import com.projectname.commonfunction.CommonMethod;

public class SampleLogin {

private final static Logger S_LOGGER =    Logger.getLogger(SampleLogin.class.getName());

WebDriver driver;

CommonMethod cp;

// Webelements

@FindBy(how = How.ID, using = “Sign In”)

private WebElement signIn;

@FindBy(how = How.NAME, using = “lpasswd”)

private WebElement pwd;

@FindBy(how = How.NAME, using = “luser”)

private WebElement userName;

public SampleLogin(WebDriver driver) {

this.driver = driver;

PageFactory.initElements(driver, this);

cp = new CommonMethod(driver);

}

public void login(){

userName.sendKeys(“shashank”);

pwd.sendKeys(“shahsank”);

signIn.click();

}

}

 

2. Create a SampleTest Class under myaccounttest package

package com.projectname.myaccounttest;

import org.apache.log4j.Logger;

import org.openqa.selenium.WebDriver;

import org.testng.ITestResult;

import org.testng.Reporter;

import org.testng.annotations.AfterClass;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Listeners;

import org.testng.annotations.Optional;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

import com.projectname.marketingportal.SampleLogin;

import com.projectname.commonfunction.CommonMethod;

import com.projectname.commonfunction.FindBrokenLinks;

import com.projectname.commonfunction.SetUP;

 

@Listeners(com.projectname.commonfunction.ExtentTestNGIReporterListener.class)  

class SampleTest {

 

private final static Logger S_LOGGER = Logger.getLogger(SampleTest.class.getName());

static WebDriver driver;

SampleLogin login;

CommonMethod cp;

@BeforeTest

@Parameters({ “browser” })

private void setUp(@Optional(“FFX”) String browser) {

System.out.println(“HomePageTest S_LOGGER is running”);

try{

SampleTest.driver = SetUP.setUP(browser);

 

} catch (Exception e) {

System.out.println(“setUp() method has not executed successfully”);

System.out.println(“setUp() method has not executed successfully  | “

+ new Object() {

}.getClass().toString() + ” | method ” + new Object() {

}.getClass().getEnclosingMethod().getName());

Reporter.log(“setUp() method has not executed successfully”);

}

cp = new CommonMethod(driver);

cp.waitForPageLoaded();

login=new SampleLogin(driver);

}

/**

/**

* This method runs after all the Test scenarios have run. It closes the

* browser

*/

@AfterClass

public void teardown() throws InterruptedException {

cp.tearDown();

System.out.println(“Browser window is closed”);

Reporter.log(“Browser window is closed”);

}

/**

* This method runs after  EachTest Method scenarios have run. It takes

* snapshot where test has failed.

*/

@AfterMethod

public void afterTestMethod(ITestResult result) throws Exception{

if(!result.isSuccess()){

CommonMethod.takeScreenshot(SampleTest.driver, result);

}

}

 

@Test( priority = 1)

private void loginTest() {

System.out.println(“headerLogoTest started”);

SampleLogin.login();

}

 

Step-9 Create log4j.xml inside project and place the below lines of code .

<?xml version=“1.0” encoding=“UTF-8”?>

<!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”>

<log4j:configuration xmlns:log4j=“http://jakarta.apache.org/log4j/”

debug=“false”>

<appender name=“file” class=“org.apache.log4j.FileAppender”>

<param name=“Threshold” value=“INFO” />

<param name=“File” value=“target/test.log” />

<param name=“Append” value=“true” />

<layout class=“org.apache.log4j.PatternLayout”>

<param name=“ConversionPattern”

value=“[%d{MM-dd-yyyy HH:mm:ss}] [%-5p] [%60c] [%25M] %m%n” />

</layout>

</appender>

<appender name=“console” class=“org.apache.log4j.ConsoleAppender”>

<param name=“Target” value=“System.out” />

<param name=“Threshold” value=“INFO” />

<layout class=“org.apache.log4j.PatternLayout”>

<!– The default pattern: Date Priority [Category] Message\n –>

<param name=“ConversionPattern”

value=“[%d{MM-dd-yyyy HH:mm:ss}] [%-5p] [%60c] [%25M] %m%n” />

</layout>

</appender>

<root>

<priority value=“TRACE” />

<appender-ref ref=“console” />

<appender-ref ref=“file” />

</root>

</log4j:configuration>

 

Step 10: Create a testNG.xml

<?xml version=“1.0” encoding=“UTF-8”?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=“Suite” thread-count=“1”>

<test name=“Test In FireFox”>

<parameter name=“browser” value=“FFX” />

<classes>

<class name=“com.projectname.myaccounttest.SampleLogin”/>

</classes>

</test> <!– Test –>

<test name=“Test In Chrome”>

<parameter name=“browser” value=“CRM” />

<classes>

<class name=“com.projectname.myaccounttest.SampleLogin”/>

</classes>

</test> <!– Test –>

<test name=“Test In IE”>

<parameter name=“browser” value=“IE” />

<classes>

<class name=“com.projectname.myaccounttest.SampleLogin”/>

</classes>

</test> <!– Test –>

<test name=“Test In safari”>

<parameter name=“browser” value=“SF” />

<classes>

<class name=“com.projectname.myaccounttest.SampleLogin”/>

</classes>

</test> <!– Test –>

</suite> <!– Suite –>

 

Step 11: Run the POM.xml as maven install or maven build.

Step 12: Run testNG.xml as testNG SUite.

Shashank is an information technology graduate, keen observer and a perfection seeker. Passionate about browser as well as Robotic Process Automation