加特林场景下如何执行一次登录步骤?

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

我正在使用 Ga特林来模拟网站上的用户交互,并且我有一个场景,我需要执行登录步骤,然后继续执行其他请求,例如“添加列表”、“更新列表”等。但是,我想要仅对模拟中的所有用户执行一次登录步骤,并在后续请求中重复使用身份验证令牌。

这是我当前的加特林模拟代码:

public class PerfCopy extends Simulation {
    String csrfToken = null;
    String isLogin = "0";


    // Run the scenario
    public OcusellPerfCopy() {
        HttpProtocolBuilder httpProtocol = http
                .baseUrl("https://dev.com/")
                .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
                .acceptLanguageHeader("en-US,en;q=0.5")
                .acceptEncodingHeader("gzip, deflate, br")
                .userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0");

        // Define the scenario
        ScenarioBuilder scn = scenario("Scenario")
                .exec(session -> session.set("isLogin", "0"))
                .exec(http("CSRF_TOKEN")
                        .get("/login")
                        .check(status().is(200))
                        .check(regex("<meta name=\"csrf-token\" content=\"(.*?)\"").saveAs("csrfToken")))
                .exec(session -> {
                    csrfToken = session.getString("csrfToken");
                    System.out.println("CSRF Token: " + csrfToken);
                    return session;
                })
                //Login Form
                .exec(http("Login")
                        .post("/login")
                        .headers(Map.of("Content-Type", "application/x-www-form-urlencoded"))
                        .formParam("_token", "#{csrfToken}")
                        .formParam("email", "[email protected]")
                        .formParam("password", "test@1234")
                        .check(status().is(200)))
                //Add Listing
                .exec(http("GET Add Listing")
                        .get("/addProperty")
                        .queryParam("listing_owner", "152")
                        .queryParam("address-autocomplete-street-number", "")
                        .queryParam("address-autocomplete-street-name", "")
                        .queryParam("address-autocomplete-street-suffix", "")
                        .queryParam("address-autocomplete-street-dir", "")
                        .queryParam("address-autocomplete-city", "")
                        .queryParam("address-autocomplete-state", "")
                        .queryParam("address-autocomplete-postal-code", "")
                        .queryParam("address-autocomplete-postal-code-suffix", "")
                        .queryParam("choose_mls[]", "Georgia")
                        .check(status().is(200))
                        .check(regex("<a class=\"\" href=\"https://dev.com/update/(\\d+)\">").saveAs("listingID")))
                .exec(session -> {
                    String listingID = session.getString("listingID");
                    System.out.println("Listing ID: " + listingID);
                    return session;
                })
                //Info section
                .exec(http("POST Info")
                        .post("/update/${listingID}")
                        .headers(Map.of(
                                "Content-Type", "application/json",
                                "X-CSRF-TOKEN", "#{csrfToken}"
                        ))
                        .body(ElFileBody("property_info.json"))
                        .check(status().is(200))
                        .check(bodyString().saveAs("updateListingResponse")))
                .exec(session -> {
                    String responseBody = session.getString("updateListingResponse");
                    System.out.println("POST updateListingResponse Listing Media Response Body: " + responseBody);
                    return session;
                });

        // Define the number of users to simulate
        int numUsers = 10;
        setUp(scn.injectOpen(rampUsers(numUsers).during(1)).protocols(httpProtocol));
    }
}

当我运行这个模拟时,我看到令牌生成了多次,并且列表 id 变得为 null ,我在这里做错了什么

gatling maven-gatling-plugin
© www.soinside.com 2019 - 2024. All rights reserved.