0

這是加特林錄音機腳本。如何使用相關性在加特林中獲取票據

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 Response的name="lt" value="(.*?)"的票證。

請幫助我如何提取在加特林的機票。

並且你能告訴我如何關聯視圖狀態。

感謝&問候

納拉辛哈

回答

3

首先,你需要做的第一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}") 

的支架也是必需的:它注意到加特林嘗試,並在會議上與搜索鍵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")) 

我冒昧地去除三重引號,這是沒有必要在這個用例。

+0

謝謝。我會盡力讓你知道。 – user2571340

+0

嗨notdrft,我需要關聯視圖狀態也。你能告訴我嗎? – user2571340

+0

我不知道那是什麼,你應該花點時間閱讀wiki:https://github.com/excilys/gatling/wiki/Checks。我很樂意隨後以任何方式幫助你。 – notdryft