Search code examples
scalagatlingscala-gatling

Failed to build request: No attribute named 'token' is defined in Gatling script


I convert HAR file to Gatling simulations script, but I have problem with extracting token from request header. After conversion I have the following code:

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", "[email protected]")
            .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)
}

But when I change

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

to

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

and when I change

 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")
            

to

http("request_6")
            .get(uri1 + "/negotiate?clientProtocol=1.5&Authorization=Bearer%20${token}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")

I've got following error: https://i.postimg.cc/0yTMKrvm/2019-10-12-23-46-37-C-WINDOWS-system32-cmd-exe.png

What am I doing wrong?


Solution

  • Looking at the screenshot, the error you are getting is: No attribute named 'token' is defined .

    This suggests that you aren't saving the token from the header Bearer.

    Look closely at the HTTP request when you call /api/account - especially the headers, and check that the header exists as you expect. You could use Chrome Dev Tools or any HTTP proxy for this.

    You might want to debug your Script in Gatling as well, so that you can see all the variables you are capturing - see this post for some ideas on that - https://www.james-willett.com/debug-gatling/