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
Showing posts with label automation testing. Show all posts
Showing posts with label automation testing. Show all posts

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.

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. 

Saturday, September 3, 2016

How to get Test Case Detail from testlink using TestLink Java API?

Getting test case detail from testlink using API is quite needful when displaying results in Reports.

To get test case related detail you will need testlink-java-api.

Prerequisite:


  1. TestLink-Java-API
  2. Java 1.7 or higher
  3. Maven

Since, TestLink-Java-API depends on mulitple other jars, it ia adviceable to create a Maven project and add the below dependencies in POM.XML


<dependencies>
  <!-- Commons APIs -->
  <dependency>
   <groupId>commons-lang</groupId>
   <artifactId>commons-lang</artifactId>
   <version>2.6</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.4</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <!-- Logging APIs -->
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-simple</artifactId>
   <version>1.7.21</version>
  </dependency>
  <!-- Configuration APIs -->
  <dependency>
   <groupId>commons-configuration</groupId>
   <artifactId>commons-configuration</artifactId>
   <version>1.10</version>
  </dependency>
  <!-- XML-RPC related APIs -->
  <dependency>
   <groupId>org.apache.xmlrpc</groupId>
   <artifactId>xmlrpc-common</artifactId>
   <version>3.1.3</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>xml-apis</groupId>
   <artifactId>xml-apis</artifactId>
   <version>2.0.2</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>commons-httpclient</groupId>
   <artifactId>commons-httpclient</artifactId>
   <version>3.1</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>br.eti.kinoshita</groupId>
   <artifactId>testlink-java-api</artifactId>
   <version>1.9.14-0</version>
  </dependency>
<dependencies>


Use the below code to retrieve Test case description, Steps to execute, Expected results etc. using External test case id.


public void getTestDetailFromTestLink() {
  String testCaseDescription = "", expectedResults = "", steps = "";
  URL url;
  try {
   url = new URL("http://localhost/testlink/lib/api/xmlrpc/v1/xmlrpc.php");

   String devKey = "5062c3fertf9b8886e0edd0ffdf6b14";
   TestLinkAPI api = new TestLinkAPI(url, devKey);
   TestCase tCase = api.getTestCaseByExternalId("P1-46039", 1);

   testCaseDescription = tCase.getSummary();

   List<TestCaseStep> list = tCase.getSteps();
   Iterator<TestCaseStep> itr = list.iterator();
   while (itr.hasNext()) {
    TestCaseStep tcs = itr.next();
    expectedResults = tcs.getExpectedResults();
    steps = tcs.getActions();

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

  System.out.println("Description :"+testCaseDescription);
  System.out.println("Expected Result :"expectedResults);
  System.out.println("Steps to Execute :"+steps);
  
 }


Above code produces the below output.

Description :Verify that on selecting Die Hard Techy from Side menu takes user to blog screen.

Expected Result :User should be navigated to Screen.

Steps to Execute :1.Open DieHardTechy website. 2.Select Die Hard Techy.

Monday, February 8, 2016

How to run two different application on two physical android devices in parallel?

Parallel execution of android applications have become a need now days given that number of test scenarios are higher. 

Parallel execution is done to achieve maximum test coverage with minimal time.

Appium provides a way for parallel execution.

To achieve this , we have to do the following steps.
  1. We need to start 2 appium instances on different ports.
  2. Boot strap port numbers for both the instances must be different.
  3. provide the unique identifier of physical devices using -U flag.
  4. If you are trying to run an Hybdrid application in parallel, make sure you start chrome driver on 2 different ports.


You can skip the point 4 if you are automating native android apps.

To start appium server you must have node.js installed or you can use default node executable residing inside appium installation directory.

1. Open a command prompt in admin mode and navigate till main.js file inside appium installation directory.

Main.js is find here: 

C:\Program Files (x86)\Appium\node_modules\appium\lib\server

2. run the below commands in different command prompt window. 

node main -p 4725 -bp 2293 -U place device 1 UDID--chromedriver-port 6000

node main -p 4724 -bp 2292 -U place device 2 UDID--chromedriver-port 6001

You will see appium server up and running on 2 different ports.


Instance 1

Instance 2


3. Now go to your testng.xml file and pass port number and device name as parameter from your testng.xml suite file.

Please make sure parallel attribute inside <suite> must be set to tests.


<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Preserve order test runs" parallel="tests">
   <test name="Test for account settings Mobile" preserve-order="true" >
        <parameter name="appName" value="accounts_582.apk" />
        <parameter name="deviceName" value="HTC Nexus 9" />   
        <parameter name="paramName" value="4724" />     
        <classes>
          <class name="omniauto.tests.Accounts_AndroidTest"></class> 
              <class name="omniauto.tests.AggregatedAccount_Settings"></class> 
              <class name="omniauto.tests.ManualAccountSettings_Test"></class> 
        </classes>
    </test>
    <test name="Test suite for account group setting Mobile" preserve-order="true" >
        <parameter name="appName" value="accountgroupsettings_582.apk" />
        <parameter name="deviceName" value="Samsung I9500 Galaxy S4" />
        <parameter name="paramName" value="4725" />
        <classes>
            <class name="omniauto.tests.Account_Group_Settings"></class>
        </classes>
    </test>
    
</suite>



4. Use that port number in forming your driver instance. 

driver = new AppiumDriver(new URL("http://127.0.0.1:"+portNum+"/wd/hub"), capabilities);

Happy learning ! :)