How can I get the received response data and verify it?

The essence of the test is that I want to get the previously saved data. Obtained using a global variable and a random value via the post request method. If I run the test, I will get the data only in a HAR file. But I can’t figure out how to output data from the response for verification and processing.


image

1 Like

Hi @jmad, :wave:

Thank you for posting your question on our forum.

Since the main language here is English, would you mind changing your topic title to English so that your question would receive better support from other members and our team?

Many thanks,
Albert

1 Like

Promptness is a sin)

1 Like

I don’t understand your question. From where do you want to get the received response data? from the ResponseObject returned by the WS keyword? or from the HAR file?

Please show your test case as text, not in the Manual mode. Copy and paste the full source code here if possible. I ask you because your test case seems to be a complex one = a mixture of WebUI and WS keywords. That’s unusual. I need to read the code, otherwise I wouldn’t be able to understand your question.

1 Like

Yes, you’re right. There was a need for this type of test. Therefore, I divided the test case into small ones, since it is huge in its whole form. I made a separation in the form of “_ _ _ _” so that it was clear where the new test case begins.


WebUI.click(findTestObject(‘Object Repository/Operators OTI/Page_/Operators OTI add’))

GlobalVariable.nameOTI = RandomStringUtils.randomAlphanumeric(7)

WebUI.setText(findTestObject(‘Object Repository/Operators OTI/Page_/FullNameOTI’), GlobalVariable.nameOTI)

WebUI.setText(findTestObject(‘Object Repository/Operators OTI/Page_/SmallNameOTI’), ‘’ + RandomStringUtils.randomAlphanumeric(
9))

WebUI.setText(findTestObject(‘Object Repository/Operators OTI/Page_/code1c’), ‘’ + RandomStringUtils.randomAlphanumeric(
6))

WebUI.setText(findTestObject(‘Object Repository/Operators OTI/Page_/213213123123’), ‘’ + RandomStringUtils.randomNumeric(
12))

WebUI.setText(findTestObject(‘Object Repository/Operators OTI/Page_/Post address’), ‘’ + RandomStringUtils.randomNumeric(
6))

WebUI.setText(findTestObject(‘Object Repository/Operators OTI/Page_/Telephone’), ‘’ + RandomStringUtils.randomNumeric(11))

WebUI.setText(findTestObject(‘Object Repository/Operators OTI/Page_/DopTelephone’), ‘’ + RandomStringUtils.randomNumeric(
11))

WebUI.setText(findTestObject(‘Object Repository/Operators OTI/Page_/email’), ‘’ + RandomStringUtils.randomAlphanumeric(9))


WebUI.click(findTestObject(‘Object Repository/Operators OTI/Page_/Save’))

WebUI.click(findTestObject(‘Object Repository/Operators OTI/Page_/Accept’))

WebUI.click(findTestObject(‘Object Repository/Operators OTI/Page_/Back on the list’))


WebUI.refresh()

WebUI.delay(5)

WebUI.clearText(findTestObject(‘Operators OTI/Page_/search a new operator OTI’))

WebUI.click(findTestObject(‘Operators OTI/Page_/search a new operator OTI’))

WebUI.sendKeys(findTestObject(‘Operators OTI/Page_/search a new operator OTI’), GlobalVariable.nameOTI)

WebUI.click(findTestObject(‘Operators OTI/Page_/Search’))

WebUI.delay(3)

def response = WS.sendRequest(findTestObject(‘Postman/api/v1/oti/operator/all/Полный список операторов ОТИ’))

statusCode = WS.getResponseStatusCode(response)

WS.verifyResponseStatusCode(response, 200)
assertThat(response.getStatusCode()).isEqualTo(200)

WebUI.click(findTestObject(‘Operators OTI/Page_/Menu’))

WebUI.click(findTestObject(‘Operators OTI/Page_/Editing’))


…next end test case…

yes. from the HAR file I meant the data itself

You do not need to analyze the HAR file. You can get the JSON response out of the response body. You just do not know how to get access to the JSON in the response body.

Katalon provides a sample project for Webservice/API test:

This project contains a test case named “GET stuff”:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

File outputFile = new File("index.html");
OutputStream outputStream = new FileOutputStream(outputFile);

ResponseObject response = WS.sendRequest(findTestObject("GET example.com"));
HttpBodyContent content = response.getBodyContent();

// see https://docs.katalon.com/javadoc/com/kms/katalon/core/testobject/HttpBodyContent.html
println "content.getContentEncoding() = " + content.getContentEncoding();
println "content.getContentLength() = " + content.getContentLength();
println "content.getContentType() = " + content.getContentType();

content.writeTo(outputStream)

In here you can find a line:

HttpBodyContent content = response.getBodyContent();

which is getting access to the response body.

If you want to see the content in the response body as a character string, do as follows:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

ByteArrayOutputStream baos = new ByteArrayOutputStream()

ResponseObject response = WS.sendRequest(findTestObject("GET example.com"));
HttpBodyContent content = response.getBodyContent();

// see https://docs.katalon.com/javadoc/com/kms/katalon/core/testobject/HttpBodyContent.html
println "content.getContentEncoding() = " + content.getContentEncoding();
println "content.getContentLength() = " + content.getContentLength();
println "content.getContentType() = " + content.getContentType();

content.writeTo(baos)

String s = baos.toString(content.getContentEncoding() ?: 'utf-8')
println s

When I ran this, I got the following output in the console:

2023-07-25 21:39:52.786 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2023-07-25 21:39:52.791 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/print response body
2023-07-25 21:39:55.276 INFO  c.k.k.core.webservice.common.HarLogger   - HAR: /var/folders/7m/lm7d6nx51kj0kbtnsskz6r3m0000gn/T/Katalon/Test Cases/print response body/20230725_213948/requests/main/0.har
content.getContentEncoding() = null
content.getContentLength() = 1256
content.getContentType() = text/html
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

2023-07-25 21:39:55.892 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/print response body

You would just want to change the URL to get to the one you want. You would see a JSON printed in the console.

I try to repeat, but in any case, a java.lang.NullPointerException notification comes out when I switch to manual mode. Maybe I’m doing something wrong? Can you show me an example of my script?

or the test ends with an error on ResponseObject response = WS.sendRequest(findTestObject(“my Url”))

No, I can’t. I do not have your code and your runtime environment (target URL to test). How come can I do it?

You must have got a full StackTrace message printed. In the message, you would find “Root Cause” annotated with the line# of your test case where you got the NPE raised. Once you got the line#, you can look back your test case source. That’s where you have a coding mistake. Please fix your mistake.

Yes, you were right. I found the error and now everything is ok :+1:

It was between the chair and the keyboard?

Sort of. After 12 hours of work, the eyes fail)

great tips!!