Thursday, March 7, 2019

How to update jenkins in ubentu 7

  1.       Locate the jenkins.war in your system.
a.       Go to manage Jenkins --> system information --> it will tell you the location of Jenkins.war file.

  2.       Download the latest Jenkins.war and keep it in some folder.  Say /usr/lib/abc

  3.       stop the jenkins service sudo service jenkins stop

  4.       cd to location obtained from step 1. If you wish to back up the old     Jenkins.war
a.       backup the jenkins.war --> mv jenkins.war jenkins-bkup.war

  5.       Now copy the latest war from step 2 to old location.
a.       cd to folder where latest war is placed. Same as step 2
b.      cp jenkins.war /usr/jenkins/lib/jenkins.war [this is dummy path, please pass the exact path from step 1]

  6.       sudo service Jenkins start

Monday, February 11, 2019

How to return all the values of a column in a web table. Selenium and xpath

Fellow automation developers, Many times during web automation we come across scenarios where we want to retrieve all the values of a column based upon the column name of a dynamic web table.

Consider in the below image, you want to get all company names. 


Or you want to get all the Contact names. 

Below are the XPath and selenium-java method to find and return all name of the company column. 



/**
  * @author diehardtechy
  * @param columnName
  *            : Column name whose all
  *            <td>values needs to be returned.
  * @return List<String> 
  */
 public List<String> getColumnValues(String columnName, String textToSearch) {

  String xpath = "//table[@id='customers']/tbody/tr/td[count(//table/tbody/tr/th[.=\"" + columnName
    + "\"]/preceding-sibling::th)+1]";

  List<WebElement> elements = d.findElements(By.xpath(xpath));
  return elements.stream().map(t->t.getText()).collect(Collectors.toList());
  
 }


Tuesday, January 22, 2019

Java 8: How to check date is valid future date.

Check if a date is a valid future date. 

As a developer we come across many scenarios where we want to validate the user selected date is the correct future date.  

Java 8 provides many useful classes and methods under Date Time API. Below code snippet checks if the date is a valid future date. 



        /**
  * DateTime function which checks for the valid future date.
  * 
  * @author ajain5
  * @param futureDate
  *            : in dd-mm-yyyy or dd/mm/yyyy format
  * @return true if futureDate isAfter currentDate
  */
 public boolean isValidFutureDate(String futureDate) {
  String date[] = futureDate.split("[\\/-]");
  int day = Integer.parseInt(date[0]);
  int month = Integer.parseInt(date[1]);
  int year = Integer.parseInt(date[2]);

  LocalDate today = LocalDate.now();
  return today.isBefore(LocalDate.of(year, month, day));

 }


You can test the above code using below driver program. 

System.out.println(new Ti().isValidFutureDate(args[0]));

Response after running the above program :

Current date : 22-01-2019

The above program was tested on 22-Jan-2019 .


Wednesday, January 31, 2018

Version one useful Restful web service to get the test case detail using test case id.



version one api to get the Test case details using Test Case ID 

http://Your_Version_ONE_URI/rest-1.v1/Data/Test?where=Number="AT-57708"


Where, Your_Version_ONE_URI looks like below :

--> https://www3.v1host.com/DieHardTechy

NOTE: 
  • To call the above api from postman you need to use basic authentication with your version one username and password.
  • If AT-57708 id exists in Version one , Above API gives the XML response containing details about that Test case. 


Sample response



<?xml version="1.0" encoding="UTF-8"?>
<Assets total="1" pageSize="2147483647" pageStart="0">
    <Asset href="/DieHardTechy/rest-1.v1/Data/Test/1215543" id="Test:1215543">
        <Attribute name="AssetType">Test</Attribute>
        <Relation name="GeneratedFrom" />
        <Relation name="SecurityScope">
            <Asset href="/DieHardTechy/rest-1.v1/Data/Scope/8686" idref="Scope:8686" />
        </Relation>
        <Relation name="Super" />
        <Relation name="Team" />
        <Relation name="Parent">
            <Asset href="/DieHardTechy/rest-1.v1/Data/Story/408352" idref="Story:408352" />
        </Relation>
        <Relation name="Status" />
        <Relation name="Category">
            <Asset href="/DieHardTechy/rest-1.v1/Data/TestCategory/116" idref="TestCategory:116" />
        </Relation>
        <Relation name="Timebox" />
        <Relation name="Scope">
            <Asset href="/DieHardTechy/rest-1.v1/Data/Scope/8686" idref="Scope:8686" />
        </Relation>
        <Attribute name="Number">AT-57708</Attribute>
        <Attribute name="VersionTested" />
        <Attribute name="ActualResults" />
        <Attribute name="ExpectedResults">This is expected result</Attribute>
        <Attribute name="Steps">Subscribe to die hardtechy blog</Attribute>
        <Attribute name="Order">1077139536</Attribute>
        <Attribute name="Description">Call GET API without passing any Parameter</Attribute>
        <Attribute name="Name">Subscribe</Attribute>
        <Attribute name="AssetState">64</Attribute>
        <Attribute name="SecurityScope.Name">DieHardTechy API</Attribute>
        <Attribute name="Parent.Name">Regression Test Cases</Attribute>
        <Attribute name="Parent.Number">B-09384</Attribute>
        <Attribute name="Status.Name" />
        <Attribute name="Category.Name">Functional</Attribute>
        <Attribute name="Timebox.Name" />
        <Attribute name="Scope.Name">DieHardTechy API</Attribute>
        <Attribute name="TaggedWith">
            <Value>SANITY</Value>
        </Attribute>
        <Attribute name="Ideas" />
        <Relation name="Owners">
            <Asset href="/DieHardTechy/rest-1.v1/Data/Member/153428" idref="Member:153428" />
        </Relation>
        <Attribute name="Owners.Name">
            <Value>xyz abc</Value>
        </Attribute>
        <Attribute name="Owners.Nickname">
            <Value>xyz</Value>
        </Attribute>
    </Asset>
</Assets>



Wednesday, January 3, 2018

What is the best way to check element not present in webpage using selenium webdriver ?


Best Way of checking element not present in webpage using selenium webdriver ?


Often encountered a situation where you have to verify an element is not present in the web page. 

using 


try {

        driver.findElement(locatorKey);

        Assert.assertTrue(false);

    } catch (org.openqa.selenium.NoSuchElementException e) {

        Assert.assertTrue(true);

    }

 


is not a right approach. Because this is layman way of asserting absence of web element.

As per selenium documantaion, best way to check if element is not present is using findElements and then asseting on the size of the list returned. 

findElements does not throw any exception in case if an element is not present. Asserting on 0 length is more appropriate for absence of an element. 

Selenium official comment : 




Jenkins Restful API with Java : Create, abort, build new jobs using API


Jenkins RestFul APIs for Abort, Creating and Building a Job.

How to abort a Jenkins Job using REST API in java.


Below is the Java method to abort a jenkins Job using REST API.

Please note you can use your own api client to post this request.

Please change the IP and PORT according to your Jenkins IP and PORT.


public  String abortJob(String jobName, String buildNumber) {
  String abortURL = "http://192.168.1.1:8080/job/" + jobName + "/"+buildNumber+"/stop";
  Client client = Client.create();

  client.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter("taas", "taas"));
  WebResource webResource = client.resource(abortURL);
  ClientResponse response = webResource.type("application/xml").post(ClientResponse.class, null);
  String jsonResponse = response.getEntity(String.class);
  client.destroy();
  return jsonResponse;

 }

How to get the list of all idle nodes from Jenkins using REST API in java.

Please change ip address with your jenkins IP and port

REQUEST TYPE : GET


public ArrayList<String> getIdleVmsList() {

  String response = callAPI("http://192.168.1.1:8080/computer/api/json");
  return parseResponse(response);

 }

 public ArrayList<String> parseResponse(String response) {

  ArrayList<String> idleVm = new ArrayList<String>();

  JSONObject stat = new JSONObject(response);
  JSONArray computer = stat.getJSONArray("computer");

  for (int i = 0; i < computer.length(); i++) {
   JSONObject info = (JSONObject) computer.get(i);
   info.getJSONObject("monitorData");
   String master = (String) info.get("displayName");
   Boolean vmOfflineStatus = (Boolean) info.get("offline");
   Boolean vmIdleStatus = (Boolean) info.get("idle");
   if (!vmOfflineStatus && vmIdleStatus && !master.equalsIgnoreCase("master")) {
    idleVm.add((String) info.get("displayName"));

   }

  }

  return idleVm;

 }

 public static String callAPI(String Url) {
  Client client = Client.create();
  try {
   client.addFilter(new HTTPBasicAuthFilter("username", "pwd"));
   WebResource webResource = client.resource(Url);
   ClientResponse response = webResource.get(ClientResponse.class);
   String jsonResponse = response.getEntity(String.class);

   client.destroy();
   return jsonResponse;
  } catch (Exception e) {
   return "couldnot call jenkins get API";
  }

 }

How to build a job using REST API. 


REST URL : http://192.168.1.1:8080/job/jobName/build?delay=0

METHOD TYPE : POST

where JobName is the name of your jenkins job.


Please contact us for more Restful web services of Jenkins. 

 

© 2014 DieHardTechy . All rights resevered. Designed by Diehardtechy