21

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

我想要做的是使用assertLocation. 但assertLocation返回绝对 url。我想将当前 url 与相对 url进行比较,而不必*在 url 的开头使用。

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

这是我目前所做的:

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

这就是我想做的:

|断言位置 | baseURL + "/some-page" | |

注意:是否甚至可以:

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

7 回答 7

18

试试这个

storeEval|window.document.domain|host
assertLocation|http://${host}/some-page|
于 2010-02-04T22:39:19.050 回答
6

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

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

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

|storeEval|window.document.location.toString().replace(new RegExp("^http://([^:]+):80"), "https://$1:40");|hostSSL|
于 2010-02-26T13:00:14.560 回答
2

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

<tr>
<td>storeEval</td>
<td>selenium.browserbot.baseUrl</td>
<td>baseurl</td>
</tr>
<tr>
<td>echo</td>
<td>${baseurl}</td>
<td></td>
</tr>
于 2014-02-19T15:52:46.807 回答
1

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

Command: open
Target: */login
Value:

这个片段字符串匹配模式在 Selenium Core [Source]中可用。

于 2015-11-11T18:39:00.537 回答
0
<tr>
<td>storeLocation</td>
<td>url</td>
<td></td>
</tr>
<tr>
<td>echo</td>
<td>${url}</td>
<td></td>
</tr>
于 2012-08-31T11:52:23.843 回答
0

您可以使用 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;
}
于 2014-02-19T16:06:25.760 回答
0

在努力使用 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
于 2016-08-31T22:52:49.200 回答