Selenium API Test Scripting

Prev Next

Catchpoint relies on a customized subset of the Selenium language to perform API tests. At the end of this article is a list of supported Selenium verbs.

Configuration

Catchpoint API Test scripts currently consist of manually typed scripts. Fortunately, writing API Test scripts is straightforward since the editor provides syntax highlighting, autocomplete/suggestions, and tooltip hints.

  1. In Control Center > Tests, select New > API
    Pic_7.png

  2. In the Test Properties page you can start writing your script:
    Pic_18.png

The autocomplete function in the script editor will become present upon typing. The hints can be viewed by clicking into the center of the command's content:

Verb:

Macro:

  1. It is strongly recommended that you run an instant test and capture headers/response content to make sure your script is working at this point.

Example Scripts

Most API Test scripts involve opening a URL, setting a header, extracting some content into a variable, using that variable in a second step where a new URL is opened, and asserting the server's response.

Please note that the assertHtmlSource() command is used to assert the response content regardless of a content type.

Here is a somewhat complex script that repeatedly opens a URL every 5 seconds in the second step and asserts that a string must be not present in the response:

//Step - 1
open("http://example.com/channel/a58231f29bc5423aafd2edf246da5d48.m3u8")
setHeader("Cookie", "foo=bar")
storeVariable("\${extract(resp-content,regexp://\[\w/\\-\\.\\?=&%\]+)}", "url")

//Step - 2
openWhile("http:\${var(url)}", "\${var(loopCounter, le(5))}")
storeVariable("\${extract(resp-content,regexp:http://(\[\w/\\-\\.\\?=&%\]+))}", "ts")
AssertNotHtmlSource("d959b7cb7e464571b9417e54494e4f46")
pause("5000")
storeVariable("\${var(loopCounter,add(1))}", "loopCounter")

A couple of sample scripts to assert an HTML Source are below:

open(io.catchpoint.com)
assertHttpResponseCode(200)
assertHtmlSource('<script language=\"JavaScript\">')
\\or
assertHtmlSource('<script language="JavaScript">')

open(io.catchpoint.com)
var a = ${extract('resp-content','regexp:script language="([a-zA-Z]+\S+)"')};
assert(${var(a,eq('JavaScript'))})
assertHttpResponseHeader(Content-Type,text/html)

Example Script To Modify HTTP Request Methods
By default, the open command will use the GET request method. Using a setNavigatePostData() command will change the request to a POST request.

// Step - 1   // POST request to generate token using key and secret
open("https://io.catchpoint.com/ui/api/token")
setStepName("1 - OAuth")
setNavigatePostData("grant_type=client_credentials&client_id=\<key\>&client_secret=\<secret\>")
var token = ${extract('resp-content','regexp:access_token":"(\[a-zA-Z0-9\]+\S+)"')};
var tokenEncoded = ${base64("${var(token)}")};

// Step - 2   // GET request to pull list of targeted nodes   open("https://io.catchpoint.com/ui/api/v1/nodes?targeted=true")   setStepName("2 - Get Nodes")   setHeader("Authorization", "Bearer \${var(tokenEncoded)}")   setHeader("Content-Type", "application/json")

By appending a secondary parameter to the open command you can use different types of request methods such as HEAD, DELETE and PUT.

For more information, refer to this article.

Supported Commands

Here is a list of the available commands in API Tests:

Command
assert
assertHtmlSource
assertHttpResponseCode
assertHttpResponseHeader
assertNotHtmlSource
assertNotHttpResponseCode
assertNotHttpResponseHeader
open
openWhile
pause
setHeader
setHeaderAll
setIndicator
setNavigatePostData
setStepName
setTracepoint
storeGlobalVariable
storeVariable
verify
verifyHtmlSource
verifyHttpResponseCode
verifyHttpResponseHeader
verifyNotHtmlSource
verifyNotHttpResponseCode
verifyNotHttpResponseHeader

Supported Macros

Here is a list of the available macros in API Tests. More information regarding macros can be found here.

Macro
AsnName
AsnValue
Base64
CityId
CityName
ContinentName
CountryName
Extract
ExtractChild
ExtractVariable
File
Global Variable
Hash
Hex
HostRequestAuthority
HostRequestHost
HostRequestPath
HostRequestPathAndQuery
HostRequestQuery
HostRequestUrl
If
IpVersion
IspId
IspName
LocationId
LocationLatitude
LocationLongitude
LocationName
NetworkType
Password
Random
RandomGuid
RandomList
RegionName
SequentialList
SequentialListByLocation
SequentialListByRun
Switch
TestId
Time
TimeDay
TimeDiff
TimeEpoch
TimeFormat
TimeTrim
UrlDecode
UrlEncode
UserName
Var

Regular Expressions

Catchpoint supports Selenium Regular Expression matching implementation, which has four types:

  1. glob: For regular command-line styled matching (where *= any char, and ? = any single char). Default regex match.
  2. regexp: For case-sensitive regular expressions.
  3. regexp: For case-insensitive regular expressions.
  4. exact: For exact matching.

Example:

**storeVariable("\${extract('resp-header','regexp:Location:\s\*(\S+)')}", "saved_url")**

For JSON testing, see the article: How To Handle JSON in Tests

You can also do API Scripting using Javascript. For more info read this article.