Qaf: 使用 QAF 的 API 自动化

创建于 2017-03-29  ·  7评论  ·  资料来源: qmetry/qaf

大家好,

我们正在尝试使用 QAF 实现 API 自动化。

任何人都可以就以下查询提出建议吗?

  1. QAF 中 GET 方法的语法或方法? 我们不确定从 GET 选项列表中选择哪种方法。 附上截图。

击中 --> new RestTestBase().getWebResource(getBundle().getString("ws.endurl", url), "/application.json")。 * * --> 使用哪个 GET 方法?

  1. 一旦我们收到响应,如何从 JSON 响应中提取内容并使用 QAF 对其进行验证?

提前致谢!!

问候,
预科
capture_webserviceget

help wanted webservice

最有用的评论

@APrem

这看起来像是 HTTP 客户端不信任该证书。

为此 - 您必须使用此处的代码片段扩展 RestClientFactory :
https://gist.github.com/outbounder/1069465

所以你的扩展类将如下所示,你可以把它放在你的任何 java 包中:
`包;

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

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

公共类 ClientHelper 扩展 RestClientFactory {

public static ClientConfig configureClient() {
    TrustManager[ ] certs = new TrustManager[ ] {
            new X509TrustManager() {
                <strong i="24">@Override</strong>
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                <strong i="25">@Override</strong>
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
                <strong i="26">@Override</strong>
                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() {
                    <strong i="27">@Override</strong>
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                },
                ctx
        ));
    } catch(Exception e) {
    }
    return config;
}

<strong i="28">@Override</strong>
protected  Client createClient() {
    return Client.create(ClientHelper.configureClient());
}

}`

完成此操作后,您必须在应用程序属性中注册此类:
rest.client.impl=<Your Package Name>.ClientHelper

所有7条评论

如果您期待任何 POJO,您可以提供该类或String.class或通用响应ClientResponse.class

您可以将ws-support用于准备使用的步骤。

感谢您的回复!

希望下面的代码部分会有所帮助。
参数

什么是 ----> 字符串资源
什么是---->地图参数

private static void requestFor(String resource, Map参数){

  • WebResource webResource = new RestTestBase().getWebResource(
  • getBundle().getString("ws.endurl", ApplicationProperties.SELENIUM_BASE_URL.getStringVal()), 资源);
  • if (null != params && !params.isEmpty()) {
  • 多值映射mparams = new MultivaluedMapImpl();


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

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

我们尝试了下面的代码部分,但我们收到了如下异常。

试过的脚本:

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

错误:

javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:
sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效认证路径

@APrem

这看起来像是 HTTP 客户端不信任该证书。

为此 - 您必须使用此处的代码片段扩展 RestClientFactory :
https://gist.github.com/outbounder/1069465

所以你的扩展类将如下所示,你可以把它放在你的任何 java 包中:
`包;

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

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

公共类 ClientHelper 扩展 RestClientFactory {

public static ClientConfig configureClient() {
    TrustManager[ ] certs = new TrustManager[ ] {
            new X509TrustManager() {
                <strong i="24">@Override</strong>
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                <strong i="25">@Override</strong>
                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }
                <strong i="26">@Override</strong>
                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() {
                    <strong i="27">@Override</strong>
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                },
                ctx
        ));
    } catch(Exception e) {
    }
    return config;
}

<strong i="28">@Override</strong>
protected  Client createClient() {
    return Client.create(ClientHelper.configureClient());
}

}`

完成此操作后,您必须在应用程序属性中注册此类:
rest.client.impl=<Your Package Name>.ClientHelper

嗨,Prem,
希望以上评论能帮助您解决问题。

我想请求您将问题移至社区支持论坛,例如带有 QAF 标签或链接到组中的stackoverflow 。 当您在 stackoverflow 上询问时添加标签qaf以获得快速响应。

谢谢,
奇拉格

感谢您的回复。

添加了 ClientHelper 类,现在我们可以看到响应。
但是在 .getWebResource(getBundle().getString("ws.endurl", url)); 中给出的 URL; 有 api 密钥。 因此,问号符号“?” 网址中有没有。 这段代码取了“?” 作为“%3F”并打印控制台输出如下。

预期: https : //sample.url.com/xxxxxxxxxxxxx? apikey =l7xxfc8df89f313944a89e280481e09bb411
实际: https : //sample.url.com/xxxxxxxxxxxxx%3Fapikey=l7xxfc8df89f313944a89e280481e09bb411

由于这种转换,我们在输出中出现错误。

控制台输出:
1 * 客户端出站请求
1 >> 获取https://sample.url.com/xxxxxxxxxxxxx%3Fapikey=l7xxfc8df89f313944a89e280481e09bb411
1 * 接收开始:87
1 * 接收:增量:87 字节:87
已完成1 * 已完成
1 * 客户端入站响应
1<<401
1 << 服务器:Apache-Coyote/1.1
1 << 内容长度:87
1 << 日期:2017 年 3 月 31 日星期五 14:39:19 GMT
1 << Content-Type: text/plain;charset=UTF-8
1<<
{
“错误”:[
{
"message": "无效的 apikey",
“代码”:4010
}
]
}

我已经通过 Header 对象传递了 apikey,如下所示,并且能够得到响应。

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

谢谢你!

谢谢@cjayswal @thetechnocrat的支持!!

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

BChitrakannan picture BChitrakannan  ·  9评论

Rupak-66 picture Rupak-66  ·  12评论

Nandesh7 picture Nandesh7  ·  8评论

raviguptasmarsh picture raviguptasmarsh  ·  8评论

cjayswal picture cjayswal  ·  17评论