Jira Integration with Selenium || Raise a bug for failure test cases || No Manual Bug in JIRA


Filing issues in JIRA is boring, wanna do with Simply Cool Automation on Selenium... 


I have tried by importing in Product and ran automation from the script, issues are getting filed in JIRA for the failed test cases.

Here we go with Selenium script to raise a bug for Failure Test Cases to direct punch on JIRA.


Step 1 : 

Maven Dependencies to add in our POM.xml

<!-- https://mvnrepository.com/artifact/net.rcarz/jira-client -->
<dependency>
<groupId>net.rcarz</groupId>
<artifactId>jira-client</artifactId>
<version>0.5</version>
</dependency>

Step 2 :

Add two packages in your project, say... com.listeners and com.jira

*Under package com.listeners
     =TestJiraListener.java
*Under package com.jira
     =JiraPolicy.java
     =JiraServiceProvider.java

Add these two files in the respective Packages, refer attachment.

Step 3 : 

Under listener package in TestJiraListener.java file, we need to provide JIRA Authentication API token with our Email ID.

Here is the piece of code change in the TestJiraListner.java file



     JiraServiceProvider jiraSp = new JiraServiceProvider("https://apptha.atlassian.net/","youremail@domain", "XXXXXXXXXXXX", "VOD");

In the place of "XXXXXX" we need to generate JIRA Authentication API Token and replace it.

How to generate JIRA Authentication API Token

Log in to https://id.atlassian.com/manage/api-tokens

  • Click Create API token.
  • From the dialog that appears, enter a memorable and concise Label for your token and click Create.
  • Click Copy to clipboard, then paste the token to your script, or elsewhere to save:


To generate JIRA API Token

Once you created the token, replace it in the place of "XXXXXX".

Now your project might look like this

Selenium Script to file failed automation test to JIRA

Step 4: 

Now we have to add JIRA annotation in our Main Class file, as follow

@JiraPolicy(logTicketReady=true)
@Test(priority = 0, enabled = true)
public void login() throws Exception
{
      //your codes
}

@JiraPolicy(logTicketReady=true)
@Test(priority = 1, enabled = true)
public void signup() throws Exception
{
      //your codes
}

Step 5 :

Now we will run our automation via testng.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" name="Suite" parallel="none" verbose="1">

<listeners>
<listener class-name="com.listeners.TestJiraListener" />
</listeners>

<!-- Start First Test -->
<test name="Test chrome" parallel="none" thread-count="1">
<parameter name="browser" value="chrome" />
<classes>
<class name="yourpackagename.yourmainclassfile" />
</classes>
</test> <!-- End First Test -->

That's it....! You are done....! All failure automate test cases are automatically filed in JIRA. 😎


****Sample Test Case Filed in JIRA****




Advantages
==========
- No need to raise a bug manually.
- Auto logging of issues/bugs/defects in JIRA
- Easy to configure and setup
- It can be used for web, mobile and API Automation also.

Referral Sources From

~~~GIT Repo path~~~
https://github.com/naveenanimation20/JiraIntegrationWithSelenium

~~~YouTube Link~~~
https://www.youtube.com/watch?v=c8lULfmhxzw


!!!!Codes are available.!!!!!


JiraServiceProvider.java
=================

//code
package com.jira;

import net.rcarz.jiraclient.BasicCredentials;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.Issue;
import net.rcarz.jiraclient.Issue.FluentCreate;
import net.rcarz.jiraclient.JiraClient;
import net.rcarz.jiraclient.JiraException;

public class JiraServiceProvider {

 public JiraClient jira;
 public String project;

 public JiraServiceProvider(String jiraUrl, String username, String password, String project) {
  BasicCredentials creds = new BasicCredentials(username, password);
  jira = new JiraClient(jiraUrl, creds);
  this.project = project;
 }

 public void createJiraTicket(String issueType, String summary, String description, String reporterName) {

  try {
   FluentCreate fleuntCreate = jira.createIssue(project, issueType);
   fleuntCreate.field(Field.SUMMARY, summary);
   fleuntCreate.field(Field.DESCRIPTION, description);
   Issue newIssue = fleuntCreate.execute();
   System.out.println("new issue created in jira with ID: " + newIssue);

  } catch (JiraException e) {
   e.printStackTrace();
  }

 }

}


JiraPolicy.java
===========

package com.jira;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface JiraPolicy {
 boolean logTicketReady();
}


TestJiraListener.java
=====================

package com.listeners;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.jira.JiraPolicy;
import com.jira.JiraServiceProvider;

public class TestJiraListener implements ITestListener {

 @Override
 public void onTestStart(ITestResult result) {
  // TODO Auto-generated method stub

 }

 @Override
 public void onTestSuccess(ITestResult result) {
  // TODO Auto-generated method stub

 }

 @Override
 public void onTestFailure(ITestResult result) {

  JiraPolicy jiraPolicy = result.getMethod().getConstructorOrMethod().getMethod().getAnnotation(JiraPolicy.class);
  boolean isTicketReady = jiraPolicy.logTicketReady();
  if (isTicketReady) {
   // raise jira ticket:
   System.out.println("is ticket ready for JIRA: " + isTicketReady);
   JiraServiceProvider jiraSp = new JiraServiceProvider("https://apptha.atlassian.net/",
     "yourJiraemailaddress@com", "XXXXXXXXXXXXXXXXXXX", "VOD");
   String issueSummary = result.getMethod().getConstructorOrMethod().getMethod().getName()
     + "got failed due to some assertion or exception";
   String issueDescription = result.getThrowable().getMessage() + "\n";
   issueDescription.concat(ExceptionUtils.getFullStackTrace(result.getThrowable()));

   jiraSp.createJiraTicket("Bug", issueSummary, issueDescription, "BShiv");
  }

 }

 @Override
 public void onTestSkipped(ITestResult result) {
  // TODO Auto-generated method stub

 }

 @Override
 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
  // TODO Auto-generated method stub

 }

 @Override
 public void onStart(ITestContext context) {
  // TODO Auto-generated method stub

 }

 @Override
 public void onFinish(ITestContext context) {
  // TODO Auto-generated method stub

 }

}

testng.xml
========

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" name="Suite" parallel="none" verbose="1">

 <listeners>
  <listener class-name="com.listeners.TestJiraListener" />
 </listeners>

 <!-- Start First Test -->
 <test name="Test chrome" parallel="none" thread-count="1">
  <parameter name="browser" value="chrome" />
  <classes>
   <class name="Major_Flow.Vplayed" />
  </classes>
 </test> <!-- End First Test -->


 <!-- <test name="my hub spot test firefox" parallel="methods" thread-count="5"> 
  <parameter name="browser" value="ff" /> <classes> <class name="com.MyTests.LoginTest" 
  /> </classes> </test> -->

</suite> <!-- Suite -->


Post Author and Executed By B.Siddarth(Admin)

10 comments:

  1. Hi, Very good info with the use of I am able to create bug ID into Jira. I have one question, if I want to attach screenshot then how should I do this?

    ReplyDelete
  2. Hi @hari Thanks for your valuable feedback and happy to hear that you are able to do it.
    Regarding screenshot, let me also look onto it and if you are able to do share the same here by helping others. Thanks

    ReplyDelete
    Replies
    1. @ B Siddarth - Thanks for this info i was trying to implement the same in my project by creating an account in Jira.. Getting below error
      net.rcarz.jiraclient.JiraException: Failed to retrieve issue metadata

      Do you have any idea as y this generic Jira exception occurs

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. @Rak could you please repost the comment posted, sorry not sure how it got removed.

      Delete
  4. This really is my first time i visit here. I discovered so many entertaining stuff in your blog, especially its discussion. From a great deal of comments in your articles, I guess I am not alone having all of the leisure here! Maintain the superb work. It is very useful who is looking for top software testing companies

    ReplyDelete
  5. can u plzz explain the same via junit , how i can achieve this

    ReplyDelete
  6. Nice article, it’s very informative content..thanks for sharing...Waiting for the next update.

    Use cases of Jira software
    what is Jira used for?

    ReplyDelete
  7. This is useful information for all of us building any software or website in Jira integration, but also check out this workshop management software free trial.

    ReplyDelete
  8. Appreciation for the author for such informative blog. Read about OKR here.

    ReplyDelete