Running API Tests

You can run and edit API tests in the API Test Editor in Eggplant Functional. You also can call API tests from several other places, including from SenseTalk scripts, from schedules, and from the command line.

New feature: API tests are available in Eggplant Functional 17.2 and later.

Run API Tests from a SenseTalk Script

Calling an API test from a SenseTalk script works just like calling one SenseTalk script from another SenseTalk script.

This script calls an API Test named ApiTest1 and returns its URL, response header, and response body.

例:

put ApiTest1() into foo

put foo.url

put foo.responseHeaders

put foo.responseBody

The API Function

Use the api() Function to access all the properties of an API test, including both the request and response. Note that this function always returns the properties from the API test that ran most recently, so you might want to save properties from individual tests as a variable if you want to use them at a later time. This example runs an API test called ApiTest1 and uses the api function to store the response time in a variable called TimeVar.

例:

ApiTest1

put api().responseTime into TimeVar

Override API Test Properties

例:

myApiTest(url: "http://www.google.com", username: "documentation")

Chain API Tests

You can chain API tests together in SenseTalk scripts in the Script Editor. This enables you to use the results of those API tests in larger tests using SenseTalk. The following example uses the results from two API tests, Get_BasicAuth_json and Head_BasicAuth_json, as part of a larger test of an error tracking system.

例:

log "-------------------------------------------------------------------------------------"

log " Feature #XXXX"

log " Support for Basic Authentication"

log "--------------------------------------------------------------------------------------"

 

BeginTestCase "Basic Authentication - Valid Authentication - Get Request"

Get_BasicAuth_json

EndTestCase "Basic Authentication - Valid Authentication - Get Request"

BeginTestCase "Basic Authentication - Valid Authentication - Head Request"

Head_BasicAuth_json

EndTestCase "Basic Authentication - Valid Authentication - Head Request"

BeginTestCase "Basic Authentication - Invalid Authentication"

set the suiteVariables.username to "xxxxx"

Get_BasicAuth_json

EndTestCase "Basic Authentication - Invalid Authentication"

Run API Tests from the Command Line

Use the runscript command to run API tests from the command line. Use the -CommandLineOutput flag to output script logs.

例:

Mac (bash shell):

/Applications/Eggplant.app/Contents/MacOS/runscript /Users/Bob/Documents/APITesting.suite/APITests/ApiTest2.apitest -CommandLineOutput yes

Windows (command shell):

"C:\Program Files\Eggplant\runscript.bat" "C:\Users\Bob\Documents\APITesting.suite\APITests\ApiTest1.apitest" -CommandLineOutput yes

Linux (bash shell):

runscript ~/Documents/APITesting.suite/APITests/Untitled.apitest -CommandLineOutput yes

You can override any parameters you've set in the API test from the command line, as well.

例:

Mac (bash shell):

/Applications/Eggplant.app/Contents/MacOS/runscript /Users/Bob/Documents/APITesting.suite/APITests/ApiTest2.apitest -pv '(url:"http://chicago.publicserver.com/issues/14618.xml")' -CommandLineOutput Yes

Windows (command shell):

"C:\Program Files\Eggplant\runscript.bat" "C:\Users\Bob\Documents\AutoDoctorScreenshots.suite\APITests\JsonGet.apitest" -pv (url:\"http://chicago.publicserver.com/issues/14920.xml\") -CommandLineOutput Yes

Linux (bash shell):

runscript ~/Documents/APITesting.suite/APITests/Untitled.apitest -pv '(url:"http://chicago.publicserver.com/issues/14920.xml")' -CommandLineOutput yes

Run API Tests from a Schedule

Use schedules to define a collection of API tests and scripts to run, the order to run them in, and any parameters or dependencies. Access schedules from the Schedules pane.

API tests and scripts as part of a schedule in Eggplant Functional

Click the arrow to the right of the Status column to view detailed results of the selected test or script run. Note that you don't need to connect to a system under test (SUT) to run a schedule of API tests.

Password Storage and Encryption for API Tests

Passwords you enter in the Authentication fields of the API Test Editor must be in plain text. Eggplant Functional encodes these passwords when you save and decodes them when you load an API test.

You should encode any passwords you choose to save as suite variables, as it's not recommended to save passwords as plain text. Those passwords must then be decoded before they're passed to API tests. Note that you need to use this method only if you're using a form of authentication that's not directly supported in Eggplant Functional.

You can both encode and decode passwords using Eggplant Functional.

Step by Step: Encode Passwords

  1. Open the Run window. In the Ad Hoc Do Box, enter the following code, then click Do:
  2. log encodeText("my password")
  3. Copy the encoded password from the Log pane.
  4. Open the Settings tab and paste the encoded password into a Value field in the Suite Variables section. Enter a name for the password variable in the corresponding Key field.
  5. To override the password property in an API test using a suite variable, use the decodeText function in an API script or a SenseTalk script:

    put decodetext(MyPwd) into myPassword

    ApiTest1(password: myPassword)

 

This topic was last updated on 2月 01, 2019, at 11:13:23 午前.

Eggplant icon Eggplant.io | Documentation Home | User Forums | Support | Copyright © 2019 Eggplant