Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Wednesday, July 17, 2019

How to send attachment in Slack channel using Restful Webservice



Slack : What comes to your mind when you hear word Slack ? It is probably one of best collaboration tool available in the market. Now a days Slack is also widely used across multiple organization of all level. [Big, mid or small] 


Now since everyone is active on Slack , it is of extreme importance to send messages to slack channels. This is when the need arise to send automated notifications. Slack provides a lot of rest api's to accomplish this task. 

In this post, We will cover how to send attachments in Slack channels using Slack endpoint with Java. 

Prerequisite: 
  • You should have slack authentication token generated. 
  • Name of the slack channel where the message will be delivered. 



package com.dht.test;

import java.io.File;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;

public class SlackChannelAttachmentUtil{

 public static void main(String[] args) {
  try {
   String url = "https://slack.com/api/files.upload";
   HttpClient httpclient = HttpClientBuilder.create().disableContentCompression().build();
   HttpPost httppost = new HttpPost(url);
   MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
   reqEntity.addBinaryBody("file", new File("C:\\dht.properties"));
   reqEntity.addTextBody("channels", "attachment-testing");
   reqEntity.addTextBody("token","xoxp-4735837518-xxxxxx");
   reqEntity.addTextBody("media", "file");
   reqEntity.addTextBody("initial_comment", "Hello This is from code");

   httppost.setEntity(reqEntity.build());
   HttpResponse execute = httpclient.execute(httppost);
   System.out.println(execute.getStatusLine().getReasonPhrase());
   System.out.println(execute.getStatusLine().getStatusCode());
  } catch (Exception e) {
   System.out.println(e);
  }

 }

}



The above code will send dht.properties file to attachment
-testing slack channel. You will also see Message "Hello This is from code" along with attachment.

Tuesday, May 21, 2019

How to fix VersionOne.Parsing.MismatchedCharException !

Are you irritated because of VersionOne.Parsing.MismatchedCharException while accessing version one restful API & using where clause. 



CAUSES

Due to the use of the special character in version1 restful endpoint, this error is common. 
The 'where' clauses in a rest-1.v1 query or query.v1 query only support ASCII. So any character which is non-ascii or Unicode will result in the this error. 

SOLUTION


The 'with' clause should be used by default to avoid unsupported character issues.

It should look something like this -

Scopes.Name=$name&with=$name= XXXXX: XXXXX



Friday, May 17, 2019

Install Jenkins with Docker file & preinstalled plugins. [Copy existing .jpi/.hpi file to docker image.]


Create a new file with name as Dockefile and add the below content.

FROM jenkins/jenkins:lts
USER root
MAINTAINER diehardtechy@gmail.com


if you want to copy any existing .hpi/.jpi file in your docker image, please add the below command in Dockerfile (Eg. If we want to copy perforce.jpi plugin from local machine to docker image )

COPY perforce.jpi /var/jenkins_home/plugins/



If you want Jenkins to preinstall plugins create a plugins.txt file with below content.

pluginid : version_info (For example if you want to install notification plugin with 1.13 version, we need to specify it as below, if we don’t pass version info, it will take the latest version)

notification:1.13 
ws-cleanup:0.34 
mask-passwords:2.12.0


Content of the plugins.txt file looks like as follow.






to copy the plugins.txt file to docker image, add the below command in Dockerfile.

Wednesday, May 8, 2019

RUNNING SELENIUM TEST IN DOCKER CONTAINER USING JENKINS WITH ZALENIUM & GRADLE.


RUNNING SELENIUM TEST IN DOCKER CONTAINER USING JENKINS WITH ZALENIUM & GRADLE.

Step 1:
Pull Jenkins image and run it.
Use the below command to pull and run the Jenkins docker instance.
docker run -p 8085:8080 jenkins/jenkins:lts
once the above command is executed, you will see the below output.





As you can see from the above output “Jenkins is up and running.”
Navigate to http://localhost:8085 to access the Jenkins.
Install Git & Gradle plugin.
Step 2:
Pull Zalenium docker image and run it.
 # Pull docker-selenium
docker pull elgalu/selenium

# Pull Zalenium
 docker pull dosel/zalenium

 # Run it!
  docker run --rm -ti --name zalenium -p 4444:4444 \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v /tmp/videos:/home/seluser/videos \
      --privileged dosel/zalenium start


After the above command is executed, you can see the output as below.



From the above-highlighted content, you can see, selenium grid is up and running. Please use http://localhost:4444/wd/hub to connect to selenium hub.

Visit: http://localhost:4444/dashboard/# this will open Zalenium dashboard. Zalenium dashboard is a unique dashboard for all test execution status and report.




Visit: http://localhost:4444/grid/admin/live to see the live execution happening in docker container created by Zalenium.



Step 3:
Point your Selenium test to http://localhost:4444/wd/hub and run it.
If you wish to run this in Jenkins please use ip address of your host machine.
I have created a sample Java-Gradle project and kept it in bitbucket.
Step 4:
Create a Jenkins job, which have bitbucket configuration for above repo. And try running it. Sample configuration from Jenkins looks like below:




Thursday, March 21, 2019

How to update jenkins in ubentu 7

  This simple guide is for people who are looking to update the jenkins in ubentu box without loosing the data. 

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>