Saturday 30 April 2016

Web Service Testing (using Client security certificate .pfx format) by using SOAP UI Tool





Please follow below steps for working with web service

Test Web Service URL = http://www.webservicex.com/currencyconvertor.asmx?WSDL
Test Client Security certificate (.pfx) = c:\test-certificate.pfx
Test Client Security certificate Password = 1234




1) create SOAP Project

- File -> New SOAP Project -> Enter Project Name -> Click ok

2) Add WSDL Link

Right Click on project -> Add Wsdl -> Enter Web Service URL -> Click ok

Ex : http://www.webservicex.com/currencyconvertor.asmx?WSDL



3) Import client Security certificate 


i) Add Certificate in Kestores

Double Click on Project -> WS-Security Configurations tab-> Keystores tab -> Click + (Add New keysore to this config) -> Select file (c:\test-certificate.pfx) -> enter password (1234) -> click ok -> The Status automatically changed to "OK" (If NOT issue in certificate).


ii) Outgoing WS-Security Configurations

- Click + (Add New Outgoing WSS Configuration) -> Enter Any name for outgoing WSS Configuration (Ex: Test_OUtgoing)-> Click "ok"

- Click + (Add new WSS Entry) -> Select Type of Entry to Add is "Signature".

- double click on "Signature" for configuration (Add below value)

Keystore = Select certificate from drop down (c:\test-certificate.pfx)
Alias = Select Alis from drop down
Password = 1234  (Test Client Security certificate Password)
Key Identifier Type = "Binary Security Token"
Signature Algorithm =
Signature canonicalization =
Digest Algorithm =
Use Single Certificate = check the checkbox


iii) incoming WS-Security Configurations

- Click + (Add New Incoming WSS Configuration) -> Enter Any name for Incoming WSS Configuration (Ex: Test_Incoming) -> Click "ok"

- Select Decrypt Keysore and Signature keystore from drop down



iv) Save all the changes



4) Add New Authorization


- Double click on "Request1" under the project
- Click "Auth" (Authentication and Security related settings) in below Request1
- Select Authorization as "Add New Authorization.."
- Select Authorization Type as "Basic"
- Select outgoing WSS = Test_OUtgoing (Which we create Outgoing WS-Security Configurations)
- Select Incoming WSS = Test_Incoming (Which we create incoming WS-Security Configurations)



5) SOAP Request

Double click on Request1 under the project and enter below mentioned code

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:ConversionRate>
         <web:FromCurrency>AFA</web:FromCurrency>
         <web:ToCurrency>DZD</web:ToCurrency>
      </web:ConversionRate>
   </soapenv:Body>
</soapenv:Envelope>


And click Run (Trigger) the SOAP Request


6) SOAP Response

We are getting the below responce once we triggered the


<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <ConversionRateResponse xmlns="http://www.webserviceX.NET/">
         <ConversionRateResult>-1</ConversionRateResult>
      </ConversionRateResponse>
   </soap:Body>
</soap:Envelope>

Web Service Testing (With out using security certificate) by using SOAP UI Tool





Please follow below steps for working with web service

Test Web Service URL = http://www.webservicex.com/currencyconvertor.asmx?WSDL




1) create SOAP Project

- File -> New SOAP Project -> Enter Project Name -> Click ok

2) Add WSDL Link

Right Click on project -> Add Wsdl -> Enter Web Service URL -> Click ok

Ex : http://www.webservicex.com/currencyconvertor.asmx?WSDL

3) SOAP Request

Double click on Request1 under the project and enter below mentioned code

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:ConversionRate>
         <web:FromCurrency>AFA</web:FromCurrency>
         <web:ToCurrency>DZD</web:ToCurrency>
      </web:ConversionRate>
   </soapenv:Body>
</soapenv:Envelope>


And click Run (Trigger) the SOAP Request


4) SOAP Response

We are getting the below responce once we triggered the


<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <ConversionRateResponse xmlns="http://www.webserviceX.NET/">
         <ConversionRateResult>-1</ConversionRateResult>
      </ConversionRateResponse>
   </soap:Body>
</soap:Envelope>


How to add the attribute in XML document to send the SOAP request message.



Consider below example


<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">
            <SOAP-ENV:Header/>
           <SOAP-ENV:Body xmlns:example="http://ws.cdyne.com/"  wsu:Id="id-123456789" >
               <example:VerifyEmail>
                   <example:email>mutantninja@gmail.com</example:email>
   <CDH/>
                   <example:LicenseKey>
<ABC>123</ABC>
   </example:LicenseKey>


               </example:VerifyEmail>
           </SOAP-ENV:Body>
     
</SOAP-ENV:Envelope>








 MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
 SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();

envelope.addNamespaceDeclaration("example", "http://ws.cdyne.com/");

SOAPBody soapBody = envelope.getBody();

soapBody.addNamespaceDeclaration("example", "http://ws.cdyne.com/");
//  soapBody.addAttribute(envelope.createName("wsu:Id"), "id-123456789");
   
QName attributeName = new QName("wsu:Id");
soapBody.addAttribute(attributeName, "id-123456789");
     


SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example").addTextNode("mutantninja@gmail.com");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("CDH");
SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("LicenseKey", "example")
SOAPElement soapBodyElem3 = soapBodyElem3.addChildElement("ABC").addTextNode("123");



MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");
soapMessage.saveChanges();




by using above code we can add the attribute value.

How to construct the XML document by using java code to send the SOAP request




Consider below Web Service URL and XML document.


Web Service URL = http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx


<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">
            <SOAP-ENV:Header/>
           <SOAP-ENV:Body>
               <example:VerifyEmail>
                   <example:email>mutantninja@gmail.com</example:email>
                   <example:LicenseKey>123</example:LicenseKey>
               </example:VerifyEmail>
           </SOAP-ENV:Body>
     
</SOAP-ENV:Envelope>



Code for construct the XML document by using java code to send the SOAP request


 MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
 SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();

envelope.addNamespaceDeclaration("example", "http://ws.cdyne.com/");

SOAPBody soapBody = envelope.getBody();


SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example").addTextNode("mutantninja@gmail.com");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example").addTextNode("123");


MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");
soapMessage.saveChanges();



By using above code we can construct the XML document by using java code to send the SOAP request message.


How to send the SOAP request message (Construct the XML document by using jave) to get the SOAP response message




We need to follow the below information for send the SOAP request and get the SOAP response by using below web service URL


Web Service URL = http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx



<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">
            <SOAP-ENV:Header/>
           <SOAP-ENV:Body>
               <example:VerifyEmail>
                   <example:email>mutantninja@gmail.com</example:email>
                   <example:LicenseKey>123</example:LicenseKey>
               </example:VerifyEmail>
           </SOAP-ENV:Body>
     
</SOAP-ENV:Envelope>

http://stackoverflow.com/questions/19291283/soap-request-to-webservice-with-java


Code:



import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class SOAPClientSAAJ {

    /**
     * Starting point for the SAAJ - SOAP Client Testing
     */
    public static void main(String args[]) {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            // Process the SOAP Response
            printSOAPResponse(soapResponse);

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("Error occurred while sending SOAP Request to Server");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://ws.cdyne.com/";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("example", serverURI);


        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
        soapBodyElem1.addTextNode("mutantninja@gmail.com");
        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
        soapBodyElem2.addTextNode("123");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");

        soapMessage.saveChanges();

        /* Print the request message */
        System.out.print("Request SOAP Message = ");
        soapMessage.writeTo(System.out);
        System.out.println();

        return soapMessage;
    }

    /**
     * Method used to print the SOAP Response
     */
    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }

}



XPATH Functions




1) Node Set Function
2) String Function
3) Boolean Function
4) Number Function




1) Node Set Function

-> last()

Identify the last item of current node set.

Ex : https://www.google.co.in

//div[@id='sfdiv']//input[last()]  -> its identify last input tag



-> position()

Identify the item as per the postion. Position value start from 14.

Ex : https://www.google.co.in

//div[@id='sfdiv']//input[position()=1]  -> its identify first input tag



-> count()

Its count the number of element from context node.


Ex : https://www.google.co.in

count(//div[@id='sfdiv']//input)  -> o/p=3, Return total number of input element.



-> local-name()


Instead of element tag(//div)) we can use local-name() (local-name()='div')

Ex : https://www.google.co.in

local-name(//div[@id='sfdiv']//input)

//*[local-name()='div' and starts-with(@id,'sfdiv')]//input



-> id()

Its returns elements which ID specified in code


Ex : https://www.google.co.in

id("sfdiv")/div//input  -> o/p=3, Identify all 3 input tag.


The xpath id("sfdiv")/div//input  and //div[@id='sfdiv']//input both are same.


**********************************************************************



2) String Functions


-> String()

we can identify the object by using text. Both Text() and String() are same.


Ex : https://www.google.co.in

//div[@id='gbw']//a[String()='Gmail']

//div[@id='gbw']//a[text()='Gmail']

Both are same.


o/p identify the object which having the GMAIL text.



-> Concat()

Its Used to concatenate the two string


Ex : https://www.google.co.in


concat(//div[@id='gbw']//a[text()],' ',//div[@id='gbw']//a[text()])

O/p String: Gmail Gmail

concat(//div[@id='gbw']//a[text()],' ',//div[@id='gbw']//div[2]//a[text()])


O/p String: Gmail Images




-> starts-with()


Its identify the object by using text which starts given string.


Ex : https://www.google.co.in

//*[local-name()='div' and starts-with(@id,'sfdiv')]//input


o/p identify all three objects.



-> Contains()

Its identify the object which contains the text

Ex: https://www.google.co.in


//div[contains(id,'sfdiv']//input

o/p identify all three objects.



-> substring-before()

Its retrives the substring of first argument

Ex : https://www.google.co.in

substring-before(//div[@id='gbw']//a[text()],'a')

o/p  String: il


we can use this xpath like below for identify the object

//div[@id='gbw']//a[contains(.,substring-before(//div[@id='gbw']//a[text()],'a'))]





-> substring-after()

Its retrives the substring of first argument

substring-after(//div[@id='gbw']//a[text()],'a')


O/P     String: il


we can use this xpath like below for identify the object

//div[@id='gbw']//a[contains(.,substring-after(//div[@id='gbw']//a[text()],'a'))]



*******************************************************************8


3) Boolean Function


-> Boolean boolean(object)

Converts the argument to a Boolean value.

Example - boolean(/journal/article/author/last[.='Jones'])



-> Boolean not(boolean)

Negates the boolean value.

Example - not(/journal/article/author/last[.='Jones'])


***************************************************


4) Number Function


Will update shortly




https://docs.oracle.com/cd/E35413_01/doc.722/e35419/dev_xpath_functions.htm#autoId19

Behaviour Driven Development framework - Cucumber


Cucumber

Cucumber is tool based on Behaviour Driven Development framework which is used with selenium for perform acceptance testing.


It allows automation of functional validation in easily readable and understandable format (like plain English) to Business Analysts, Developers, Testers, etc.


@Test
public void should_do_something() {
    // given
    Something something = getSomething();

    // when
    something.doSomething();
    // then
    assertSomething();

    // when
    something.doSomethingElse();
    // then
    assertSomethingElse();
}



Behavior Driven Development is extension of Test Driven Development and it is used to test the system rather than testing the particular piece of code.