WebService自动化-WSDL调用
在做自动化测试的过程中,有时候需要测试一个业务流程特定的部分, 这个特定部分可能是接口,它往往需要依赖前期产生的数据输出作为输入,这个时候,重新跑一遍前边流程来获得需要的数据显然不合理, 那么利用后端开发发布出来的Web service来直接生成所需数据就显得尤为便捷, 今天我们就来看如何利用suds调用web service。
Suds is a lightweight SOAP python client for consuming Web Services.
The suds Client class provides a consolidated API for consuming web services. The object contains (2) sub-namespaces:
service
The service namespace provides a proxy for the consumed service. This object is used to invoke operations (methods) provided by the service endpoint.
factory
The factory namespace provides a factory that may be used to create instances of objects and types defined in the WSDL.
suds Client 是作为一个API来消费提供的web services, 它有两个子命名空间:
Service :对象用来调用被消费的web service提供的方法。
Factory:提供一个工厂用来生成一个定义在WSDL的对象或方法的实例。
简单来说就是service用来直接调用web service里的方法,factory用来生成一个web service对象实例。
我们用一段代码来说明:
from nt import Client
class WebServices(object):
WSDL_ADDRESS = "http://*/services/*/
def __init__(self):
_service = Client(_ADDRESS)
print _service
def is_class_booked(self, class_id, member_id):
return _assBooked(class_id, member_id)["ClassBooked"]
def cancel_clas(self, class_id, member_id):
parameter = _te("CancelClass")
print parameter
print dir(parameter)
s_id = class_id
er_id = member_id
elBy = 'T'
elReason = 'test'
return _elClass(m)
if __name__ == '__main__':
web_service_class = WebServices()
print web_service__class_booked('315983', '23540202')
print web_service_el_clas('315983', '23540202')
以上代码里:
WSDL_ADRESS:是我们提供的web service的地址。
__init__方法: 实现了suds client的生成, client的用法如下:
from nt import Client
url = 'http://*.?wsdl'
client = Client(url)
is_class_booked 方法:使用了client的service这个命名空间,即直接调用web service 的可用方法。那么如何知道哪个方法如何调用呢?
参考代码里__init__方法的print语句,打印出来了所有可用的方法和类型, print的打印结果片段如下:
Suds ( ) version: 0.4 GA build: R699-20100913
Service ( StudentPrivateLessonService ) tns=""
Prefixes (9)
ns0 = "iceParams"
ns1 = "entPrivateLesson"
*
ns8 = ""
Ports (1):
(BasicHttpBinding_IStudentPrivateLessonService)
Methods (18):
*
CancelClass(ns1:CancelParameter param, )
*
IsClassBooked(xs:int class_id, xs:int member_id, )
*
Types (47):
ns4:ArrayOfBatchCancelDetail
ns4:ArrayOfBookablePLClass
ns4:ArrayOfBookedPLClass
*
从打印结果可以看出,IsClassBooked方法可以直接调用,它需要2个参数,类型为int型。
Cancel_class方法:利用了 client的factory这个命名空间。
parameter = _te("CancelClass")
创建了Cancel Class这个方法的一个实例,然后通过 print parameter,可以看出这个函数的参数组成:
suds_inpect.png
它是一个字典,字典的'param的值又是一个字典,故我们要调用这个方法时下需要用s_id 这样的方式来引用。
下图是整段代码的运行结果:
证明成功,我们再去DB里查下结果:
可以看出,有一条心的记录添加出来。
以上,只要给出WSDL的地址,导入suds,通过Client, service, factory这3个类就可以实现web services的自动化调用,是不是很简单?
-
结构抗震概念设计的基本原则
建筑结构抗震概念设计的目标是使整体结构能发挥耗散地震能量的作用,避免结构出现敏感的薄弱部位,地震能量的耗散如果仅集中在极少数的薄弱部位,将会导致结构过早破坏。一、结构的简单性结构简单是指结构在抗震作用下具有直接和明确的传力途径,结构的计算模型、内力...
-
淘宝直通车要怎样才能开好
用直通车推广不需要太大量,要有针对性的选择产品推广即可,自己店铺里的宝贝哪些是比较好卖的,哪些是量多的,哪些最容易让顾客看一眼就淘腰包的……然后就选择哪几款。一定要记住,选品推广在精不在多。针对性的做几个有效果的推广,总比泛滥的盲目去推也没...
-
word安全模式修复方法
word安全模式修复今天启动Word时,弹出如下提示:“MicrosoftOfficeWord遇到问题需要关闭。我们对此引起的不便表示抱歉遇到问题需要关闭。您正在处理的信息有可能丢失。MicrosoftOfficeWord可以尝试为您恢复。恢复我的工作并重启MicrosoftOfficeWord。请将此问题...
-
移动硬盘突然识别不了
导语:移动硬盘顾名思义是以硬盘为存储介质,计算机之间交换大容量数据,强调便携性的存储产品,系统不能识别移动硬盘怎么办呢?在正常情况下,移动硬盘同主机相连后系统即可将它认成“移动存储设备”,并自动在“我的电脑”中添加盘符。如果系统将移动硬盘认成“未知的USB...