-
introduction
-
What is Web Services
-
Web Services composition and calling principle
-
The difference between Web Services interface and API (application program interface)
-
Web Services interface combat
-
1.suds-SOAP client
-
2.Zeep-SOAP client
-
3. Comparison of Zeep and suds
-
-
to sum up
introduction
I have shared many articles about API and UI automation testing based on HTTP requests.
In fact, there are many forms of interfaces. In addition to our common HTTP RESTFUL interface, there are also Web Services type interfaces and RPC interfaces. Different types of interface test methods are different.
The topic shared today is: How to test the interface of the Web Services type.
What is Web Services
Web Service is a cross-programming language and cross-operating system platform remote call technology.
In layman's terms, a Web Service is an application that provides services to the outside world by exposing an API that can be called through the Web. WebService can cross programming languages and operating systems, that is, your client program and the server program that provides the service can use different programming languages and use different operating systems.
For example: through WebServices, a client program written in C++ running on the windows platform can communicate with a server program written in Java running on the Linux platform.
Web Services composition and calling principle
The composition of the Web Service platform relies on the following technologies:
-
UDDI: means Universal Description, Discovery, and Integration. It is a directory service through which enterprises can register and search for Web services. It is a cross-platform description specification based on XML.
-
SOAP: is a simple XML-based protocol that enables applications to exchange information via HTTP.
-
WSDL: It is a language based on XML and used to describe Web Services and how to access Web Services.
The calling principle of Web Services is as follows:
Step 1: The client wants to call a service, but does not know where to call it, so it initiates an inquiry to the UDDI registry.
Step 2: UDDI registration center, after some searching, I found that there is a guy named Web Service A who can provide the service that the client wants.
Step 3: The client sends a message to Web Service A, asking how to call the service it provides.
Step 4: Web Service A receives the request and sends a WSDL file to the client. The various method interfaces that Web Service A can provide are recorded here.
Step 5: The client generates a SOAP request through WSDL (the xml format interface method provided by the Web Service is encapsulated into an HTTP request using the SOAP protocol), and sends it to Web Service A to call the service it wants.
Step 6: Web Service A executes the corresponding service according to the SOAP request and returns the result to the client.
The difference between Web Services interface and API (application program interface)
What are the differences between the Web Services interface and our commonly used API (application programming interface)? The following table shows the difference:
In our daily work, whether the interface is provided to us for testing in the form of Web Service, API, or RESTFUL API often depends on the actual situation of the business.
Web Services interface combat
Through the previous explanation, we understand that WSDL is the interface service description generated by Web Services to be called by the client. Through WSDL, the client can construct the correct request and send it to the server.
This is also true in actual work. For an interface in the form of Web Services, development is often a link in WSDL format. For example, the following link is a public Web Servbice service interface:
# IP地址服务
http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl
1.suds-SOAP client
In Python, the client can use the suds library to call Web Service. The suds Client class provides a unified API object for using Web Service. This object includes the following two namespaces.
-
service: The service object is used to call the methods provided by the consumed web service.
-
factory: Provides a factory that can be used to create instances of objects and types defined in WSDL.
Use of suds
-
Suds installation
After Python officially stopped supporting Python 2.X version and fully switched to Python 3.X, the development of the original suds project has stalled, but this does not mean that suds no longer supports Python 3.X. suds-community forks the original suds library and develops a version that supports Python 3.X. The installation commands are as follows:
pip install suds-community
-
Simple to use
from suds.client import Client
if __name__ == "__main__":
url = 'http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl'
# 初始化
client = Client(url)
# 打印出所有可用的方法
print(client)
Run the above code directly, you will find the execution result is as follows:
# 运行结果片段
Suds ( https://fedorahosted.org/suds/ ) version: 0.8.4
Service ( IpAddressSearchWebService ) tns="http://WebXml.com.cn/"
Prefixes (1)
ns0 = "http://WebXml.com.cn/"
Ports (2):
(IpAddressSearchWebServiceSoap)
Methods (3):
getCountryCityByIp(xs:string theIpAddress)
getGeoIPContext()
getVersionTime()
Types (1):
ArrayOfString
(IpAddressSearchWebServiceSoap12)
Methods (3):
getCountryCityByIp(xs:string theIpAddress)
getGeoIPContext()
getVersionTime()
Types (1):
ArrayOfString
In this code, all the methods supported by IpAddressSearchWebService are printed out. As you can see, it has three methods (Methods(3) shows the methods and parameters provided by this Web Service).
-
Actual case
Now that I see that the Web Service of IpAddressSearchWebService supports 3 methods, let's apply these methods:
from suds.client import Client
if __name__ == "__main__":
url = 'http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl'
# 初始化
client = Client(url)
# 打印出所有支持的方法
print(client)
# 调用支持的方法, 使用client.service
print(client.service.getVersionTime())
print(client.service.getCountryCityByIp('192.168.0.1'))
Execute the above code, you will find the following output:
# 输出结果片段
# 此为getVersionTime这个方法的输出
IP地址数据库,及时更新
# 此为getCountryCityByIp方法的输出
(ArrayOfString){
string[] =
"192.168.0.1",
"局域网 对方和您在同一内部网",
}
Note that in the code, I used the client.service method, because the service object is used to call the methods provided by the consumed web service.
In actual work, the WSDL interface you encounter will be much more complicated than this. Therefore, under normal circumstances, we will encapsulate the WSDL interface into classes for use, and then write the corresponding test cases for each class method, as shown below:
import pytest
from suds.client import Client
@pytest.mark.rmb
class WebServices(object):
WSDL_ADDRESS = 'http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl'
def __init__(self):
self.web_service = Client(self.WSDL_ADDRESS)
def get_version_time(self):
return self.web_service.service.getVersionTime()
def get_country_city_by_ip(self, ip):
return self.web_service.service.getCountryCityByIp(ip)
class TestWebServices:
def test_version_time(self):
assert WebServices().get_version_time() == "IP地址数据库,及时更新"
@pytest.mark.parametrize('ip, expected', [('10.10.10.10', '10.10.10.10')])
def test_get_country_city_by_ip(self, ip, expected):
assert expected in str(WebServices().get_country_city_by_ip(ip))
if __name__ == "__main__":
pytest.main(["-m", "rmb", "-s", "-v"])
2.Zeep-SOAP client
Zeep is a modern SOAP client in Python. Zeep uses the services and types in the WSDL document by checking the WSDL document and generating the corresponding code. This method provides an easy-to-use programming interface for the SOAP server.
The following is a specific explanation of the use of Zeep:
-
Zeep installation The
installation command is as follows:python pip install zeep
-
Zeep queries available methods in WSDL
Compared with suds, if you want to see which methods are available in a WSDL description, Zeep does not need to perform initialization actions. Simply enter the following command in the command line:
python -mzeep http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl
After execution, you will find the output as follows:
Prefixes:
xsd: http://www.w3.org/2001/XMLSchema
ns0: http://WebXml.com.cn/
Global elements:
ns0:ArrayOfString(ns0:ArrayOfString)
ns0:getCountryCityByIp(theIpAddress: xsd:string)
ns0:getCountryCityByIpResponse(getCountryCityByIpResult: ns0:ArrayOfString)
ns0:getGeoIPContext()
ns0:getGeoIPContextResponse(getGeoIPContextResult: ns0:ArrayOfString)
ns0:getVersionTime()
ns0:getVersionTimeResponse(getVersionTimeResult: xsd:string)
ns0:string(xsd:string)
Global types:
xsd:anyType
ns0:ArrayOfString(string: xsd:string[])
xsd:ENTITIES
xsd:ENTITY
xsd:ID
xsd:IDREF
xsd:IDREFS
xsd:NCName
xsd:NMTOKEN
xsd:NMTOKENS
xsd:NOTATION
xsd:Name
xsd:QName
xsd:anySimpleType
xsd:anyURI
xsd:base64Binary
xsd:boolean
xsd:byte
xsd:date
xsd:dateTime
xsd:decimal
xsd:double
xsd:duration
xsd:float
xsd:gDay
xsd:gMonth
xsd:gMonthDay
xsd:gYear
xsd:gYearMonth
xsd:hexBinary
xsd:int
xsd:integer
xsd:language
xsd:long
xsd:negativeInteger
xsd:nonNegativeInteger
xsd:nonPositiveInteger
xsd:normalizedString
xsd:positiveInteger
xsd:short
xsd:string
xsd:time
xsd:token
xsd:unsignedByte
xsd:unsignedInt
xsd:unsignedLong
xsd:unsignedShort
Bindings:
HttpGetBinding: {http://WebXml.com.cn/}IpAddressSearchWebServiceHttpGet
HttpPostBinding: {http://WebXml.com.cn/}IpAddressSearchWebServiceHttpPost
Soap11Binding: {http://WebXml.com.cn/}IpAddressSearchWebServiceSoap
Soap12Binding: {http://WebXml.com.cn/}IpAddressSearchWebServiceSoap12
Service: IpAddressSearchWebService
Port: IpAddressSearchWebServiceSoap (Soap11Binding: {http://WebXml.com.cn/}IpAddressSearchWebServiceSoap)
Operations:
getCountryCityByIp(theIpAddress: xsd:string) -> getCountryCityByIpResult: ns0:ArrayOfString
getGeoIPContext() -> getGeoIPContextResult: ns0:ArrayOfString
getVersionTime() -> getVersionTimeResult: xsd:string
Port: IpAddressSearchWebServiceSoap12 (Soap12Binding: {http://WebXml.com.cn/}IpAddressSearchWebServiceSoap12)
Operations:
getCountryCityByIp(theIpAddress: xsd:string) -> getCountryCityByIpResult: ns0:ArrayOfString
getGeoIPContext() -> getGeoIPContextResult: ns0:ArrayOfString
getVersionTime() -> getVersionTimeResult: xsd:string
Port: IpAddressSearchWebServiceHttpGet (HttpGetBinding: {http://WebXml.com.cn/}IpAddressSearchWebServiceHttpGet)
Operations:
getCountryCityByIp(theIpAddress: xsd:string) -> xsd:string
getGeoIPContext() -> xsd:string
getVersionTime() -> xsd:string
Port: IpAddressSearchWebServiceHttpPost (HttpPostBinding: {http://WebXml.com.cn/}IpAddressSearchWebServiceHttpPost)
Operations:
getCountryCityByIp(theIpAddress: xsd:string) -> xsd:string
getGeoIPContext() -> xsd:string
getVersionTime() -> xsd:string
As can be seen from the results, IpAddressSearchWebService provides 3 methods, namely getCountryCityByIp, getGeoIPContext and getVersionTime.
-
Simple use
After knowing which methods are available, we can directly call the available methods:
import zeep
if __name__ == "__main__":
wsdl = 'http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl'
client = zeep.Client(wsdl=wsdl)
print(client.service.getCountryCityByIp('10.10.10.10'))
-
actual case
Now we change the code of the test IpAddressSearchWebService implemented with suds above to use the Zeep test:
import pytest
import zeep
@pytest.mark.rmb
class WebServices(object):
WSDL_ADDRESS = 'http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl'
def __init__(self):
self.web_service = zeep.Client(wsdl=self.WSDL_ADDRESS)
def get_version_time(self):
return self.web_service.service.getVersionTime()
def get_country_city_by_ip(self, ip):
return self.web_service.service.getCountryCityByIp(ip)
class TestWebServices:
def test_version_time(self):
assert WebServices().get_version_time() == "IP地址数据库,及时更新"
@pytest.mark.parametrize('ip, expected', [('10.10.10.10', '10.10.10.10')])
def test_get_country_city_by_ip(self, ip, expected):
assert expected in str(WebServices().get_country_city_by_ip(ip))
if __name__ == "__main__":
pytest.main(["-m", "rmb", "-s", "-v"])
As you can see, using Zeep to call the Web Service service is also very simple.
3. Comparison of Zeep and suds
suds is an old SOAP client, and Zeep is currently a very popular SOAP client. So how should we choose? Here are a few differences between the two, for reference only:
In summary, Zeep supports the latest version of Python better and has no performance issues. If your project is newly established, you might as well use Zeep directly when choosing the Web Service client.
to sum up
Today's sharing focuses on how to test the Web Service and Web Service interface, especially the interface provided in WSDL format.
It also introduces two Python libraries suds and Zeep for testing Web Services, and how we use suds or Zeep to encapsulate the Web Services interface in our daily work.
Welcome to pay attention to [The Way of Infinite Testing] public account , reply [Receive Resources]
Python programming learning resources dry goods,
Python+Appium framework APP UI automation,
Python+Selenium framework Web UI automation,
Python+Unittest framework API automation,
Resources and codes are sent for free~
There is a QR code of the official account at the bottom of the article, you can just scan it on WeChat and follow it.
Remarks: My personal public account has been officially opened, dedicated to the sharing of test technology, including: big data testing, functional testing, test development, API interface automation, test operation and maintenance, UI automation testing, etc., WeChat search public account: "Infinite The Way of Testing", or scan the QR code below:
Add attention and let us grow together!