TESTEVERYTHING

Monday 20 May 2013

Create issue in JIRA using Http Client via REST API services/ from JAVA



JIRA is a well known Software-suite. In short, JIRA is a proprietary issue tracking product, maintained and developed by Atlassian. It is commonly used for bug tracking, issue tracking, and project management



The JIRA Remote API Calls


I was surprised how easy it was to get the Remote API up and running. Basically you just enable the ‘Accept remote API calls’ option in your General Configuration settings under the Administration tab. The moment you turn it on, the following URL should give you a nice response


Note: It supports only in JIRA >5.0.

Here in am using http client 4.1.2

Here is  the code:

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;

public class CreateIssue {

 public static void main(String[] args) throws Exception {
  CreateIssue.CreateIssueJira();
 }
public static void CreateIssueJira() throws ClientProtocolException,
   IOException {
  String host = "https://rajivkumarnandvani.atlassian.net/";
  // int port = 8080;
  String userName = "EnterUserName";
  String password = "EnterPassword";
  String ResponseData;
  DefaultHttpClient httpClient = new DefaultHttpClient();
  String jsonObj = "{"
    + "\"fields\": {"
    + "\"project\":"
    + "{"
    + "\"key\": \"DEMO\""
    + "},"
    + "\"summary\": \"REST ye merry gentlemen.\","
    + "\"description\": \"Creating of an issue using project keys and issue type names using the REST API\","
    + "\"issuetype\": {" + "\"name\": \"Bug\"" + "}" + "}" + "}";

  HttpHost targetHost = new HttpHost("rajivkumarnandvani.atlassian.net",
    -1, "https");

  httpClient.getCredentialsProvider().setCredentials(
    new AuthScope(targetHost.getHostName(), targetHost.getPort(),
      targetHost.getSchemeName()),
    new UsernamePasswordCredentials(userName, password));

  HttpPost httpPost = new HttpPost(
    "https://rajivkumarnandvani.atlassian.net/rest/api/2/issue/");
  StringEntity entity = new StringEntity(jsonObj);
  entity.setContentType("application/json");
  httpPost.setEntity(entity);

  // Create AuthCache instance
  AuthCache authCache = new BasicAuthCache();
  // Generate BASIC scheme object and add it to the local auth cache
  BasicScheme basicAuth = new BasicScheme();

  authCache.put(targetHost, basicAuth);

  // Add AuthCache to the execution context
  BasicHttpContext localcontext = new BasicHttpContext();
  localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

  try {

   HttpResponse httpResponse = httpClient.execute(httpPost,
     localcontext);
   HttpEntity entitydata = httpResponse.getEntity();
   ResponseData = new String(EntityUtils.toByteArray(entitydata));
   System.out.println(ResponseData);
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  httpClient.getConnectionManager().shutdown();
 }
}




Thursday 17 January 2013

Jmeter bean shell script, create file, read jmeter variable value, store script variable value into jmeter variable

Hi All,

Some times in JMeter scripting we need to store the output value in file or wanna show the run time  variable value in console window. OR we need to get the current JMeter script directory path. This can be achieved via scripting in Bean Shell Pre/Post Processor.

Here I am using time and counter function of JMeter to create the file.In bean shell scripting we are reading the JMeter variables value and storing script variable value into JMeter variable or creating Jmeter variable

Here is the  Bean Shell script :

import org.apache.jmeter.services.FileServer;
import org.apache.jmeter.services.FileServer;
import java.util.Date;
import java.text.SimpleDateFormat;

SimpleDateFormat formatter = new SimpleDateFormat( "yyyyMMddHHmmss" );  
String datetime = formatter.format( new java.util.Date() ); 

// Get current running counter value(C refrence name deifned in counter config element)

String counter= vars.get("C");

// Get Jmeter variable value in bean shell script by vars.get method

String timer= vars.get("JmeterTimerVariable");

// here JmeterTimerVariable defined in User Defined variable  config element


//  Display value in Console 

System.out.println("Current counter value = " + counter);

System.out.println("JmeterTimerVariable value = " + timer);


// Store beanshel script variable into Jmeter variable by using vars.put method and we will user this jmetervariable in Wikisearch http request

vars.put("JmeterSearchVariable",datetime+counter);

// get JmeterSearchVariable value in beanshell script

String SearchVariable = vars.get("JmeterSearchVariable");

System.out.println("SearchVariable value = " + SearchVariable);

// Here we can get the directory path of Jmeter script file

String DirPath = FileServer.getFileServer().getBaseDir();

// write into jmeter.log file under Jmeter/bin directory

log.info(DirPath);

System.out.println("Directory path of Jmeter script file = " + FileServer.getFileServer().getBaseDir());

// we will create a file under directory of jmeter script file with name JmeterReords using File system True file will be created if not and data will //append into the file False will create a new file with fresh data

f = new FileOutputStream(FileServer.getFileServer().getBaseDir()+"\\JmeterReords.txt", true); 
p = new PrintStream(f); 
// write data into file 
p.println("Current counter value = " + counter);
p.println("JmeterTimerVariable value = " + timer);
p.println("Directory path of Jmeter script file = " +DirPath);
p.close();
f.close();

// if you want to create unique file for each loop counter refer below script

String uniquefilename = timer+counter;
f = new FileOutputStream(FileServer.getFileServer().getBaseDir()+"\\"+uniquefilename+".log", true); 
p = new PrintStream(f); 
// write data into file 
p.println("Current counter value = " + counter);
p.println("JmeterTimerVariable value = " + timer);
p.println("Directory path of Jmeter script file = " +DirPath);
p.close();
f.close();





Here is the structure of JMeter script

Jmeter Bean Shell script
Jmeter bean Shell script


Wednesday 9 January 2013

Perform Mouse, drag-and-drop, sliding, selecting multiple Action in Webdriver/Selenium 2

Perform Mouse, drag-and-drop, sliding, selecting multiple Actions in Web driver/Selenium 2 

Solution

The example code below shows some examples where we can use the Actions interface of Selenium WebDriver.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package selenium2.examples;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ActionExample {
    private static WebDriver driver;

    @BeforeClass
    public void setUp() {
        driver = new FirefoxDriver();
    }

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

    @Test
    public void draggable() {
        driver.get("http://jqueryui.com/demos/draggable/");

        WebElement draggable = driver.findElement(By.id("draggable"));
        new Actions(driver).dragAndDropBy(draggable, 120, 120).build()
                .perform();
    }

    @Test
    public void droppable() {
        driver.get("http://jqueryui.com/demos/droppable/");

        WebElement draggable = driver.findElement(By.id("draggable"));
        WebElement droppable = driver.findElement(By.id("droppable"));
        new Actions(driver).dragAndDrop(draggable, droppable).build().perform();
    }

    @Test
    public void selectMultiple() throws InterruptedException {
        driver.get("http://jqueryui.com/demos/selectable/");

        List<WebElement> listItems = driver.findElements(By
                .cssSelector("ol#selectable *"));

        Actions builder = new Actions(driver);
        builder.clickAndHold(listItems.get(1)).clickAndHold(listItems.get(2))
                .click();

        Action selectMultiple = builder.build();
        selectMultiple.perform();
    }

    @Test
    public void sliding() {
        driver.get("http://jqueryui.com/demos/slider/");

        WebElement draggable = driver.findElement(By
                .className("ui-slider-handle"));
        new Actions(driver).dragAndDropBy(draggable, 120, 0).build().perform();
    }
}

Which one is right ?

Translate







Tweet