Forum Discussion

Shilzoting's avatar
Shilzoting
Occasional Contributor
2 years ago

How to create and send Json request in readyapi through script

Hi

I am writing Zephyr scale test case execution status update script in readyAPI. I want to create script which will create request add Authorization header and execute this request in afterTestcase event handler, but I am not sure how i can do it readyAPI 

can anyone please help me with this?

Thanks in advance 

  • nmrao's avatar
    nmrao
    Champion Level 3

    I want to create script which will create request

    One step at a time. How this request looks like? Is it fixed always? or changes values?

    • Shilzoting's avatar
      Shilzoting
      Occasional Contributor

      Yes, I am getting value from each Testcase Property and assign to Zephyr scale api parameter I am trigging this aftertestCase  event, below is my script but it's not working in event 

       

      def tcobject = testCaseRunner.getTestCase()
      def tsobject = testCaseRunner.getTestCase().testSuite
      def projobject = testCaseRunner.getTestCase().testSuite.project
      def stepList = tcobject.getTestStepList();

      // // Get all test cases’ names from the suite
      def testCaseName = tcobject.name;
      def testCaseID = projobject.getPropertyValue("testCaseKey")
      def testCycleID = projobject.getPropertyValue("TestCycleID");
      def projectKey = projobject.getPropertyValue("projectID")
      testcaseStatus=tcobject.getPropertyValue("tcstatus")

      log.info("testCaseName"+testCaseName)
      log.info("projectKey1"+projectKey)
      log.info("testcaseKey1"+testCaseID)
      log.info("testcycleKey1"+testCycleID)
      log.info("statusinpost1"+testcaseStatus)
      log.info(projectKey.length() )
      log.info(testCaseID.length() )

      // log.info "projectKey : $projectKey , TestCaseName : $testCaseName , TestCaseIID : $testCaseID, TestCycleID: $testCycleID , ";
      if (testCaseID == null || testCaseID.length() == 0 || testCycleID == null || testCycleID.length() == 0 || projectKey == null || projectKey.length() == 0) {
      log.info "MANDATORY FIELDS NOT AVAILABLE. Please check Ready API script to confirm they have all fields defined"
      return 0
      }

      // testcaseStatus="PASS"
      //testRunner.getResults()
      log.info(testcaseStatus)
      // Check whether the case has failed
      if (testcaseStatus == 'FAIL') {
      // Log failed cases and test steps’ resulting messages
      log.info "$testCaseName has failed"
      for (testStepResult in testCaseRunner.getResults()) {
      testStepResult.messages.each() {
      msg -> log.info msg
      }
      }
      postToZephyrScale(projectKey, testCaseID, testCycleID, "Fail")
      } else if (testcaseStatus == 'PASS') {
      postToZephyrScale(projectKey, testCaseID, testCycleID, "Pass")
      log.info "$testCaseName Test Passed";
      }

      // log.info "Results updation to Jira is complete"
      //String projectKey, String testcaseKey, String testcycleKey, String status, String comment)
      def postToZephyrScale( String projectKey, String testcaseKey, String testcycleKey, String status ){

      def today = new Date()
      def formattedDate = today.format("yyyy-MM-dd'T'HH:mm:ssZ")
      log.info("postToZephyrScale")
      log.info("projectKey"+projectKey)
      log.info("testcaseKey"+testcaseKey)
      log.info("testcycleKey"+testcycleKey)
      log.info("statusinpost"+status)


      def project = testRunner.testCase.testSuite.project
      def requestTc = project.testSuites["UpdateTCsStatus Suite"].testCases["TestExecutions Test Case"]
      def requestSTP = requestTc.getTestStepByName("StatusUpdate")
      def request = requestSTP.getHttpRequest()

      requestSTP.setPropertyValue("testCaseKey",testcaseKey.toString )
      requestSTP.setPropertyValue("status",status.toString )
      runner=requestSTP.run(testCaseRunner,context)
      // log.info ("runner status ....:"+ runner.hasResponse())

      }