Skip to main content

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 action picks 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.

Gherkin
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
SOAPAction Header

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.

Gherkin
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.

Gherkin
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'
soap-requests/get-user.xml
<?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>.

Gherkin
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.

Gherkin
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:User matches the element whose namespace is the URI that usr stands for. A step without a prefix matches only an element in no namespace, so /Envelope/Body no longer matches a SOAP envelope.
  • A prefix resolves through the prefixes you configure with xmlNamespaces first, then through the prefixes declared in the document itself.
  • match compares 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.
Gherkin
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:

Old backend
<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>
New backend
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<getCustomerResponse xmlns="urn:customer">
<name>Ann</name>
</getCustomerResponse>
</s:Body>
</s:Envelope>
Gherkin
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:

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.

Gherkin
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.

Gherkin
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.

Gherkin
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.

Gherkin
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.

Gherkin
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​

ConfigurationTypeDefaultDescription
xmlNamespaceAwarebooleanfalseParse XML with its namespaces: XPath resolves prefixes to namespace URIs and match compares namespace URI plus local name. See Namespace-Aware Configuration
xmlNamespacesJSON-Prefixes your XPath uses, as { prefix: 'namespace-uri' }, resolved before the document's own prefixes when xmlNamespaceAware is on. null clears them
connectTimeoutinteger30000Connection timeout in milliseconds
readTimeoutinteger30000Read 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-envelope namespace: Content-Type: application/soap+xml with an action parameter, and no SOAPAction header
  • Otherwise sends a SOAP 1.1 request: Content-Type: text/xml and the SOAPAction header
  • Executes the HTTP POST request, replacing the method post step

Example Configuration​

Gherkin
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 xmlNamespaces so the path does not depend on the prefixes the service uses
  • An element in a default namespace (xmlns="...") needs a prefix from xmlNamespaces, or *[local-name()='Name']

Problem: A path without prefixes stopped matching after enabling xmlNamespaceAware

Solutions:

  • With namespaces on, /Envelope/Body matches only elements in no namespace; use prefixes bound with xmlNamespaces

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 the Content-Type instead of a SOAPAction header
  • Review service documentation for correct action format

Problem: XML parsing errors

Solutions:

  • Validate XML structure with online validator
  • Check for unescaped special characters
  • With xmlNamespaceAware on, 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: