【问题标题】:How to Get Ticket In Gatling using Correlation如何使用相关性在 Gatling 中获取票证
【发布时间】:2013-08-21 07:37:17
【问题描述】:

这是加特林录音机脚本。

val httpProtocol = http
  // LaunchURL
  .baseURL("https://mywebsite/instance")
  .acceptHeader("*/*")
  .acceptEncodingHeader("gzip, deflate")
  .acceptLanguageHeader("en-US,en;q=0.5")
  .connection("keep-alive")
  .userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:26.0) Gecko/20100101 Firefox/26.0")

  // Login
  .exec(http("request_6")
   .post("""/cas/login;jsessionid=cN7KK9FvXzsqWjmLxL2M5xjk.undefined?service=https://mywebsite/instance/index.jsp""")
   .headers(headers_6)
   .param("""username""", """abc""")
   .param("""password""", """abcpwd""")
   .param("""lt""", """LT-828-wppjtrEoGU6gj9UVFt3soVqQ3mLMwe""")
   .param("""execution""", """e1s1""")
   .param("""_eventId""", """submit""")
   .param("""submit""", """LOGIN"""))
   .pause(10)

如果我们看到这三行:

.param("""username""", """abc""")
.param("""password""", """abcpwd""")
.param("""lt""", """LT-828-wppjtrEoGU6gj9UVFt3soVqQ3mLMwe""")

我们将对用户名和密码使用参数化。这些是我们在运行测试时可以从 csv 文件中获取的输入值。这里的“lt”是ticket的参数。它是在我们启动 URL 时由 CAS 生成的。

以下代码是 BaseURL 响应的一部分。

<table width="100%">
  <tr>
    <td>
      <label for="username" class="fl-label"><span class="accesskey">U</span>sername:</label>
      <input id="username" name="username" class="required" tabindex="1" accesskey="u" type="text" value="" size="25" autocomplete="false"/>
    </td>
  </tr>
  <tr>
    <td>                          
      <label for="password" class="fl-label"><span class="accesskey">P</span>assword:</label>
      <input id="password" name="password" class="required" tabindex="2" accesskey="p" type="password" value="" size="25" autocomplete="off"/>
    </td>
  </tr>
  <tr>
    <td>
      <input id="warn" name="warn" value="true" tabindex="3" accesskey="w" type="checkbox" />
      <label for="warn"><span class="accesskey">W</span>arn me before logging me into other sites.</label>
      <input type="hidden" name="lt" value="LT-828-wppjtrEoGU6gj9UVFt3soVqQ3mLMwe" />
      <input type="hidden" name="execution" value="e1s1" />
      <input type="hidden" name="_eventId" value="submit" />
    </td>
  </tr>
  <tr>
    <td>
      <input class="btn-submit" name="submit" accesskey="l" value="LOGIN" tabindex="4" type="submit" />
      <input class="btn-reset" name="reset" accesskey="c" value="CLEAR" tabindex="4" type="reset" />          
    </td>
  </tr>
</table>

这里 CAS 在 BaseURL 响应中生成票证 "LT-828-wppjtrEoGU6gj9UVFt3soVqQ3mLMwe"。在这里,我需要从 BaseURL Response 中提取票证并在登录请求中使用此票证。

之前我在 Jmeter 中使用正则表达式从 BaseURL 响应中提取票证为 name="lt" value="(.*?)"

请帮助我如何在加特林提取车票。

你能告诉我如何关联视图状态吗?

感谢和问候

那拉辛哈

【问题讨论】:

    标签: testing integration-testing load-testing performance-testing gatling


    【解决方案1】:

    首先,您需要向您的服务发出第一个GET 请求:

    http("getLogin")
      .get(casUrl)
    

    考虑到casUrl val 包含您实际服务的路径,那么,只有这样,您才能检索到您需要的信息,比方说,使用 css 表达式:

    http("getLogin")
      .get(casUrl)
      .check(css("input[name='lt']", "value").saveAs("lt"))
    

    检查器用于从请求正文中提取数据。 saveAs 是重要的部分。它将允许您将数据记录到 gatling 的会话中。

    你可以这样重复使用它:

    http("postLogin")
      .post(...)
      ...
      .param("lt", "${lt}")
    

    括号也是强制性的:它注意到 Gatling 尝试在会话中搜索与键 lt 关联的值。

    这是基于您的脚本的完整示例:

    val casUrl = "/cas/login;jsessionid=cN7KK9FvXzsqWjmLxL2M5xjk.undefined?service=https://mywebsite/instance/index.jsp"
    
    val httpProtocol = http
      // LaunchURL
      .baseURL("https://mywebsite/instance")
      .acceptHeader("*/*")
      .acceptEncodingHeader("gzip, deflate")
      .acceptLanguageHeader("en-US,en;q=0.5")
      .connection("keep-alive")
      .userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:26.0) Gecko/20100101 Firefox/26.0")
    
      // Login
      .exec(
        http("getLogin")
          .get(casUrl)
          .check(css("input[name='lt']", "value").saveAs("lt")))
      .exec(
        http("postLogin")
          .post(casUrl)
          .headers(headers_6)
          .param("username", "abc")
          .param("password", "abcpwd")
          .param("lt", "${lt}")
          .param("execution", "e1s1")
          .param("_eventId", "submit")
          .param("submit", "LOGIN"))
    

    我冒昧地删除了三引号,这在本用例中不是必需的。

    【讨论】:

    • 嗨 notdrft,我还需要关联视图状态。你能告诉我吗?
    • 我不知道那是什么,你真的应该花点时间阅读维基:github.com/excilys/gatling/wiki/Checks。之后我很乐意以任何方式帮助你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 2013-12-07
    • 2020-08-19
    相关资源
    最近更新 更多