HTTP REQUESTS
SOAP Web Services
Test SOAP 1.1 and 1.2 web services with native XML support, namespace-aware XPath, prefix-independent paths and XML assertions for enterprise systems.
Benefits of SOAP Testing
- No WSDL required: Test SOAP services without generating client code or stubs
- Native XML support: Write XML directly in feature files with embedded expressions
- Powerful XPath: Assert on response elements using XPath expressions
- Namespace handling: Resolve XPath by namespace, so a test survives a backend that changes its prefixes
- SOAP 1.1 and 1.2:
soap actionpicks the version from the envelope you send - Request flexibility: Inline XML or external files for cleaner test organisation
Basic SOAP Request
Send SOAP requests using the soap action keyword with inline XML envelopes.
Feature: Basic SOAP request
Scenario: Call SOAP service
Given url soapServiceUrl
And request
"""
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetUser xmlns="http://www.example.com/services/v1">
<UserId>12345</UserId>
</GetUser>
</soap:Body>
</soap:Envelope>
"""
When soap action 'GetUser'
Then status 200
For a SOAP 1.1 envelope, or any body that is not a SOAP 1.2 envelope, soap action sends Content-Type: text/xml and a SOAPAction header with the value you give, then performs a POST. For SOAP 1.2, see SOAP 1.2.
SOAP 1.2
soap action reads the namespace of the request body's root element. When it is http://www.w3.org/2003/05/soap-envelope, the request is sent as SOAP 1.2: the action travels as a parameter of the content type, Content-Type: application/soap+xml; charset=UTF-8; action="<action>", and no SOAPAction header is sent, even one set with header or configure headers. The prefix you use does not matter, and an empty action (soap action '') sends no action parameter.
Feature: SOAP 1.2
Scenario: Call a SOAP 1.2 service
Given url soapServiceUrl
And request
"""
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Body>
<GetUser xmlns="http://www.example.com/services/v1">
<UserId>12345</UserId>
</GetUser>
</env:Body>
</env:Envelope>
"""
When soap action 'urn:GetUser'
Then status 200
And match header Content-Type contains 'application/soap+xml'
And match response /Envelope/Body/GetUserResponse/User/Name == 'John Doe'
A Content-Type set with configure headers replaces the one soap action sets, for both versions; a header Content-Type step before soap action is replaced by it.
SOAP with External Files
Organise SOAP requests in separate XML files for better maintainability.
Feature: SOAP with external files
Scenario: Load SOAP request from file
Given url soapServiceUrl
And request read('classpath:soap-requests/get-user.xml')
When soap action 'GetUser'
Then status 200
And match response /Envelope/Body/GetUserResponse/User/Email == 'john.doe@example.com'
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetUser xmlns="http://www.example.com/services/v1">
<UserId>12345</UserId>
</GetUser>
</soap:Body>
</soap:Envelope>
XPath Assertions
Validate SOAP responses using XPath expressions to extract and verify values. By default Karate ignores namespaces: a prefix is part of the element name, and a path written without prefixes, such as /Envelope/Body, matches <soap:Envelope><soap:Body>.
Feature: XPath assertions on SOAP responses
Scenario: Validate SOAP response elements
Given url soapServiceUrl
And request read('get-billing-info.xml')
When soap action 'GetBillingInfo'
Then status 200
# Extract values using XPath
And match response /Envelope/Body/GetBillingInfoResponse/AccountBalance == '150.75'
And match response /Envelope/Body/GetBillingInfoResponse/Currency == 'USD'
And match response /Envelope/Body/GetBillingInfoResponse/Status == 'Active'
# Compare entire response section
And match response /Envelope/Body/GetBillingInfoResponse == read('expected-billing-response.xml')
Dynamic SOAP Requests
Use embedded expressions to create dynamic SOAP requests with variable data.
Feature: Dynamic SOAP requests
Scenario: Create user with dynamic data
* def userId = '67890'
* def userName = 'Alice Johnson'
* def userEmail = 'alice.johnson@example.com'
Given url soapServiceUrl
And request
"""
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<CreateUser xmlns="http://www.example.com/services/v1">
<User>
<Id>#(userId)</Id>
<Name>#(userName)</Name>
<Email>#(userEmail)</Email>
<Active>true</Active>
</User>
</CreateUser>
</soap:Body>
</soap:Envelope>
"""
When soap action 'CreateUser'
Then status 201
And match response /Envelope/Body/CreateUserResponse/Success == 'true'
And match response /Envelope/Body/CreateUserResponse/UserId == '#(userId)'
XML Namespace Handling
Namespace-Aware Configuration
configure xmlNamespaceAware = true makes Karate parse XML with its namespaces: the response body, XML literals, the xml keyword, read() of an .xml file, the karate.* XML helpers and, in a mock, the request. On such a document:
- XPath is resolved by namespace. A prefixed step such as
usr:Usermatches the element whose namespace is the URI thatusrstands for. A step without a prefix matches only an element in no namespace, so/Envelope/Bodyno longer matches a SOAP envelope. - A prefix resolves through the prefixes you configure with
xmlNamespacesfirst, then through the prefixes declared in the document itself. matchcompares elements and attributes by namespace URI and local name. A different prefix, or a namespace declared in a different place, still matches; the same name in a different namespace does not.
Feature: SOAP with namespaces
Scenario: XPath with the document's own prefixes
* configure xmlNamespaceAware = true
Given url soapServiceUrl
And request
"""
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:usr="http://www.example.com/user/v1"
xmlns:addr="http://www.example.com/address/v1">
<soap:Body>
<usr:GetUserDetails>
<usr:UserId>12345</usr:UserId>
<addr:IncludeAddress>true</addr:IncludeAddress>
</usr:GetUserDetails>
</soap:Body>
</soap:Envelope>
"""
When soap action 'GetUserDetails'
Then status 200
# XPath with namespace prefixes
And match response //usr:User/usr:Name == 'John Doe'
And match response //addr:Street == '123 Main St'
The prefixes in this example are the ones the response declares. A path that relies on them breaks when a backend changes its prefixes, which is why xmlNamespaces is the recommended way to write namespace-aware paths.
Prefix-Independent Paths with xmlNamespaces
configure xmlNamespaces declares the prefixes your test uses, each bound to a namespace URI. A path written with them works whatever prefixes the document uses, including an element in a default namespace (xmlns="..."), which a document-declared prefix cannot reach. The bindings apply to every XPath: match and get on a path, set and remove, and karate.xmlPath(). An element that set creates takes the namespace of its prefix. configure xmlNamespaces = null clears the bindings. With xmlNamespaceAware off the key is accepted and has no effect.
Here an old backend and a new one return the same customer with different prefixes, and one path reads both:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<c:getCustomerResponse xmlns:c="urn:customer">
<c:name>Ann</c:name>
</c:getCustomerResponse>
</soap:Body>
</soap:Envelope>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<getCustomerResponse xmlns="urn:customer">
<name>Ann</name>
</getCustomerResponse>
</s:Body>
</s:Envelope>
Feature: Prefix-independent XPath
Background:
* configure xmlNamespaceAware = true
* configure xmlNamespaces = { soap: 'http://schemas.xmlsoap.org/soap/envelope/', c: 'urn:customer' }
* url soapServiceUrl
Scenario Outline: Read the customer from the <backend> backend
Given path '<backend>'
And request <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><c:getCustomer xmlns:c="urn:customer"><c:id>1</c:id></c:getCustomer></soap:Body></soap:Envelope>
When soap action 'getCustomer'
Then status 200
And match /soap:Envelope/soap:Body/c:getCustomerResponse/c:name == 'Ann'
* def name = get response //c:name
* match name == 'Ann'
# match compares by namespace, so the old backend's shape matches both
* match response ==
"""
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<c:getCustomerResponse xmlns:c="urn:customer">
<c:name>#string</c:name>
</c:getCustomerResponse>
</soap:Body>
</soap:Envelope>
"""
# set and remove use the same prefixes
* copy customer = response
* set customer /soap:Envelope/soap:Body/c:getCustomerResponse/c:name = 'Bob'
* match customer //c:name == 'Bob'
* remove customer /soap:Envelope/soap:Body/c:getCustomerResponse/c:name
* match customer count(//c:name) == 0
Examples:
| backend |
| old |
| new |
To apply both settings to every feature, set them in karate-config.js:
function fn() {
karate.configure('xmlNamespaceAware', true);
karate.configure('xmlNamespaces', { soap: 'http://schemas.xmlsoap.org/soap/envelope/', c: 'urn:customer' });
return {};
}
Advanced XPath
Use XPath functions and dynamic expressions for complex SOAP response validation.
Feature: Advanced XPath operations
Scenario: XPath functions and node counting
Given url soapServiceUrl
And request read('get-order-items.xml')
When soap action 'GetOrderItems'
Then status 200
# Count nodes using XPath function
* match response count(/Envelope/Body/GetOrderItemsResponse/Items/Item) == 5
# Match attribute values
* match response //Item[@id='101']/Name == 'Widget A'
* match response //Item[@id='102']/Price == '29.99'
# Extract node-sets as arrays
* match response //Item[@category='electronics']/Name == ['Laptop', 'Mouse', 'Keyboard']
Dynamic XPath Expressions
Build XPath expressions dynamically using variables and the karate.xmlPath() helper.
Scenario: Dynamic XPath evaluation
* def elementName = 'User'
* def fieldName = 'Email'
Given url soapServiceUrl
And request read('get-user.xml')
When soap action 'GetUser'
Then status 200
# Build XPath dynamically
* def xpath = '/Envelope/Body/GetUserResponse/' + elementName + '/' + fieldName
* def email = karate.xmlPath(response, xpath)
* match email == 'john.doe@example.com'
# Extract an entire element and match it as XML
* def userElement = karate.xmlPath(response, '/Envelope/Body/GetUserResponse/' + elementName)
* match userElement == <User><Name>#string</Name><Email>john.doe@example.com</Email></User>
Common SOAP Patterns
SOAP Fault Handling
Validate SOAP fault responses for error scenarios.
Feature: SOAP fault handling
Scenario: Handle SOAP fault response
Given url soapServiceUrl
And request
"""
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetUser xmlns="http://www.example.com/services/v1">
<UserId>invalid-id</UserId>
</GetUser>
</soap:Body>
</soap:Envelope>
"""
When soap action 'GetUser'
Then status 500
# Validate fault structure
And match response /Envelope/Body/Fault/faultcode == 'soap:Server'
And match response /Envelope/Body/Fault/faultstring == 'Invalid user ID'
And match response /Envelope/Body/Fault/detail/ErrorCode == 'USER_NOT_FOUND'
SOAP Authentication Headers
Add WS-Security or custom authentication headers to SOAP requests.
Feature: SOAP with authentication
Scenario: WS-Security username token
* def username = 'admin'
* def password = 'secret123'
Given url soapServiceUrl
And request
"""
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<soap:Header>
<wsse:Security>
<wsse:UsernameToken>
<wsse:Username>#(username)</wsse:Username>
<wsse:Password>#(password)</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body>
<GetSecureData xmlns="http://www.example.com/services/v1">
<DataId>12345</DataId>
</GetSecureData>
</soap:Body>
</soap:Envelope>
"""
When soap action 'GetSecureData'
Then status 200
Data-Driven SOAP Testing
Parameterise SOAP requests for testing multiple scenarios.
Feature: Data-driven SOAP testing
Scenario Outline: Test multiple users
* def userId = '<userId>'
* def expectedName = '<name>'
Given url soapServiceUrl
And request
"""
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetUser xmlns="http://www.example.com/services/v1">
<UserId>#(userId)</UserId>
</GetUser>
</soap:Body>
</soap:Envelope>
"""
When soap action 'GetUser'
Then status 200
And match response /Envelope/Body/GetUserResponse/User/Name == expectedName
Examples:
| userId | name |
| 101 | John Doe |
| 102 | Jane Smith |
| 103 | Bob Johnson |
Configuration Reference
SOAP-Specific Configuration
| Configuration | Type | Default | Description |
|---|---|---|---|
xmlNamespaceAware | boolean | false | Parse XML with its namespaces: XPath resolves prefixes to namespace URIs and match compares namespace URI plus local name. See Namespace-Aware Configuration |
xmlNamespaces | JSON | - | Prefixes your XPath uses, as { prefix: 'namespace-uri' }, resolved before the document's own prefixes when xmlNamespaceAware is on. null clears them |
connectTimeout | integer | 30000 | Connection timeout in milliseconds |
readTimeout | integer | 30000 | Read timeout in milliseconds |
JSON request/response bodies are pretty-printed by default; SOAP/XML bodies pass through unchanged. To redact sensitive header or body content from logs, see Logging.
SOAP Action Keyword
When soap action '<SOAPAction>'
The soap action keyword:
- Sends a SOAP 1.2 request when the body's root element is in the
http://www.w3.org/2003/05/soap-envelopenamespace:Content-Type: application/soap+xmlwith anactionparameter, and noSOAPActionheader - Otherwise sends a SOAP 1.1 request:
Content-Type: text/xmland theSOAPActionheader - Executes the HTTP POST request, replacing the
method poststep
Example Configuration
Feature: SOAP configuration
Background:
* url soapServiceUrl
* configure xmlNamespaceAware = true
* configure xmlNamespaces = { soap: 'http://schemas.xmlsoap.org/soap/envelope/' }
* configure connectTimeout = 10000
* configure readTimeout = 10000
Scenario: Configured SOAP request
Given request read('soap-request.xml')
When soap action 'GetData'
Then status 200
And match response /soap:Envelope/soap:Body/GetDataResponse/Status == 'OK'
Troubleshooting SOAP Requests
Common Issues
Problem: A prefixed XPath such as //usr:Name matches nothing
Solutions:
- Prefixes in XPath resolve only with
configure xmlNamespaceAware = true; without it, write the path without prefixes (/Envelope/Body/...) - With it on, declare your prefixes with
configure xmlNamespacesso the path does not depend on the prefixes the service uses - An element in a default namespace (
xmlns="...") needs a prefix fromxmlNamespaces, or*[local-name()='Name']
Problem: A path without prefixes stopped matching after enabling xmlNamespaceAware
Solutions:
- With namespaces on,
/Envelope/Bodymatches only elements in no namespace; use prefixes bound withxmlNamespaces
Problem: SOAPAction header mismatch
Solutions:
- Verify SOAP action name matches WSDL operation
- A SOAP 1.2 service expects a SOAP 1.2 envelope (
http://www.w3.org/2003/05/soap-envelope); Karate then sends the action in theContent-Typeinstead of aSOAPActionheader - Review service documentation for correct action format
Problem: XML parsing errors
Solutions:
- Validate XML structure with online validator
- Check for unescaped special characters
- With
xmlNamespaceAwareon, every prefix must be declared in the document - Run with
--log-console trace(or* configure logging = { console: 'trace' }) to print the full request body
Next Steps
Continue testing enterprise services:
- Handle XML responses: Response Handling
- Assert with XPath: Response Validation
- Compare XML documents: XML Matching
- Parameterise requests: Dynamic Scenarios
- Organise SOAP files: Project Structure
- Configure timeouts: Configuration