无法生成请求:在加特林脚本中未定义名为“ token”的属性

时间:2019-10-12 21:51:27

标签: qa gatling scala-gatling

我将HAR文件转换为Gatling模拟脚本,但是从请求标头提取令牌时遇到问题。转换后,我有以下代码:

import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormControl, FormGroup} from '@angular/forms';

@Component({
  selector: 'datepicker-example',
  templateUrl: 'datepicker-example.html',
  styleUrls: ['datepicker-example.css'],
})
export class DatepickerExample implements OnInit {
  myGroup: FormGroup

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.myGroup = this.fb.group({
      dateFormCtrl: new FormControl(new Date())
    })
  }
}

但是当我改变

<div [formGroup]="myGroup">
  <mat-form-field>
    <input matInput formControlName="dateFormCtrl" [matDatepicker]="picker"  
      placeholder="When is your event?" readonly>
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
  </mat-form-field>

  <mat-form-field>
    Selected date:
    <input matInput [value]="myGroup.get('dateFormCtrl').value | date">
  </mat-form-field>
</div>


import scala.concurrent.duration._

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

class LoginTest extends Simulation {

    val httpProtocol = http
        .baseUrl("https://webapi.mytestsite.com)
        .inferHtmlResources()
        .userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36")

    val headers_0 = Map(
        "Accept" -> "application/json, text/plain, */*",
        "Origin" -> "https://mytestsite.com",
        "Sec-Fetch-Mode" -> "cors")

    val headers_1 = Map(
        "Access-Control-Request-Headers" -> "authorization",
        "Access-Control-Request-Method" -> "GET",
        "Origin" -> "https://mytestsite.com",
        "Sec-Fetch-Mode" -> "cors")

    val headers_2 = Map(
        "Accept" -> "application/json, text/plain, */*",
        "Origin" -> "https://mytestsite.com",
        "Sec-Fetch-Mode" -> "cors",
        "authorization" -> "Bearer ipV0mVqxcVO7xQUJ6lBPNxn77tWCivmO7Gohe47z7d7skAJV-mRCuVOwT1YmJVOvZ8N-T-ZFCgvT4nclOsfI303G7Y4EBIS9pFl4iU23C8l1k6Di387ZnAuDIkncHAne9HukSN9bBf1in9L8ath8fE57gx1CZw-kDXU4K67Q9F9brj413mMZiQrIp6VMJaFFPmMcEgq-h0uo9lP03qTdz6Aq_GdxZOV9iXls2nGG5j6nt_cFaxoTFICn0nwKRjR7eJloyUXmF3wfoqkvC_5t5gZ_YKDMf_mLxg4V2seBVyM1O11EwOpGpTOZG5FYDnJoarDybX6QkE-F4eb5aZM27NiPjstMwMn4zQ2EvhuinLNWHBzTtRN0MNW8TLT0X4f6hNS0F9lmjWBzl_OF-wCzoYQiFM7tQsU2UkHC1FHROJbPQDOH6oxDG06u8Nnco07cmLKn60zE4FYDa52DTt1lNGK7GIP97LgrGCHEdJByN4U")

    val headers_3 = Map("Sec-Fetch-Mode" -> "no-cors")

    val headers_6 = Map(
        "Origin" -> "https://mytestsite.com",
        "Sec-Fetch-Mode" -> "cors",
        "content-type" -> "application/x-www-form-urlencoded; charset=UTF-8")

    val uri1 = "https://api.wage.iteodev.com/signalr"
    val uri2 = "https://mytestsite.com/static/media/download-app-headline.a2c2b312.svg"

    val scn = scenario("LoginTest")
        .exec(http("request_0")
            .post("/api/oauth/token")
            .headers(headers_0)
            .formParam("username", "yelari@malboxe.com")
            .formParam("password", "Zaq1@wsx")
            .formParam("grant_type", "password")
            .resources(http("request_1")
            .options("/api/account")
            .headers(headers_1),
            http("request_2")
            .get("/api/account")
            .headers(headers_2),
            http("request_3")
            .get(uri2 + "")
            .headers(headers_3),
            http("request_4")
            .options("/api/conversations/")
            .headers(headers_1),
            http("request_5")
            .options("/api/notifications")
            .headers(headers_1),
            http("request_6")
            .get(uri1 + "/negotiate?clientProtocol=1.5&Authorization=Bearer%20ipV0mVqxcVO7xQUJ6lBPNxn77tWCivmO7Gohe47z7d7skAJV-mRCuVOwT1YmJVOvZ8N-T-ZFCgvT4nclOsfI303G7Y4EBIS9pFl4iU23C8l1k6Di387ZnAuDIkncHAne9HukSN9bBf1in9L8ath8fE57gx1CZw-kDXU4K67Q9F9brj413mMZiQrIp6VMJaFFPmMcEgq-h0uo9lP03qTdz6Aq_GdxZOV9iXls2nGG5j6nt_cFaxoTFICn0nwKRjR7eJloyUXmF3wfoqkvC_5t5gZ_YKDMf_mLxg4V2seBVyM1O11EwOpGpTOZG5FYDnJoarDybX6QkE-F4eb5aZM27NiPjstMwMn4zQ2EvhuinLNWHBzTtRN0MNW8TLT0X4f6hNS0F9lmjWBzl_OF-wCzoYQiFM7tQsU2UkHC1FHROJbPQDOH6oxDG06u8Nnco07cmLKn60zE4FYDa52DTt1lNGK7GIP97LgrGCHEdJByN4U&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
            .headers(headers_6),
            http("request_7")
            .get("/UploadedFiles/03765fee-ede8-4689-9a4c-13dd2ada18a4.png")
            .headers(headers_3),
            http("request_8")
            .get("/api/notifications")
            .headers(headers_2),
            http("request_9")
            .get("/api/conversations/")
            .headers(headers_2),
            http("request_10")
            .get("/UploadedFiles/26ac4d69-8a63-4575-bec4-849d5a5e194a.png")
            .headers(headers_3),
            http("request_11")
            .get(uri1 + "/start?transport=serverSentEvents&clientProtocol=1.5&Authorization=Bearer%20ipV0mVqxcVO7xQUJ6lBPNxn77tWCivmO7Gohe47z7d7skAJV-mRCuVOwT1YmJVOvZ8N-T-ZFCgvT4nclOsfI303G7Y4EBIS9pFl4iU23C8l1k6Di387ZnAuDIkncHAne9HukSN9bBf1in9L8ath8fE57gx1CZw-kDXU4K67Q9F9brj413mMZiQrIp6VMJaFFPmMcEgq-h0uo9lP03qTdz6Aq_GdxZOV9iXls2nGG5j6nt_cFaxoTFICn0nwKRjR7eJloyUXmF3wfoqkvC_5t5gZ_YKDMf_mLxg4V2seBVyM1O11EwOpGpTOZG5FYDnJoarDybX6QkE-F4eb5aZM27NiPjstMwMn4zQ2EvhuinLNWHBzTtRN0MNW8TLT0X4f6hNS0F9lmjWBzl_OF-wCzoYQiFM7tQsU2UkHC1FHROJbPQDOH6oxDG06u8Nnco07cmLKn60zE4FYDa52DTt1lNGK7GIP97LgrGCHEdJByN4U&connectionToken=P%2FGWEN0HNtZWUowxiulVcCbSNQGTg%2BtCrti09SWiZKkPiFyOPNuEW4HBfWX%2FTPbRLhbhW5sst3BwnE3ihcbCvNE9m42MoguFmLCUGUib12Xrqsb8mZ7LsjlhYh5ul07Q1ups4KfOqxgcT0bx%2F03o1A%3D%3D&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
            .headers(headers_6)))

    setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}

当我更改

http("request_2")
            .get("/api/account")
            .headers(headers_2),

http("request_2")
            .get("/api/account")
            .headers(headers_2)
                        .check(header("Bearer").saveAs("token"))

我遇到以下错误: https://i.postimg.cc/0yTMKrvm/2019-10-12-23-46-37-C-WINDOWS-system32-cmd-exe.png

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

看截图,您得到的错误是:No attribute named 'token' is defined

这表明您没有保存标题token中的Bearer

调用/api/account时,请仔细查看HTTP请求-尤其是标头,并检查标头是否如预期的那样存在。您可以为此使用Chrome开发工具或任何HTTP代理。

您可能还想在Gatling中调试脚本,以便可以看到捕获的所有变量-有关此内容的一些想法,请参阅此帖子-https://www.james-willett.com/debug-gatling/