We are currently using a suite of selenium tests to navigate through our app and at important points we are using the following code to run the spider and an active scan
public void testWebdriver(String currentUrl) throws Exception {
log.debug("Setting zaproxy {}:{}", ZAP_PROXY_ADDR, zapPort);
log.debug("Connecting to {}{}", appToTest, IntegrationTestConstants.link);
ApiResponse spiderResponse = clientApi.spider.scan(currentUrl, null, null, null, null);
spiderScanId = ((ApiResponseElement) spiderResponse).getValue();
while (true) {
int progress = Integer.parseInt(
((ApiResponseElement) clientApi.spider.status(spiderScanId)).getValue());
int spiderPercentageGate = 100;
if (progress >= spiderPercentageGate) break;
Thread.sleep(2000);
}
clientApi.spider.results(spiderScanId);
clientApi.pscan.setEnabled("true");
ApiResponse activeScanResult = clientApi.ascan.scan(currentUrl, "true", "false", null, null, null);
//UnComment the following loop if more than 2 tests begin to fail.
String activeScanId = ((ApiResponseElement) activeScanResult).getValue();
while (true) {
int progress = Integer.parseInt(
((ApiResponseElement) clientApi.ascan.status(activeScanId)).getValue());
int activeScanPercentageGate = 100;
if (progress >= activeScanPercentageGate) break;
Thread.sleep(2000);
}
}
Once we have completed the suite of scans we use the following code to build a context from the following code
private void buildContext() throws ClientApiException {
ApiResponse spiderResponse = clientApi.spider.allUrls();
List<ApiResponse> responseUrls = ((ApiResponseList) spiderResponse).getItems();
for (ApiResponse apiResponse : responseUrls) {
String url = ((ApiResponseElement) apiResponse).getValue();
clientApi.context.includeInContext(contextName, url + "*");
}
}
which is called in this method for generating reports.
public void reports() throws ClientApiException, IOException, TransformerException {
checkScanStatus();
buildContext();
List<ZapAlert> falsePositives = getUnmarkedFalsePositives();
updateFalsePositives(falsePositives);
clientApi.reports.generate(contextName,
"traditional-html",
null,
null,
contextName,
null,
null,
null,
null,
contextName,
null,
TARGET_OUTPUT_DIR,
"true");
clientApi.reports.generate(contextName,
"traditional-xml",
null,
null,
contextName,
null,
null,
null,
null,
contextName,
null,
TARGET_OUTPUT_DIR,
"true");
xmlToCsv();
}
The xmlToCsv method converts the xml report into a csv for our reporting and updating false positives calls the API to ensure anything zap is being a bit overzealous about is marked appropriately.