Selenium IDE - 任何500错误总是失败

时间:2011-06-03 14:52:48

标签: selenium-ide

是否有一种简单的方法可以告诉Selenium IDE任何导致http 500响应的操作意味着测试失败了?

我的测试时间长达75页。有时,我遇到了崩溃并在中间的某个地方燃烧,但测试结果是绿色的。

3 个答案:

答案 0 :(得分:3)

看一下selenium-api.js,我看到selenium-api.js中 doOpen 方法的签名中有一个参数 ignoreResponseCode :< / p>

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);
}

我尝试使用 value = false 从selenium IDE调用open函数,这会导致错误(测试失败)。

我的PHP测试页面是:

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

这会导致: enter image description here

对我来说,这解决了检查HTTP响应状态的问题。

答案 1 :(得分:0)

创建一个名为“user-extensions.js”的JavaScript文件,并将其添加到Selenium-IDE下的Options&gt;选项。如果您正在运行Selenium RC,请在jar命令中启动服务器时将其传递给参数。应该有一个用户扩展javascript文件属性。

enter image description here

然后关闭并重新启动Selenium-IDE。 IDE启动时会缓存User-Extensions文件。

将此代码添加到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

日志显示已通过!

答案 2 :(得分: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/