Try โ€‚โ€‰HackMD

SideeX WebService Client API for Python

SideeX WebService Client API primarily handles the transfer of test suites to a hosted SideeX WebService server and returns the test reports.

Prerequisite:

  • pip install asyncio
  • pip install aiohttp

Download

Download the SideeX WebService Client API for Python

The API for Python

ProtocolType

  • Description: ProtocolType is a Enum Class type which specifies the http protocol. HTTP stands for http request; HTTPS_DISABLE stands for https request without certificate; HTTPS_ENABLE stands for https request with certificate.

SideeXWebserviceClientAPI(baseURL, protocolType, caFilePath)

  • Description: The constructor to create a Client API object.
  • baseURL: Base URL of the SideeX WebService server.
  • protocolType: The type of http request used. Default value is set to ProtocolType.HTTP.
  • caFilePath: The file path of the http certificate. Default value is set to None.

runTestSuite(file)

  • Description: Uses the API to run test cases.
  • file: The file that contains test cases.
  • Return:
    โ€‹โ€‹โ€‹โ€‹{ โ€‹โ€‹โ€‹โ€‹ "token": "xxxx" โ€‹โ€‹โ€‹โ€‹}

getState(token)

  • Description: Gets the current test case execution state.
  • token: The token returned from the runTestSuite API.
    • If the token is invalid, the response will be
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹{ โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "ok": false, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "errorMessage": "invalid_token" โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹}
    • If the token is valid and the state is running, the response will be
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹{ โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "ok": true, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "webserviceState": { โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "state": "running" โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ } โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹}
    • If the token is valid and the state is complete, the response will be
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹{ โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "ok": true, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "webserviceState": { โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "state": "complete" โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ }, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "reports": { โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "url": "http://{publicURL_in_serviceconfig}/sideex-webservice/downloadReports?token=xxxx", โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "passed": true, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "summarry": [ โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ { โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "Suites": ["Test_Suite_1"], โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "SideeXVersion": [3,3,7], โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "Browser": "chrome 81.0.4044.138", โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "Platform": "windows", โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "Language": "(default)", โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "StartTime": 1589867469846, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "EndTime": 1589867472874, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "PassedSuite": 1, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "TotalSuite": 1, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "PassedCase": 1, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "TotalPassedCase": 1 โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ } โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ ] โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ }, โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "logs": { โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "url": "http://{publicURL_in_serviceconfig}/sideex-webservice/downloadLogs?token=xxxx" โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ } โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹}

download(fromData, filePath, option)

  • Description: Download HTML test reports or logs.
  • fromData: A JSON object containing
    • token: The token returned from the runTestSuite API
    • file: This parameter is optional. If set file to "reports.zip", the API will return a zip file containing all HTML reports, otherwise, it will return an HTML index webpage.
  • filePath: Set your download file path. e.g.: "./reports"
  • option: If option is set to 0, the API will download reports. If set to 1, it will download logs.

deleteJob(token)

  • Description: Delete the test case and the test reports on SideeX WebService server.
  • token: The token returned from the runTestSuite API.
    • If success, the response will be
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹{ โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "ok": true, "state": "delete_complete" โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹}
    • If the token is invalid, the response will be
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹{ โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "ok": false, "errorMessage": "invalid_token" โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹}
    • If the test case is running, the response will be
      โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹{ โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ "ok": false, "errorMessage": "testcase_is_running" โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹}

How to use

Send a test case file to a SideeX WebService server

import asyncio import aiohttp import json #Include the lib of the SideeX WebService Client API from SideeXWebserviceClientAPI import SideeXWebserviceClientAPI #Create a Client API object connecting to a SideeX ws_client = SideeXWebserviceClientAPI('http://127.0.0.1:50000', ProtocolType.HTTP) #Prepare a test case file file = open('testcase.zip','rb') #Send the test case file to the server and get a token print(asyncio.run(ws_client.runTestSuite(file)))

Get the current test execution state from the server

#Get the current state token = "xxxx" print(asyncio.run(ws_client.getState(token)))

Download the test reports and logs

#Download the test reports as an HTML index webpage token = "xxxx" froamData = aiohttp.FormData() froamData.add_field('token', token, content_type='multipart/form-data') froamData.add_field('file', "index.html", content_type='application/x-www-form-urlencoded') asyncio.run(ws_client.download( froamData, "./index.html", 0)) #Download the logs as a zip file token = "xxxx" froamData = aiohttp.FormData() froamData.add_field('token', token, content_type='multipart/form-data') froamData.add_field('file', "reports.zip", content_type='multipart/form-data') asyncio.run(ws_client.download( froamData, "./reports.zip", 0)) # Download the logs token = "xxxx" froamData = aiohttp.FormData() froamData.add_field('token', token, content_type='multipart/form-data') froamData.add_field('file', "reports.zip", content_type='multipart/form-data') asyncio.run(ws_client.download( froamData, "./logs.zip", 1))

Delete the test job from the server

#Delete the test job token = "xxxx" print(asyncio.run(ws_client.deleteTestJob(token)))

A complete example:

#Include the Client API lib import asyncio import aiohttp import json SideeXWebServiceClientAPI = __import__("sideex-webservice-client").SideeXWebServiceClientAPI ProtocolType = __import__("sideex-webservice-client").ProtocolType async def delay(time): await asyncio.sleep(time) if __name__=="__main__": #Connect to a SideeX WebService server ws_client = SideeXWebServiceClientAPI('http://127.0.0.1:50000', ProtocolType.HTTP) file = open('testcase.zip','rb') token = json.loads(asyncio.run(ws_client.runTestSuite(file)))['token']# get the token flag = False #Check the execution state every 2 seconds while not flag: #Get the current state state = json.loads(asyncio.run(ws_client.getState(token)))['webservice']['state']# get the WebService current state if (state != "complete" and state != "error"): print(state) asyncio.run(delay(2)) #If the test is error elif state == "error": flag = True #If the test is complete else: print(state) froamData = aiohttp.FormData() froamData.add_field('token', token, content_type='application/x-www-form-urlencoded') froamData.add_field('file', "reports.zip", content_type='application/x-www-form-urlencoded') #download the test report to the target file path asyncio.run(ws_client.download( froamData, "./reports.zip", 0)) #Download the logs froamData = aiohttp.FormData() froamData.add_field('token', token, content_type='application/x-www-form-urlencoded') asyncio.run(ws_client.download( froamData, "./logs.zip", 1)) #Delete the test case and report from the server print(asyncio.run(ws_client.deleteJob(token))) flag = True