API and web test combined

Is it possible to combine API and web test and use the response from API test as test data for Web Tests?

Thanks

Yes, you can.

When you create a test case in Katalon Studio, it generates the following import statements:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobileimport com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

This means your test case can call both of WebUI.xxx keywords and WS.xxx keywords wherever you like.

That’s right direction to start with. Do you have any samples or any examples you can point me to?

Thanks

I made a Katalon Studio project. The project does the following:

  1. get the Yahoo! RSS feed at Yahoo News - Latest News & Headlines using Katalon Web Service keyword WS.sendRequest(TestObject)
  2. parse the XML document and find a list of URLs of contained news pages using XPath. Basic knowledge about XML processing in Java is required.
  3. iterate over the list of URLs found; open the URL in browser, take screenshots and save the image into /tmp folder

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import java.nio.file.Path
import java.nio.file.Paths
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPath
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory
import org.w3c.dom.Document
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance()
    dbfactory.setNamespaceAware(true)ResponseObject response = WS.sendRequest(findTestObject('Yahoo.com RSS mostviewed'))
if (response.isXmlContentType()) {
    println "is XML"
    HttpBodyContent content = response.getBodyContent()
    println "content encoding: ${content.getContentEncoding()}"
    println "content length: ${content.getContentLength()}"
    println "content type: ${content.getContentType()}"
    InputStream is = content.getInputStream()
    // parsing XML document
    DocumentBuilder db = dbfactory.newDocumentBuilder()
    Document document = db.parse(is)
    // get access to Items using XPath
    XPath xpath = XPathFactory.newInstance().newXPath()
    WebUI.openBrowser('')
    Path outdir = Paths.get(RunConfiguration.getProjectDir()).resolve('tmp')
    for (int x = 1; x <= 3; x++) {
        String url = (String)xpath.evaluate("/rss/channel/item[${x}]/link",                                            document, XPathConstants.STRING)
        println "url: ${url}"
        if (url != null) {
            // taking evidence screenshot
            WebUI.navigateToUrl(url)
            WebUI.delay(2)
            Path outfile = outdir.resolve(URLEncoder.encode(url,'UTF-8') + ".png")
            WebUI.takeScreenshot(outfile.toString())
        }
    }
    WebUI.closeBrowser()
}

I got 3 png files successfully.

You can downlowd the zip file of this sample project at Releases · kazurayam/KatalonDiscussion10444 · GitHub

KS10444_TestObject.PNG

2 Likes

kazurayam said:

I made a Katalon Studio project. The project does the following:

  1. get the Yahoo! RSS feed at Yahoo News - Latest News & Headlines using Katalon Web Service keyword WS.sendRequest(TestObject)
  2. parse the XML document and find the URL of contained news pages using XPath. Basic knowledge about XML processing in Java is required.
  3. iterate over the list of URLs found; open the URL in browser, take screenshots and save the image into /tmp folder

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

import java.nio.file.Path
import java.nio.file.Paths
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPath
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory
import org.w3c.dom.Document
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance()
dbfactory.setNamespaceAware(true)ResponseObject response = WS.sendRequest(findTestObject(‘Yahoo.com RSS mostviewed’))
if (response.isXmlContentType()) {
println “is XML”
HttpBodyContent content = response.getBodyContent()
println “content encoding: ${content.getContentEncoding()}”
println “content length: ${content.getContentLength()}”
println “content type: ${content.getContentType()}”
InputStream is = content.getInputStream()
// parsing XML document
DocumentBuilder db = dbfactory.newDocumentBuilder()
Document document = db.parse(is)
// get access to Items using XPath
XPath xpath = XPathFactory.newInstance().newXPath()
WebUI.openBrowser(‘’)
Path outdir = Paths.get(RunConfiguration.getProjectDir()).resolve(‘tmp’)
for (int x = 1; x <= 3; x++) {
String url = (String)xpath.evaluate(“/rss/channel/item[${x}]/link”, document, XPathConstants.STRING)
println “url: ${url}”
if (url != null) {
// taking evidence screenshot
WebUI.navigateToUrl(url)
WebUI.delay(2)
Path outfile = outdir.resolve(URLEncoder.encode(url,‘UTF-8’) + “.png”)
WebUI.takeScreenshot(outfile.toString())
}
}
WebUI.closeBrowser()
}


  
  
I got 3 png files successfully.  
  
You can downlowd the zip file of this sample project at https://github.com/kazurayam/KatalonDiscussion10444/releases

  

kazurayam said:

I made a Katalon Studio project. The project does the following:

  1. get the Yahoo! RSS feed at Yahoo News - Latest News & Headlines using Katalon Web Service keyword WS.sendRequest(TestObject)
  2. parse the XML document and find the URL of contained news pages using XPath. Basic knowledge about XML processing in Java is required.
  3. iterate over the list of URLs found; open the URL in browser, take screenshots and save the image into /tmp folder

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

import java.nio.file.Path
import java.nio.file.Paths
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPath
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory
import org.w3c.dom.Document
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance()
dbfactory.setNamespaceAware(true)ResponseObject response = WS.sendRequest(findTestObject(‘Yahoo.com RSS mostviewed’))
if (response.isXmlContentType()) {
println “is XML”
HttpBodyContent content = response.getBodyContent()
println “content encoding: ${content.getContentEncoding()}”
println “content length: ${content.getContentLength()}”
println “content type: ${content.getContentType()}”
InputStream is = content.getInputStream()
// parsing XML document
DocumentBuilder db = dbfactory.newDocumentBuilder()
Document document = db.parse(is)
// get access to Items using XPath
XPath xpath = XPathFactory.newInstance().newXPath()
WebUI.openBrowser(‘’)
Path outdir = Paths.get(RunConfiguration.getProjectDir()).resolve(‘tmp’)
for (int x = 1; x <= 3; x++) {
String url = (String)xpath.evaluate(“/rss/channel/item[${x}]/link”, document, XPathConstants.STRING)
println “url: ${url}”
if (url != null) {
// taking evidence screenshot
WebUI.navigateToUrl(url)
WebUI.delay(2)
Path outfile = outdir.resolve(URLEncoder.encode(url,‘UTF-8’) + “.png”)
WebUI.takeScreenshot(outfile.toString())
}
}
WebUI.closeBrowser()
}


  
  
I got 3 png files successfully.  
  
You can downlowd the zip file of this sample project at https://github.com/kazurayam/KatalonDiscussion10444/releases

  

kazurayam said:

I made a Katalon Studio project. The project does the following:

  1. get the Yahoo! RSS feed at Yahoo News - Latest News & Headlines using Katalon Web Service keyword WS.sendRequest(TestObject)
  2. parse the XML document and find the URL of contained news pages using XPath. Basic knowledge about XML processing in Java is required.
  3. iterate over the list of URLs found; open the URL in browser, take screenshots and save the image into /tmp folder

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

import java.nio.file.Path
import java.nio.file.Paths
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPath
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory
import org.w3c.dom.Document
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance()
dbfactory.setNamespaceAware(true)ResponseObject response = WS.sendRequest(findTestObject(‘Yahoo.com RSS mostviewed’))
if (response.isXmlContentType()) {
println “is XML”
HttpBodyContent content = response.getBodyContent()
println “content encoding: ${content.getContentEncoding()}”
println “content length: ${content.getContentLength()}”
println “content type: ${content.getContentType()}”
InputStream is = content.getInputStream()
// parsing XML document
DocumentBuilder db = dbfactory.newDocumentBuilder()
Document document = db.parse(is)
// get access to Items using XPath
XPath xpath = XPathFactory.newInstance().newXPath()
WebUI.openBrowser(‘’)
Path outdir = Paths.get(RunConfiguration.getProjectDir()).resolve(‘tmp’)
for (int x = 1; x <= 3; x++) {
String url = (String)xpath.evaluate(“/rss/channel/item[${x}]/link”, document, XPathConstants.STRING)
println “url: ${url}”
if (url != null) {
// taking evidence screenshot
WebUI.navigateToUrl(url)
WebUI.delay(2)
Path outfile = outdir.resolve(URLEncoder.encode(url,‘UTF-8’) + “.png”)
WebUI.takeScreenshot(outfile.toString())
}
}
WebUI.closeBrowser()
}


  
  
I got 3 png files successfully.  
  
You can downlowd the zip file of this sample project at https://github.com/kazurayam/KatalonDiscussion10444/releases

  

Kazurayam, Thanks a lot for the example, there is lot to learn about the abilities of this tool from this example, really appreciate your help.

Kazurayam, Thanks a lot for the example, there is lot to learn about the abilities of this tool from this example, really appreciate your help.