- 相關(guān)推薦
WebService自動(dòng)化-WSDL調用
在做自動(dòng)化測試的過(guò)程中,有時(shí)候需要測試一個(gè)業(yè)務(wù)流程特定的部分, 這個(gè)特定部分可能是接口,它往往需要依賴(lài)前期產(chǎn)生的數據輸出作為輸入,這個(gè)時(shí)候,重新跑一遍前邊流程來(lái)獲得需要的數據顯然不合理, 那么利用后端開(kāi)發(fā)發(fā)布出來(lái)的web service來(lái)直接生成所需數據就顯得尤為便捷, 今天我們就來(lái)看如何利用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 是作為一個(gè)API來(lái)消費提供的web services, 它有兩個(gè)子命名空間:
Service :對象用來(lái)調用被消費的web service提供的方法。
Factory:提供一個(gè)工廠(chǎng)用來(lái)生成一個(gè)定義在WSDL的對象或方法的實(shí)例。
簡(jiǎn)單來(lái)說(shuō)就是service用來(lái)直接調用web service里的方法,factory用來(lái)生成一個(gè)web service對象實(shí)例。
我們用一段代碼來(lái)說(shuō)明:
from suds.client import Client
class WebServices(object):
WSDL_ADDRESS = "http://*/services/*/StudentPrivateLessonService.svc?wsdl"
def __init__(self):
self.web_service = Client(self.WSDL_ADDRESS)
print self.web_service
def is_class_booked(self, class_id, member_id):
return self.web_service.service.IsClassBooked(class_id, member_id)["ClassBooked"]
def cancel_clas(self, class_id, member_id):
parameter = self.web_service.factory.create("CancelClass")
print parameter
print dir(parameter)
parameter.param.Class_id = class_id
parameter.param.Member_id = member_id
parameter.param.CancelBy = 'T'
parameter.param.CancelReason = 'test'
return self.web_service.service.CancelClass(parameter.param)
if __name__ == '__main__':
web_service_class = WebServices()
print web_service_class.is_class_booked('315983', '23540202')
print web_service_class.cancel_clas('315983', '23540202')
以上代碼里:
WSDL_ADRESS:是我們提供的web service的地址。
__init__方法: 實(shí)現了suds client的生成, client的用法如下:
from suds.client import Client
url = 'http://*.?wsdl'
client = Client(url)
is_class_booked 方法:使用了client的service這個(gè)命名空間,即直接調用web service 的可用方法。那么如何知道哪個(gè)方法如何調用呢?
參考代碼里__init__方法的print語(yǔ)句,打印出來(lái)了所有可用的方法和類(lèi)型, print的打印結果片段如下:
Suds ( https://fedorahosted.org/suds/ ) version: 0.4 GA build: R699-20100913
Service ( StudentPrivateLessonService ) tns="http://tempuri.org/"
Prefixes (9)
ns0 = "EFSchools.Englishtown.TeacherTools.Client.ServiceParams"
ns1 = "EFSchools.Englishtown.TeacherTools.Client.ServiceParams.StudentPrivateLesson"
*
ns8 = "http://tempuri.org/"
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個(gè)參數,類(lèi)型為int型。
Cancel_class方法:利用了 client的factory這個(gè)命名空間。
parameter = self.web_service.factory.create("CancelClass")
創(chuàng )建了Cancel Class這個(gè)方法的一個(gè)實(shí)例,然后通過(guò) print parameter,可以看出這個(gè)函數的參數組成:
suds_inpect.png
它是一個(gè)字典,字典的param的值又是一個(gè)字典,故我們要調用這個(gè)方法時(shí)下需要用Parameter.param.Class_id 這樣的方式來(lái)引用。
下圖是整段代碼的運行結果:
證明成功,我們再去DB里查下結果:
可以看出,有一條心的記錄添加出來(lái)。
以上,只要給出WSDL的地址,導入suds,通過(guò)Client, service, factory這3個(gè)類(lèi)就可以實(shí)現web services的自動(dòng)化調用,是不是很簡(jiǎn)單?
【W(wǎng)ebService自動(dòng)化-WSDL調用】相關(guān)文章:
XFire創(chuàng )建WebService實(shí)例09-14
調用javascript08-02
java調用cmd命令01-29
Axis2創(chuàng )建WebService實(shí)例教程10-06
php調用shell的方法技巧07-15
分析JavaScript函數的調用模式07-20
關(guān)于PHP引用的調用方法06-14
Windows網(wǎng)絡(luò )診斷怎么調用10-02
PHP調用的C代碼整理08-14
javascript數組使用調用方法11-05