我无法在加特林会话期间访问空手道功能文件中设置的变量

问题描述 投票:0回答:1

我无法在加特林会话期间访问空手道功能文件中设置的变量*

Error Msg:02:35:57.473 [GatlingSystem-akka.actor.default-dispatcher-3] ERROR i.g.c.a.b.SessionHookBuilder$$anon$1 - 'hook-10' crashed on session Session(CreateSubscription,1,1725397536872,Map(orderId1 -> mqj5fjxy, attempts -> 0, customerId ->3432423, requestStartTime -> 1725397536894, 2dd34680-e360-4aaa-aaf9-f325f4e8f461 -> 0),0,OK,List(ExitOnCompleteLoopBlock(attempts), GroupBlock(List(Create and Confirm Subscription),1725397536894,0,OK), ExitOnCompleteLoopBlock(2dd34680-e360-4aaa-aaf9-f325f4e8f461)),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda$485/1658512704@639d7e27), forwarding to the 
next onejava.util.NoSuchElementException: No attribute named 'subscription_confirmed' is defined    at io.gatling.core.session.SessionAttribute.as(Session.scala:46)

我想在空手道功能文件中将 subscription_confirmed 变量设置为 true 并在加特林会话中访问。 **

Subscription.feature

Feature: create Subscription feature

  Background: Obtain test data for the scenario
      * def envName = karate.env
      * def token = “1234”
      * header Authorization = 'Bearer '+token   
      * def CID = __gatling.customerId
      * def orderId = __gatling.orderId1
      * print 'Captured orderId:', orderId
      
  

    Scenario: CreateSubscription
            
          * def createPayLoad = read('classpath:tests/payLoads/CreateSubscriptions_testdata/TestData_Subscriptions.json')
          * url “endurl”
          * path  createSubscription
          And headers {correlationId: #(correlational), env: '#(envName)'}
          * createPayLoad.orderItem[0].id = orderId
          * createPayLoad.relatedParty[0].id = CID
          * request createPayLoad
          * method post
          * status 200
    

 getSubscriptionWithValidation.feature
            
            Feature: Get Subscription   
              Scenario: GetSubscription
            * def getToken = call read('classpath/test/generateToken.feature')
            * def token = getToken.response.access_token
            * header Authorization = 'Bearer '+token
            * url 'https://rest.sandbox.com/v8/subscriptions/123'
            * method get
            * status 200  
            * And match response.data[0].custom_fields.product_id__c == 123
            * def subscription_confirmed = response.data[0].custom_fields.product_instance_id__c ==  123 
            *   And karate.set('subscription_confirmed' ,subscription_confirmed )

加特林脚本:

package performance

import io.gatling.core.scenario.Simulation
import io.gatling.core.Predef._
import com.intuit.karate.gatling.PreDef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

class PerformanceRunner extends Simulation {
  
  val protocol = karateProtocol()
  protocol.nameResolver = (req, ctx) => req.getHeader("scenario")

  System.setProperty("telus.appName", "Performance")
  System.setProperty("KongFlag.value", "true")
  System.setProperty("useToken.value", "true")
  System.setProperty("karate.appName", "appname")

  val cidfeeder = csv("CustomerID.csv").circular

  val createSubscription = scenario("CreateSubscription")
    .repeat(1) {
      feed(ecidfeeder)
        .exec { session =>
          println(session("customerId").as[String])
          session
        }
        .exec(karateFeature("classpath:performance/Subscription.feature"))
        
.exec(session => {
          var confirmed = false
          var attempts = 0
          val maxAttempts = 10
          val pollInterval = 1000 // 5 seconds

          while (!confirmed && attempts < maxAttempts) {
            // Polling GET API
            exec(karateFeature("classpath:services/Zoura/getSubscriptionWithValidation.feature"))
            // Check if the response is confirmed
            if (session.contains("subscription_confirmed")) {
              confirmed = true
            } else {
              attempts += 1
              Thread.sleep(pollInterval)
            }
          }
          if (!confirmed) {
            // Handle failure, if needed
            session.markAsFailed
          }

          session
        })
    }

  setUp(
    createSubscription.inject(rampUsers(1) during (10 seconds)).protocols(protocol)
  )
}

二手加特林版本:0.9.4

karate gatling
1个回答
0
投票

您在功能文件中正确设置了值,我认为您只是在 scala/gadling 文件中错误地访问了它,请尝试在会话块内部进行访问。

.exec(session => {
//capture the set value from the karate feature
val subscription_confirmed = session("subscription_confirmed").as[String]
println(s"Captured value from feature file is : $subscription_confirmed")
session })
© www.soinside.com 2019 - 2024. All rights reserved.