【问题标题】:In Selenium IDE, how to get the value of the base url在 Selenium IDE 中,如何获取基本 url 的值
【发布时间】:2011-01-13 07:14:30
【问题描述】:

是否可以从 Selenium 脚本(Selenium IDE 中的纯 HTML 保存脚本)中检索 base url 的值?

我正在尝试使用assertLocation 验证当前网址。但是assertLocation 返回绝对网址。我想将当前 url 与 相对 url 进行比较,而不必在 url 的开头使用 *

我想访问基本字符串,因为我希望能够在不同的站点(各种开发站点 + 生产站点)上运行测试,但是如果我使用 * 我无法检查根页面(*/ 对每个以 / 结尾的页面都是 true...)

这是我目前所做的:

|断言位置 | */一些页面 | |

这是我想做的:

|断言位置 |基本网址 + “/一些页面” | |

注意:是否有可能:

  1. 在目标中使用变量;
  2. 连接变量和字符串?

【问题讨论】:

    标签: testing selenium automated-tests selenium-ide


    【解决方案1】:

    试试这个

    storeEval|window.document.domain|host
    assertLocation|http://${host}/some-page|
    

    【讨论】:

    • 谢谢,它工作得很好!由于生产站点使用端口 80,而开发站点使用自定义端口,因此我使用了以下内容:storeEval | window.document.domain |主机 storeEval |窗口.location.port |端口断言位置 | regexp:http://${host}(|:${port})/some-page$ (抱歉格式错误)
    • @Emilien:您可以使用反引号 (`) 来表示 cmets 中的代码。见this help page
    • 很棒的提示!我用它来强制 selenium IDE 使用 relative URL,而不是它总是尝试使用的 faux-relative-actually-absolute URL。
    • 我使用window.location.origin 将协议、基本 URL 和端口合二为一:)
    【解决方案2】:

    您还可以打开基本页面并使用 storeLocation 将当前位置放入变量中:

    |open|/||
    |storeLocation|host||
    |assertLocation|${host}somepage.html
    

    奖励:这是我找出相应 SSL url 的方法

    |storeEval|window.document.location.toString().replace(new RegExp("^http://([^:]+):80"), "https://$1:40");|hostSSL|
    

    【讨论】:

    • 您可以通过从 storedVars['host'] 而不是 window.document.location 开始来简化上面的 storeEval ...
    【解决方案3】:

    上述解决方案导致我的开发环境位于非标准端口上。此解决方案也可能对 https 有所帮助。如果您真的想要基本网址:

    <tr>
    <td>storeEval</td>
    <td>selenium.browserbot.baseUrl</td>
    <td>baseurl</td>
    </tr>
    <tr>
    <td>echo</td>
    <td>${baseurl}</td>
    <td></td>
    </tr>
    

    【讨论】:

      【解决方案4】:

      使用 Selenium IDE,可以在不存储 $(host) 值的情况下做到这一点。

      Command: open
      Target: */login
      Value:
      

      Selenium Core [Source] 中提供了这个 sn-p 字符串匹配模式。

      【讨论】:

        【解决方案5】:
        <tr>
        <td>storeLocation</td>
        <td>url</td>
        <td></td>
        </tr>
        <tr>
        <td>echo</td>
        <td>${url}</td>
        <td></td>
        </tr>
        

        【讨论】:

          【解决方案6】:

          您可以使用 Selenium 驱动程序将当前 Url 获取为 String,然后将对象转换为 URL 对象。完成此操作后,Java URL 类有一些方法可用于获取部分 URL。

          String url = driver.getCurrentUrl();
          String domain = getDomainName( url );
          public static String getDomainName( String url ) throws URISyntaxException {
              URI uri = new URI(url);
              String domain = uri.getHost();
              return domain.startsWith("www.") ? domain.substring(4) : domain;
          }
          

          【讨论】:

            【解决方案7】:

            在努力使用 verifyLocation selenium IDE 命令(它只为您提供要验证的主机名,并且将包括在运行时可能广泛随机的查询字符串的一部分)之后,验证当前 url 和路径的方法是最灵活的(到目前为止)是使用“verifyEval”命令并使用javascript解析出我要测试的url部分:

            verifyEval | window.location.pathname=="/my/stuff" | true
            

            您可以放置​​任何 javascript 支持的位置属性并对其进行测试(请参阅https://gist.github.com/jlong/2428561)...

            Command: verifyEval
            Target: window.location.pathname=="/my/stuff"
            Value: true
            

            或者例如:

            Command: verifyEval
            Target: window.location.hostname=="example.com"
            Value: true
            

            或者随意组合

            Command:verifyEval
            Target: window.location.protocol+'//'+window.location.hostname+window.location.pathname=='http://example.com/my/stuff'
            Value:true
            

            【讨论】:

              猜你喜欢
              • 2020-02-22
              • 1970-01-01
              • 2013-04-19
              • 1970-01-01
              • 2014-02-10
              • 1970-01-01
              • 1970-01-01
              • 2011-11-22
              • 1970-01-01
              相关资源
              最近更新 更多