Qaf: API Automation using QAF

Created on 29 Mar 2017  ·  7Comments  ·  Source: qmetry/qaf

Hi Team,

We are trying to implement the API automation using QAF.

Can anyone please suggest on the below queries?

  1. Syntax or method to GET method in QAF? We are not sure which method to select from the list of GET options. Attached is the screenshot.

Struck at --> new RestTestBase().getWebResource(getBundle().getString("ws.endurl", url), "/application.json").** --> which GET method to use?

  1. Once we received the response, how to extract the contents from the JSON response and validate the same using QAF?

Thanks in advance!!

Regards,
Prem
capture_webserviceget

help wanted webservice

Most helpful comment

Hi @APrem

This looks like the HTTP Client is not trusting the certificate.

For this - you have to extend the RestClientFactory with the code snippet from here:
https://gist.github.com/outbounder/1069465

so your extended class will looks like as below and you can place this inside any of your java package:
`package ;

import com.qmetry.qaf.automation.ws.rest.RestClientFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;

import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class ClientHelper extends RestClientFactory {

public static ClientConfig configureClient() {
    TrustManager[ ] certs = new TrustManager[ ] {
            new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
            }
    };
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (java.security.GeneralSecurityException ex) {
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

    ClientConfig config = new DefaultClientConfig();
    try {
        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(
                new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                },
                ctx
        ));
    } catch(Exception e) {
    }
    return config;
}

@Override
protected  Client createClient() {
    return Client.create(ClientHelper.configureClient());
}

}`

Once you do this, you have to register this class inside application properties:
rest.client.impl=<Your Package Name>.ClientHelper

All 7 comments

If you are expecting any POJO you can provide that class or a String.class or generic response ClientResponse.class

You can use ws-support for ready to use steps.

Thanks for the response!

Hoping the below portion of code shall be helpful. But we require the values of paramers.
String resource, Map params

What's ----> String resource
What's ---->Map params

private static void requestFor(String resource, Map params) {

  • WebResource webResource = new RestTestBase().getWebResource(
  • getBundle().getString("ws.endurl", ApplicationProperties.SELENIUM_BASE_URL.getStringVal()), resource);
  • if (null != params && !params.isEmpty()) {
  • MultivaluedMap mparams = new MultivaluedMapImpl();


    • for (String key : params.keySet()) {

  • mparams.add(key, params.get(key));
  • }
  • webResource = webResource.queryParams(mparams);
  • }
  • webResource.get(ClientResponse.class);
  • }

We tried with the below portion of code and we are getting an exception as below.

Tried script:

    WebResource webResource = new RestTestBase().getWebResource(getBundle().getString("ws.endurl", url));
    webResource.get(ClientResponse.class);

Error:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Hi @APrem

This looks like the HTTP Client is not trusting the certificate.

For this - you have to extend the RestClientFactory with the code snippet from here:
https://gist.github.com/outbounder/1069465

so your extended class will looks like as below and you can place this inside any of your java package:
`package ;

import com.qmetry.qaf.automation.ws.rest.RestClientFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;

import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class ClientHelper extends RestClientFactory {

public static ClientConfig configureClient() {
    TrustManager[ ] certs = new TrustManager[ ] {
            new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
            }
    };
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (java.security.GeneralSecurityException ex) {
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

    ClientConfig config = new DefaultClientConfig();
    try {
        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(
                new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                },
                ctx
        ));
    } catch(Exception e) {
    }
    return config;
}

@Override
protected  Client createClient() {
    return Client.create(ClientHelper.configureClient());
}

}`

Once you do this, you have to register this class inside application properties:
rest.client.impl=<Your Package Name>.ClientHelper

Hi Prem,
Hope above comment will help you to get your problem resolve.

I would like to request you move the question to community support forum like stackoverflow with QAF tag or linked in group. When you are asking on stackoverflow add tag qaf to get quick response.

Thanks,
Chirag

Thanks for the response.

Added ClientHelper class and now we could able to see the response.
But the URL which ever is given inside .getWebResource(getBundle().getString("ws.endurl", url)); is having the api key. Hence, Question mark symbol "?" is there in the URL. This piece of code is taking the "?" as "%3F" and printing the console out put as below.

Expected: https://sample.url.com/xxxxxxxxxxxxx?apikey=l7xxfc8df89f313944a89e280481e09bb411
Actual: https://sample.url.com/xxxxxxxxxxxxx%3Fapikey=l7xxfc8df89f313944a89e280481e09bb411

Because of this conversion we get error in output. Let us know how to handle this.

Console output:
1 * Client out-bound request
1 >> GET https://sample.url.com/xxxxxxxxxxxxx%3Fapikey=l7xxfc8df89f313944a89e280481e09bb411
1 * Receive Start: 87
1 * Received: delta: 87 bytes: 87
Finished1 * Finished
1 * Client in-bound response
1 << 401
1 << Server: Apache-Coyote/1.1
1 << Content-Length: 87
1 << Date: Fri, 31 Mar 2017 14:39:19 GMT
1 << Content-Type: text/plain;charset=UTF-8
1 <<
{
"errors": [
{
"message": "Invalid apikey",
"code": 4010
}
]
}

I have passed the apikey with Header object as below and able to get response.

Builder builder = webResource.getRequestBuilder();
builder.header(apikey, apiValue);
builder.get(ClientResponse.class);

Thank You!

Thank you @cjayswal @thetechnocrat for the support !!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cjayswal picture cjayswal  ·  17Comments

raviguptasmarsh picture raviguptasmarsh  ·  8Comments

BChitrakannan picture BChitrakannan  ·  9Comments

Nandesh7 picture Nandesh7  ·  8Comments

Rupak-66 picture Rupak-66  ·  12Comments