2011-06-03 20 views
4

是否有一種簡單的方法可以告訴Selenium IDE:任何導致http 500響應的操作都意味着測試失敗?Selenium IDE - 任何500錯誤都會失敗

我有75個頁面請求長的測試。有時候,我會在中間的某個地方發生崩潰並燒傷,但測試恢復爲綠色。

+0

請注意,每當可能發生500錯誤時,應將這些命令(來自我的答案)置於適當位置。通常在發生clickAndWait後,會發生這種情況。但它可能發生在許多情況下。 – MacGyver 2011-08-08 22:19:06

回答

0

製作一個名爲「user-extensions.js」的JavaScript文件,並將其添加到Selenium-IDE下的選項>選項下。如果您正在運行Selenium RC,請在jar命令中啓動服務器時將它傳遞給參數。應該有一個用戶擴展javascript文件屬性。

enter image description here

然後關閉並重新啓動硒IDE。用戶擴展文件在IDE啓動時被緩存。

將此代碼添加到您的Selenium user-extensions.js文件中,以生成名爲「AssertLocationPart」的自定義命令。正如你所知道的,「assertLocation」和「storeLocation」是標準命令。我試圖通過獲取自定義函數中的href來將額外的代碼行減少到storeLocation。我無法獲得doAssertValue命令的工作。我必須爲此發佈自己的問題。這就是爲什麼它被評論。現在,只需使用「this.doStore」。在您的自定義AssertLocationPart命令後添加一行到您的腳本。由於我們實際上並沒有在自定義函數/命令中進行斷言,我們應該將其稱爲「storeLocationPart」(函數將被命名爲「doStoreLocationPart」),而不是「assertLocationPart」(函數將被命名爲「doAssertLocationPart」),在第一個參數中。但是,如果你可以得到doAssert *的工作,請讓我知道。因爲我需要爲工作做同樣的事情,所以我會在另一天搞砸它。

Selenium.prototype.doAssertLocationPart = function(partName,assertTo) { 

    var uri = selenium.browserbot.getCurrentWindow().document.location.href; 
    //alert("URI = " + uri); 

    var partValue = parseUri(uri,partName); 
    //alert("Part '" + partName + "' = " + partValue); 

    //this.doAssertValue(partValue,assertTo); 
    this.doStore(partValue,"var_"+partName); 
}; 


// Slightly modified function based on author's original: 
// http://badassery.blogspot.com/2007/02/parseuri-split-urls-in-javascript.html 
// 
// parseUri JS v0.1, by Steven Levithan (http://badassery.blogspot.com) 
// Splits any well-formed URI into the following parts (all are optional): 
// 
// - source (since the exec() method returns backreference 0 [i.e., the entire match] as key 0, we might as well use it) 
// - protocol (scheme) 
// - authority (includes both the domain and port) 
//  - domain (part of the authority; can be an IP address) 
//  - port (part of the authority) 
// - path (includes both the directory path and filename) 
//  - directoryPath (part of the path; supports directories with periods, and without a trailing backslash) 
//  - fileName (part of the path) 
// - query (does not include the leading question mark) 
// - anchor (fragment) 
// 
function parseUri(sourceUri,partName){ 
    var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"]; 
    var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri); 
    var uri = {}; 

    for(var i = 0; i < 10; i++){ 
     uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : ""); 
     if (uriPartNames[i] == partName) { 
      return uri[uriPartNames[i]]; // line added by MacGyver 
     } 
    } 

    // Always end directoryPath with a trailing backslash if a path was present in the source URI 
    // Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key 
    if(uri.directoryPath.length > 0){ 
     uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/"); 
     if (partName == "directoryPath") { 
      return uri.directoryPath; // line added by MacGyver 
     } 
    } 

    return uri; 
} 

然後將此添加到您的web.config文件並確保customErrors已關閉。由於您有500錯誤,它會將用戶重定向到默認頁面。如果您希望在您的Selenium腳本中特定,可以爲500個HTTP狀態代碼添加自定義頁面。

<customErrors mode="On" defaultRedirect="/ErrorHandler.aspx"> 
    <error statusCode="401" redirect="/AccessDenied.aspx" /> 
    <error statusCode="403" redirect="/AccessDenied.aspx" /> 
    <error statusCode="404" redirect="/PageNotFound.aspx" /> 
</customErrors> 

這是你的命令看起來就像在IDE:

enter image description here

運行腳本之前,請確保你這個頁面(或類似的東西)上:

https://localhost/ErrorHandler.aspx?aspxerrorpath=/path/pathyouweretryingtoviewinwebapp.aspx 

日誌顯示臨時它通過了!

+0

我可以做assertnotlocation glob:*錯誤嗎? – 2011-08-08 22:19:37

+0

無論如何,你可能仍然需要解析URL,即使這樣也沒有得到整個「Location」/ URL/href? – MacGyver 2011-08-08 22:25:49

+0

我剛剛測試過,它存儲了包括查詢字符串參數在內的整個URL,所以你不得不解析它......儘管我認爲它們有一個模式參數。瞭解模式參數如何工作會很有趣。 – MacGyver 2011-08-08 22:29:20

0

從我以前的答覆使用錯誤處理程序:

Command: assertLocation 
Target: regexp:^(https://localhost/ErrorHandler.aspx).*$ 

或(根據您的評論),它的倒數,如果您沒有錯誤處理開啓,使用AssertNotLocation。這可能需要編寫腳本的人做更多的工作。你必須跟蹤所有頁面。

更多關於模式匹配:

http://seleniumhq.org/docs/02_selenium_ide.html#matching-text-patterns

http://www.codediesel.com/testing/selenium-ide-pattern-matching/

3

考慮看看硒api.js,我看到存在的簽名參數ignoreResponseCodedoOpen selenium-api.js中的方法:

Selenium.prototype.doOpen = function(url, ignoreResponseCode) { 

此參數用於由browserbot對象:

if (!((self.xhrResponseCode >= 200 && self.xhrResponseCode <= 399) || self.xhrResponseCode == 0)) { 
    // TODO: for IE status like: 12002, 12007, ... provide corresponding statusText messages also. 
    LOG.error("XHR failed with message " + self.xhrStatusText); 
    e = "XHR ERROR: URL = " + self.xhrOpenLocation + " Response_Code = " + self.xhrResponseCode + " Error_Message = " + self.xhrStatusText; 
    self.abortXhr = false; 
    self.isXhrSent = false; 
    self.isXhrDone = false; 
    self.xhrResponseCode = null; 
    self.xhrStatusText = null; 
    throw new SeleniumError(e); 
} 

我試圖調用從硒IDE open函數與值=假並且這導致錯誤(測試失敗)。

我的PHP測試頁面:

<?php 
    header('HTTP/1.1 500 Simulated 500 error'); 
?> 

,這導致: enter image description here

對於我來說,這解決了檢查HTTP響應狀態的問題。

相關問題