API testing
Start here when you want one REST call, one assertion, and request/response evidence in the same report as your browser and mobile tests.
Prerequisites
- A SHAFT Maven project. Install SHAFT walks through generating one.
- Network access from the test machine to the API under test (the sample calls
restcountries.com), including any proxy settings your network requires. - Credentials for protected APIs, kept outside source control. See API Authentication.
import com.shaft.driver.SHAFT;
import org.testng.annotations.Test;
public class CountryApiTest {
@Test
public void getCountryByCapital() {
SHAFT.API api = new SHAFT.API("https://restcountries.com/v3.1/");
api.get("capital/Cairo");
api.assertThatResponse()
.extractedJsonValue("$[0].name.common")
.isEqualTo("Egypt");
}
}
Continue with request building, authentication, and response assertions. When the API is part of a browser journey, use UI and API contract replay to record the exchanged traffic once and assert or replay it later.
Run and inspect evidence
Run the test from the project root:
mvn test
SHAFT attaches request, response, status, headers, body, and assertion evidence
under allure-results and the generated Allure report under the project target output. If the request fails before assertions, check the
base URI, proxy settings, authentication, and whether automatic 2xx status
assertion is appropriate for the scenario.
Verify
- Maven reports the test as run and passed.
- The Allure report for the test shows the request, the response status,
headers, and body, and the
extractedJsonValueassertion. - To prove the assertion is live, change
"Egypt"to another value and rerun. The test fails, and the report shows the expected and actual values.
First useful next steps
| Need | Start with |
|---|---|
| Add headers, query parameters, or body | Request Builder |
| Reuse tokens or basic auth | API Authentication |
| Validate JSON fields or schemas | Response Validations |
| Record and replay UI/API traffic | UI and API contract replay |
| Track OpenAPI coverage | API configuration |