Wednesday, 15 March 2017

Java reflection

Reflection in Java is a very powerful concept and it’s of little use in normal programming but it’s the backbone for most of the Java, J2EE frameworks. Some of the frameworks that use java reflection are:
JUnit – uses reflection to parse @Test annotation to get the test methods and then invoke it.
Spring – dependency injection, read more at Spring Dependency Injection
Tomcat web container to forward the request to correct module by parsing their web.xml files and request URI.
Eclipse auto completion of method names
Struts
Hibernate
The list is endless and they all use java reflection because all these frameworks have no knowledge and access of user defined classes, interfaces, their methods etc.


http://www.journaldev.com/1789/java-reflection-example-tutorial#reflection-in-java

Tuesday, 14 March 2017

json schema validators

http://wilddiary.com/validate-json-against-schema-in-java/

https://jsonschema.net/#/

http://jsonviewer.stack.hu/


sample json schema:
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "mytitle",
  "description": "my description",
  "type": "object",
  "required": [
    "data",
    "status"
  ],
  "properties": {
    "status": {
      "type": "number"
    },
    "data": {
      "type": "array",
      "minItems": 1,
      "additionalItems": true,
      "items": {
        "required": [
          "myStatus1",
          "myMediaPlanType",
          "myBudget",
          "myAccountID",
          "myAccountName",
          "myNotes",
          "myMediaPlans",
          "myAuditData",
          "myUsage1",
          "myMediaPlansCount",
          "myIsParentCrossBrandCampaign",
          "myCrossBrandIDs",
          "myCrossBrandChildCampaign",
          "myID",
          "myName"
        ],
        "myStatus1": {
          "type": "number"
        },
        "myMediaPlanType": {
          "type": "number"
        },
        "myBudget": {
          "type": "number"
        },
        "myAccountID": {
          "type": "number"
        },
        "myNotes": {
          "type": "string"
        },
        "myMediaPlans": {
          "type": "array"
        },
        "myAuditData": {
          "type": "object",
          "required": [
            "myCreatedByID",
            "myLastUpdatedByID",
            "myCreatedDate",
            "myLastUpdatedDate"
          ],
          "properties": {
            "myCreatedByID": {
              "type": "number"
            },
            "myLastUpdatedByID": {
              "type": "number"
            },
            "myCreatedDate": {
              "type": "string"
            },
            "myLastUpdatedDate": {
              "type": "string"
            }
          }
        },
        "myUsage1": {
          "type": "number"
        },
        "myMediaPlansCount": {
          "type": "number"
        },
        "myIsParentCrossBrandCampaign": {
          "type": "string"
        },
        "myCrossBrandIDs": {
          "type": "array"
        },
        "myCrossBrandChildCampaign": {
          "type": "array"
        },
        "myID": {
          "type": "number"
        },
        "myName": {
          "type": "string"
        }
      }
    }
  }
}




Tuesday, 7 March 2017

Write into file in append mode / file writer

package com.google.email;

import java.io.*;
import java.io.IOException;

public class WriteTextUtil {
public void writeInAppendMode(String path, String strFinal) throws IOException {
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter(path, true);
bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw);
out.println(strFinal);
out.println("\n");

} finally {
if (bw != null)
bw.close();

if (fw != null)
fw.close();
}
}
}

Saturday, 4 March 2017

Appium on OSX

Appium-OSX Tutorial

Required software:

Java: JDK


Any java IDE: Eclipse / Intellij idea

Appium java client : https://mvnrepository.com/artifact/io.appium/java-client/4.1.2

Selenium stand alone server : https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server-standalone/2.53.0

Xcode : developer.apple.com/xcode/downloads : version 5.1 or higher (i.e 6.1 )
( for opening Iphone simulator)
-          Command line tools (OS X 10.9).pkg
-          Xcode_6.1.1.dmg

Device Configuration : Enable UI automation in mobile (1.Connect your mobile to laptop.  2. Go to settings 3. Developper 4. Enable UI automation

Iphone Simulator verification:

Open Xcode

Click on Create a new Xcode project – Choose single view application – product name : test, 

language : Objective c, Device: Universal
Next
Choose folder name
Create
Click on Run

If it opens a simulator, you are done and then you can close everything.

Installation:

Install Appium  and choose IOS

Click on Apple symbol to add or modify settings

Basic  Tab:

Provide App path : App which you want to automate ( download app in your local  .ipa extension)

Force Device : check the check box and choose your connected device

Platform version: UDID- Unique Device Id: (open xcode -> Devices-> select the device you want to 
test , you can find Identifier , this is the UDID ) (This is basically alias name , give Name: KranthiIphone6)

Full Rest : check the checkbox – every time we run test we have a clean start

Advanced tab:

Xcode path : make sure xcode path pointed to actual installation
Click on launch button
Once you see 200 in the console, you can start inspecting elements
Click on Inspector UI ( magnifier symbol )
This will install the app on your mobile and shows screen in inspector
We can choose any element and find xpath for the same

Appium setup Check:
Click on Appium Doctor button

Code sample:
Open Eclipse
Create a maven project
Add appium-client and selenium server standalone jar to build path

Public class FirstTest{
Public static void main(String[] args){
DesiredCapabilities cap=new DesiredCapabilities();
Cap.setCapabilities(“platformName”,”ios”);
Cap.setCapabilities(“platformVersion”,”8.1”);
Cap.setCapabilities(“deviceName”,” KranthiIphone6”);// this we configured earlier in appium server
IOSDriver driver=new IOSDriver(new URL(http://127.0.0.1:4723/we/hub”),cap);
Driver.manage.timeouts().implicitwiat(60,TimeUnits.SECONDS);
Driver.findElement(By.xpath(“xpath”)).click();
//we can build our own xpaths by copying xpath of the device screen and then we can write xpaths
//xpathtester.com/xpath
Driver.quite();
}







Friday, 17 February 2017

release the port in windows OS


Windows OS:


cmd> netstat -ano | findstr 8080

lists who is using port 8080 and related process id

port  - pid
8080 - 13892

do you want to kill process ?
taskkill /f /pid 13892

SUCCESS

Thursday, 16 February 2017

Read soap xml file using xpath



import org.apache.xmlbeans.impl.xb.xsdschema.FieldDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.FileReader;


public class SoapReaderUsingXpath {
    public static void main(String[] args){
        String result ="";
        String soapXmlFile;
        DocumentBuilderFactory dbf;
        DocumentBuilder db;
        Document doc;
        XPath xpath;
        Node nl;
        soapXmlFile="C:\\wssoap.xml";
        String myXpath="//Element/Body/Node1/text()";
        try{
            dbf=DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(false);
            db=dbf.newDocumentBuilder();
            doc=db.parse(new InputSource(new FileReader(soapXmlFile)));
            xpath= XPathFactory.newInstance().newXPath();
            nl=(Node)xpath.evaluate(myXpath,doc.getDocumentElement(), XPathConstants.NODE);
            result=nl.getNodeValue();
        }
        catch (Exception ex){}

    }
}

Read text file?




BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
} finally {
    br.close();
}