背景:
在进行web自动化测试过程中(eg. Selenium),测试代码和Application代码是分开的。也就是说,自动化测试的代码中是不能调用Application的代码。只是在test case运行时,利用application的web容器。
这样就会遇到一个问题,Application中的某些功能,需要后台service执行后才能获得数据,这些service一般是定时执行的。在自动化测试中,比如click页面上的某些按钮,要想在页面上看到数据,需要等待service执行以后,才能看到数据。有些service的执行周期可能是24小时。显然不能让test case等待24小时后再检查数据。
解决方案:
针对上述问题,很容易想到的解决办法,就是在test case执行过程中,显式地调用application的后台service。前面提到过,test代码是不能调用application的代码。那该怎么办呢?
一个通用的解决办法是,把application的service功能封装成标准的接口,比如 web service,REST等,对外开放。然后在test中调用这些标准的web service或REST接口。这样就解决了依赖的问题。
具体实现:
这里以REST为例,介绍把application后台service封装成RESTFul接口的方法。在实际的开发过程中,会有很多后台service。随着系统的升级,可能还会增加新的service。如果每增加一个service,都开放一个RESTful接口,就会是系统变得越来越笨重。而且有些开发是plugin的形式,也不允许新增Restful接口。
这就需要在系统设计的时候,考虑到这一点。怎么办呢?在Application core中定义一个RESTful接口对外开放。在这个开放的接口中进行内部处理。方法是,定义一个可扩展的REST Service接口,通过JDK的java.util.ServiceLoader来加载实现该REST Service接口的具体类。根据URL中指定的serviceName来触发相应的REST Service。
以Apache Wink Framework为例,介绍部署RESTful API的方法
--------------------------Application core中的RESTful接口------------------------------------------------------
package com.mycompany.rest;
@Path("/automation/AutomationServiceTest")
public class TestingRestfulResource {
private static Map<String, AutomationServiceTest> serviceTestMap =
new HashMap<String, AutomationServiceTest>();
static {
ServiceLoader<AutomationServiceTest> driverProvider =
ServiceLoader.load(AutomationServiceTest.class);
for (AutomationServiceTestserviceTest : driverProvider) {
serviceTestMap.put(serviceTest.getServiceName(), serviceTest);
}
}
@GET
@Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Object runService(@Context final HttpServletRequest request)
throws WSException
{
// The service name like "AgileSyncService"
String serviceName = request.getParameter("serviceName");
AutomationServiceTest serviceTest = serviceTestMap.get(serviceName);
if (null == serviceTest) {
log.error("Service " + serviceName + "not found");
throw new WSException(Response.Status.INTERNAL_SERVER_ERROR, request,
WSErrorHelper
.createWSError("INT_SERVICE_NOT_FOUND", new String[] {serviceName}));
}
return serviceTest.runService(request);
}
}
在wink_ws.app文件中增加一行
com.mycompany.rest.TestingRestfulResource
-------------------------AutomationServiceTest 接口-------------------------------------
package com.mycompany.test.plugin.rest.service;
import javax.servlet.ServletRequest;
public interface AutomationServiceTest {
public Object runService(ServletRequest request);
public String getServiceName();
}
----------------------RESTful service实现类--------------------------------------------
package com.mycompany.test.plugin.rest.service.impl
public class MyServiceTestImpl implements AutomationServiceTest {
@Override
public Object runService(ServletRequest request) {
MyService service = new MyService ();
service.runService("jobID", 2L, 2L);
return "<result>run my service successfully</result>";
}
@Override
public String getServiceName() {
return "MyService";
}
}
在/META-INF/services/com.mycompany.test.plugin.rest.service.AutomationServiceTest 文件中增加一行
com.mycompany.test.plugin.rest.service.impl.MyServiceTestImpl
-------------------------------------------------------------------------------------------------------------------
这样,通过URL:
http://localhost:8080/<my app>/rest/automation/AutomationServiceTest?serviceName=MyService
就可以在Automation Testing的代码中调用RESTful接口,进而触发后台servie。
比如,利用Cactus framework,可以按照如下方式编写testing code。
package com.mycompany.webdriver.page.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.junit.Before;
import org.junit.Test;
public class TestMyServic {
private static final String SERVICE_URL = "http://localhost:8080/app/rest/automation/AutomationServiceTest?serviceName=MyService";
private DefaultHttpClient client;
private HttpGet get;
@Before
public void setup() {
//For REST API Call
client = new DefaultHttpClient();
//set time out
HttpParams params = new BasicHttpParams();
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
client.setParams(params);
get = new HttpGet();
}
@Test
public void testCallService() {
System.out.println("begin............");
get.setURI(URI.create(SERVICE_URL));
HttpResponse response;
try {
response = client.execute(get);
String responseText = getHttpResponseBodyContent(response);
System.out.println("******response: " + responseText);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end............");
}
private static String getHttpResponseBodyContent(HttpResponse response) throws IllegalStateException, IOException{
String result = null;
BufferedReader bufferedReader = null;
try{
bufferedReader = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
StringBuilder sb = new StringBuilder();
String outputLine;
while ((outputLine = bufferedReader.readLine()) != null) {
sb.append(outputLine);
}
result = sb.toString();
}finally{
bufferedReader.close();
}
return result;
}
}
分享到:
相关推荐
API-Automation-Testing RESTFul Webservice 接口测试教程和演示代码 Rest-Assured 是一个测试 Restful Web Service 的 Java 类库,我们能够测试各种各样的请求组合,依次测试核心业务逻辑的不同组合。Rest-Assured ...
在本教程中,您将学习如何使用RESTAssured Library在Java中自动化REST API。 本教程使用的技术堆栈为: JAVA作为用于编写测试代码的编程语言 以TestNg为框架 Gradle作为构建工具 IntelliJ是编写Java代码的首选IDE...
RestService和身份验证自动化测试对于REST测试,我使用了提供的服务,对于登录测试,则使用了 directoy文档包含用于错误跟踪的示例(如果您不使用任何错误跟踪器)和基本测试策略计划的示例。 测试策略还包含运行...
实用的安全自动化和测试 这是Packt发布的《 的代码库。 DevSecOps中用于自动安全性扫描和...执行Rest API的安全性测试使用用于CI / CD集成的开源工具和脚本模板实现Web应用程序的安全性 如果您觉得这本书适合您,请立
REST保证的示例 概述 该项目提供了一个示例,说明如何使用库为...REST保证的示例测试用例是使用JUnit(4.x),REST保证(3.x)和AssertJ在Java(8.x)中实现的。 这些测试可以在项目的src / main / test / java文件夹
Cerberus Testing是一个低代码测试自动化平台,支持测试Web,iOS,Android和API(REST,SOAP和Kafka)应用程序。 该平台允许通过易于使用的Web界面创建自动化测试用例,而无需任何开发技能-如今,开发,质量和业务...
使用AWS Cloud Provider的Terraform Automation演示 该存储库的目的是显示AWS Cloud的案例Terraform自动化。 该演示将提供与对应的terraform配置关联的以下云资源: AWS资源 地形资源 链接到TerraHub配置 API网关...
坦率目录什么是弗兰克Frank是REST API自动化测试工具,例如Postman,但在命令行中。安装Linux $ sudo curl -L git.io/frank -o /usr/local/bin/frank$ sudo chmod +x /usr/local/bin/frank苹果系统$ curl -L git.io/...
放心例子上的Rest-Assured课程的配套资料库
由于现有API和新添加的API不断变化,因此REST API渗透测试非常复杂。 安全工程师或开发人员可以将Astra用作其过程的组成部分,因此他们可以在开发周期的早期检测并修补漏洞。 Astra可以自动检测并测试登录和注销...
:rocket: 灵活的REST测试 :link: 连接多个请求:示例将您作为登录请求的响应获得的授权令牌自动嵌入到以下请求中 :memo: YAML语法:在YAML文件中编写所有测试 :party_popper: 易于理解:您将在几秒钟内理解该概念,...
正义API Just-API是用于REST , GraphQL API的基于声明式,基于规范的测试框架。 用户可以在不编写代码的情况下测试API,但也可以在需要时使用代码。 它从YAML文件中读取API测试规范,并以串行/并行模式运行它们。 ...
v1.8.0在v1.8.0中v1.8.0 ,现在支持使用Puppeteer进行基于Web的自动化。 您现在可以创建针对浏览器应用程序的基于Gherkin的测试。 我们不想错过的最简单的项目之一是打破前端应用程序与我们的服务应用程序之间的...
保留了REST Assured框架的所有功能的扩展功能-用于发送请求和响应分析的简单界面-自动为REST和SOAP生成服务对象-支持BDD方法-以指定的尝试次数重试失败的测试-以JSON格式详细记录请求和响应,可在您的报告中使用 ...
虚构 virtualan插件: 虚拟化: 现场演示链接: 仅OpenAPI: ...OpenAPI和SOAP: ...什么是Virtualan: ... 它如何发挥作用:在敏捷世界中,我们需要开发(微)服务并并行测试服务。 在开发真正的微服务
You will then take a deep dive into the life cycle of building, testing, and deploying applications with maximum automation to reduce the deployment cycle time. Gradually, you will move on to ...
尽可能在不同类别的场景中创建共同的体验 保持框架尽可能轻便,这可能意味着自动发现并动态加载可选库 显而易见,应该误导混淆或引起额外的认知负担 做你说的,说你做的。 无副作用。 尝试以动词开始方法,除非它真...
Frameworkium核心 用Java编写可维护的Selenium和REST API测试的框架。 发行说明 请参阅。 用法 首先,您可以访问。 有关“快速入门”模板和示例的信息,请参见。
它受到项目的思想和应用程序设计的强烈启发,并复制了服务提供的功能。 目录 介绍 可在我们的演示服务器上使用服务: : 但是,我们建议您设置自己的服务器,并控制通过Request Baskets服务传递和收集的信息。 ...
Meqa使用YAML中的OpenAPI(以前称为Swagger)规范生成并运行测试套件。 通过生成有用的测试模式,REST API的测试变得很容易-无需编码。 演示版 强调 了解对象关系并生成使用正确对象和值的测试。 使用OpenAPI规范中...