Selenium Integration with Jenkins

In this post we will discuss about following points:
  • What is Continuous Integration?
  • What is Jenkins
  • Advantages of Jenkins
  • Other CI tools
  • Complete Jenkins installation Process
What is Continuous Integration?
Continuous Integration is a development practice in which the Automation Testers are required to commit changes to the source code in a shared repository several times a day or more frequently. Every commit made in the repository is then built. This allows the teams to detect the problems early. Continuous Integration is the most important part of DevOps that is used to integrate various DevOps stages. Jenkins is the most famous Continuous Integration tool.

What is Jenkins?
Jenkins is an open source automation tool written in Java with plugins built for Continuous Integration purpose. Jenkins is used to build and test your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. It also allows you to continuously deliver your software by integrating with a large number of testing and deployment technologies. Jenkins integrates development life-cycle processes of all kinds, including build, document, test, package, stage, deploy, static analysis and much more. Jenkins achieves Continuous Integration with the help of plugins. Plugins allows the integration of Various DevOps stages. If you want to integrate a particular tool, you need to install the plugins for that tool. For example: Git, Maven, HTML publisher etc.

Advantages:
  • Advantages of Jenkins include:
  • It is an open source tool with great community support.
  • It is easy to install.
  • It has 1000+ plugins to ease your work. If a plugin does not exist, you can code it and share with the community.
  • It is built with Java and hence, it is portable to all the major platforms.
Apart from Jenkins, we have many more tools in the market such as:
  • Anthill
  • Bamboo
  • Cruise Control
  • Team City
Complete Jenkins Installation Process
URL: https://www.jenkins.io/download/ 

1. Download the WAR file from https://www.jenkins.io/download/ 

2. Start the Jenkins using below commands:
>C:\Users\Hitendra>java -jar <Path of WAR file> --httpPort=8082
Default port is 8080, no need to specify port if your jenkins running on 8080 port

Press Enter

3. Browse to http://localhost:8080 (or whichever port you configured for Jenkins when installing it) and wait until the Unlock Jenkins page appears.

4.  From the Jenkins console log output, copy the automatically-generated alphanumeric password from the cmd or from given path.

5. On the Unlock Jenkins page, paste this password into the Administrator password field and click Continue.

6. You can install either the suggested plugins or selected plugins you choose. To keep it simple, we will install the suggested plugins.

7. Wait until the plugins are completely installed.
8. The next thing we should do is create an admin user for Jenkins. Put in your details and click “Save and Continue”.

9. Click “Save and Finish” to complete the Jenkins installation.
10. Now, click “Start using Jenkins” to start Jenkins.
11. Finally, here is the default Jenkins page.
Please refer below YouTube videos to understand the Different Configurations on Jenkins
  • Demo 1: Create a batch file and run on Jenkins (Integration of Eclipse Java Project + Jenkins)
  • Steps to create the batch file 
set projectLocation=D:\Workspace_Eclipse\TestNGJenkinsDemo
cd %projectLocation%
set classpath=%projectLocation%\bin;%projectLocation%\lib\*
java org.testng.TestNG %projectLocation%\testng.xml
pause

Save this file with extension (.bat).
Please follow below video on how to use this command in Jenkins

Demo2 : Installation of different plugins and Integration of (Selenium + Maven + Jenkins) and Selenium + Maven + GitHub + Jenkins Integration with Parameters

Demo3 : Selenium + Maven + GitHub + Jenkins + TestNG Report + Extent Report + Email +Email with Attachment + Schedule Jobs
In this video, we will discuss about following things.
1. How to generate HTML report in Jenkins
2. How to generate Extent report in Jenkins
3. How to send email
4. How to send email with an attachment
5. How to run job periodically in Jenkins

Sample Email Body Template:
Hi Team,
<br/>
<br/>
Please find the below Automation Testing Results for Project: <b>$PROJECT_NAME</b>.
<br/>
<br/>
<b>Project Name:</b> $PROJECT_NAME 
<br/>
<b>Build#</b>: $BUILD_NUMBER
<br/>
<b>Build Status</b>: $BUILD_STATUS.<br/>
<br/>
Check <a href='http://localhost:8082/job/Demo4/ws/ExtentDemo/test-output/ExtentReport/MyReport.html'>Extent Report</a> to view full results.<br/>
<br/>
Please find below <b>Colsole Logs</b> for your reference<br/>
<br/>
--LOG-BEGIN--<br/>
<pre style='line-height: 22px; display: block; color: #333; font-family: Monaco,Menlo,Consolas,"Courier New",monospace; padding: 10.5px; margin: 0 0 11px; font-size: 13px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: #f5f5f5; border: 1px solid #ccc; border: 1px solid rgba(0,0,0,.15); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px;'>
${BUILD_LOG, maxLines=500, escapeHtml=true}
</pre>
--LOG-END--
<br/>
<br/>
Thank You
Hitendra

Please refer below YouTube Video for complete understanding:

Demo4 : Add logo in extent report and attach screen shot for failed test case-

Selenium Integration with Git and GitHub

In this post we will discuss about following things:
  • What do you mean by CM and what is SCM tool?
  • Why do we use SCM tools and advantages
  • What is Git and GitHub?
  • Basic Architecture
  • Steps to work with Git and GitHub
  • How to install Git
  • Demo – execute different commands using cmd
What is CM and what are diff SCM tool?
  • Configuration management(CM) is managing the configuration of all of the project's key products and assets.
  • SCM stands for Source Code Management is an integral part of any project in the IT world.
  • Important component in DeveOps culture. 
  • Source Code Management or Version Control Systems in any project ensure all the members of a team stay on top of the source code changes.
  • SCM practices include revision control and the establishment of baselines.
Top SCM Tools:
  • MS Team Foundation Server (TFS):
  • Kallithea - Open Source
  • GitLab - Continuous Integration (CI), Continuous Delivery (CD) is an integral part of GitLab
  • Bitbucket Server:
  • Subversion (SVN):
  • Git and GitHub
Why do we use version control System?
  • Collaboration- Without a SCM in place, you're probably working together in a shared folder on the same set of files. It's extremely error prone, someone will overwrite someone else's changes. With a SCM, everybody on the team is able to work absolutely freely - on any file at any time. 
  • Storing Versions
  • Restoring Previous Versions
  • Understanding What Happened: Every time you save a new version of your project, your SCM requires you to provide a short description of what was changed. This helps you understand how your project evolved between versions.
  • Backup
Now lets talk about Git and GitHub

Git and GitHub
  • Git – initially developed by Linus Torvalds is a version control system.
  • Git is a version control system that lets you manage and keep track of your source code history. 
  • GitHub is a cloud-based hosting service that lets you manage Git repositories.
Different Terminologies with GitHub

Repository: You can simply, treat it as a storage area of your workplace that contains all your documentation files and the history of changes.
Clone: Clones are literally clones (copies) of a repository that sit on the developer’s computer instead of a server elsewhere.
Commit: Whatever the changes you make in your files will come under commit. Every change is saved under a particular name or ID which is also called “revision”.
Push: Pushing refers to sending your committed changes to a remote repository such as GitHub.com.

Pull Request: If you have made changes in code/script, to show the other collaborators you send a pull request.
Fork: It is a copy of other's repository in your account in which you can make changes and it won't affect the original code.
Branching: When you extract a portion /section of code from the main or remote track of your software, then it is called ‘branch' and the process is known as Branching.
Fetch: Fetching refers to getting the latest changes from an online repository (like GitHub.com) without merging them in.
Merge: Merging takes the changes from one branch (in the same repository or from a fork), and applies them into another.
Git and GitHub

Steps to work with Git and GiHub
  • Download and Install Git - https://git-scm.com/download/win , https://git-scm.com/download/mac (Please refer below YouTube video at 27:00 minute for download/installation of Git)
  • Create a GitHub Account
  • Using Command Prompt - by using different git commands
  • Using Eclipse -  we have inbuilt Git plugin in eclipse
Please refer below different git commands:
1. git config
This command sets the author name and email address respectively to be used with your commits.
git config global user.name [name]  
git config global user.email [email address]
Example: 
git config user.name "Hitendra Kuamar Verma"
git config user.email "Hitendra@Hitendra-PC"

2. git init
This command is used to start a new repository.
git init [repository path
Example: navigate to repo path and
git init

3. git clone
This command is used to obtain a repository from an existing URL.
git clone [url]  
Example: navigate to your repo path where you want to clone and write below command in cmd
git clone https://github.com/hverma22/Test2

4. git add
This command adds a file to the staging area.
git add [file] 
Example: 
git add [file]
git add *  
git add .

5. git commit
git commit -m [commit message] 
This command records or snapshots the file permanently in the version history. 
Example:
git commit -m First Commit

6. git diff
This command shows the file differences which are not yet staged.
Example:
git diff -staged
git diff [first branch] [second branch]  

7. git reset
This command unstages the file, but it preserves the file contents.
git reset [file]  
git reset [commit]  
git reset hard [commit]

8. git status
git status  
This command lists all the files that have to be committed.

9. git rm
This command deletes the file from your working directory and stages the deletion.
git rm [file] 

10. git branch
git branch  
This command lists all the local branches in the current repository.
Example:
git master

11. git log
git log  
This command is used to list the version history for the current branch.
Example:
git log --online

12. git merge
git merge [branch name]  
This command merges the specified branchs history into the current branch.

13. git remote add [variable name] [Remote Server Link]  
This command is used to connect your local repository to the remote server.
Example:
git remote add origin https://github.com/hverma22/Test5

14. git push
git push [variable name] master  
This command sends the committed changes of master branch to your remote repository.
Example:
git push origin master
git push origin master --force

15. git pull
git pull [Repository Link]  
This command fetches and merges changes on the remote server to your working directory.
Example:
git pull https://github.com/hverma22/Test2.git

16. git show
git show [commit]  
This command shows the metadata and content changes of the specified commit.
Command: git show <ChangeID>:<FilePath>
Example: 
git show 45dhfg56:/src/test/newtest.xml

17. Checkout
git checkout [branch name]  
This command is used to switch from one branch to another or You can get the specific previous version.
Command: git checkout <ChangeID> <filePath with extenion>
Example:
git checkout 6475fgh5 pom.xml

Please refer below video to understand better. We have covered the following scenarios:
  • Create a new Project and Add your fresh project into Github (into existing repository) 
 Add all project files
 Make the change in the code and push the code
  • How to pull the code when someone make the changes.
  • How to checkout the particular version.
  • How to work with existing repository- Clone a existing repository
YouTube video: Part A - Introduction to SCM tools and Demo of Git and GitHub using Command Line

YouTube Video Part B - How To Add Eclipse Project To GitHub | How to Commit, Push, Pull and Checkout




YouTube Video Part C - Branching, Merging and Conflict Resolution using Eclipse

Data Driven Framework Part 2

Data driven example with excel sheet - Please refer below source codes to understand the implementation of Data Driven Testing/Framework using excel sheet.

In this example we are passing the username and password using excel sheet to test the login functionality.

Excel Sheet with user credentials:

Base Class: In this base class we have created following methods

setup() - to launch the browser and navigate to application
tearDown() - quit the browser
getExcelData() - to get the excel test data and store using 2-D Array.
and we have created the object of NewExcelLibrary to use the different methods to access the excel sheet. Complete excel library you can find here.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import com.utility.NewExcelLibrary;

public class BaseClass {
 public WebDriver driver;
 NewExcelLibrary obj= new NewExcelLibrary("D:\\Workspace_Eclipse\\DataDriven\\TestData\\User.xlsx");
 
 @BeforeMethod
 public void setup() {
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32 (2)\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://opensource-demo.orangehrmlive.com/");
 }

 @AfterMethod
 public void tearDown() {
  driver.quit();
 }
 
 @DataProvider(name ="Credentials1")
 public Object[][] getExcelData() {
  //Totals rows count
  int rows=obj.getRowCount("Data");
  //Total Columns
  int column=obj.getColumnCount("Data");
  int actRows=rows-1;
  
  Object[][] data= new Object[actRows][column];
  
  for(int i=0;i<actRows;i++) {
   for(int j=0; j<column;j++) {
    data[i][j]=obj.getCellData("Data", j, i+2);
   }
  }
  return data;
 }
}

TestClass: Supply the dataProvider name in test method

import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.base.BaseClass;

public class DataDrivenTest extends BaseClass {

 @Test(dataProvider = "Credentials1")
 public void loginTest(String username,String password) {
  
  driver.findElement(By.id("txtUsername")).sendKeys(username);
  driver.findElement(By.id("txtPassword")).sendKeys(password);
  driver.findElement(By.id("btnLogin")).click();
  String actualURL=driver.getCurrentUrl();
  String expectedURL="https://opensource-demo.orangehrmlive.com/index.php/dashboard";
  Assert.assertEquals(actualURL, expectedURL);
 }
}

Output: Refer the below output, you can see our test executed with different set of test data

Please refer below YouTube video to understand better.

Data Driven Framework Part 1

What is Data Driven Testing?

Data-driven is a test automation framework which stores test data in a table or spreadsheet format. This allows automation engineers to have a single test script which can execute tests for all the test data in the table.


Why Data Driven Testing?

Example 1:
We want to test the login system with multiple input fields with 100 different data sets.
To test this, you can take following different approaches:
Approach 1) Create 100 scripts one for each dataset and runs each test separately one by one.
Approach 2) Manually change the value in the test script and run it several times.
Approach 3) Import the data from the excel sheet. Fetch test data from excel rows one by one and execute the script.
In the given three scenarios first two are laborious and time-consuming. Therefore, it is ideal to follow the third approach.
Thus, the third approach is nothing but a Data-Driven framework.

Example 2: Test different products in e-commerce application.

Advantages of Data Driven Framework
  • Advantages of using Data Driven Test Framework
  • Re-usability of code
  • Improves test coverage
  • Faster Execution
  • Less maintenance
  • Permits better error handling
How to Implement?

Using DataProvider in TestNG
An important features provided by TestNG is the testng DataProvider feature. It helps you to write data-driven tests which essentially means that same test method can be run multiple times with different data-setsTo test this.

To use the DataProvider feature in the tests, you have to declare a method annotated by @DataProvider and then use the said method in the test method using the ‘dataProvider‘ attribute in the @Test annotation.
Data provider returns a two-dimensional JAVA object and test method will invoke M times in a M*N type of object array. 

Lets have a look Data driven example without using excel sheet just to understand the data driven test --  Data driven testing of login functionality.

Base Class: In this base class we have created following methods

setup() - to launch the browser and navigate to application
tearDown() - quit the browser
getData() - to setup the test data using 2-D Array.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import com.utility.NewExcelLibrary;

public class BaseClass {
 public WebDriver driver;
 NewExcelLibrary obj= new NewExcelLibrary("D:\\Workspace_Eclipse\\DataDriven\\TestData\\User.xlsx");
 
 @BeforeMethod
 public void setup() {
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32 (2)\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://opensource-demo.orangehrmlive.com/");
 }

 @AfterMethod
 public void tearDown() {
  driver.quit();
 }

 @DataProvider(name = "Credentials")
 public Object[][] getData() {

  Object[][] data = new Object[3][2];

  data[0][0] = "admin";
  data[0][1] = "admin123";

  data[1][0] = "admin1";
  data[1][1] = "admin123";

  data[2][0] = "admin2";
  data[2][1] = "admin";

  return data;
 }
}

Test Class: Supply the dataProvider name in test method
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.base.BaseClass;

public class DataDrivenTest extends BaseClass {

 @Test(dataProvider = "Credentials")
 public void loginTest(String username,String password) {
  
  driver.findElement(By.id("txtUsername")).sendKeys(username);
  driver.findElement(By.id("txtPassword")).sendKeys(password);
  driver.findElement(By.id("btnLogin")).click();
  String actualURL=driver.getCurrentUrl();
  String expectedURL="https://opensource-demo.orangehrmlive.com/index.php/dashboard";
  Assert.assertEquals(actualURL, expectedURL);
 }
}

Output: Refer the below output, you can see our test executed with different set of test data
Please refer the below YouTube video to understand better: