2010-02-04 22 views
21

是否可以從Selenium腳本(Selenium IDE中的純HTML保存腳本)中檢索基址的值在Selenium IDE中,如何獲取基址的值

我想要做的是使用assertLocation驗證當前的網址。但assertLocation返回絕對網址。我想將當前網址與相關網址進行比較,而不必在網址開頭使用*

我想訪問基本字符串,因爲我想能夠在不同的網站(各種開發網站+生產網站)上運行測試,但如果我使用*,我無法檢查根目錄頁面(*/將是與一個/結束每一頁真...)

這是我目前做的:

| assertLocation | */some-page | |

這是我想做什麼:

| assertLocation | baseURL + 「/ some-page」| |

注:是它甚至有可能:

  1. 使用在靶的變量;
  2. 連接一個變量和一個字符串?

回答

18

試試這個

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

謝謝,它的工作就好了! 由於生產現場使用端口80,但dev的站點使用自定義端口,我用了以下內容: storeEval | window.document.domain |主機 storeEval | window.location.port |端口 assertLocation |正則表達式:http:// $ {host}(|:$ {port})/ some-page $ (抱歉格式不對) – Emilien 2010-02-05 19:32:47

+1

@Emilien:您可以使用反引號(')來表示代碼。請參閱[本幫助頁面](http://stackoverflow.com/editing-help#comment-formatting)。 – 2011-11-04 19:13:04

+0

感謝羅伊的提示 – Emilien 2011-11-20 14:24:05

6

你也可以打開你的基本頁面,並使用storeLocation把當前位置到一個變量:

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

獎勵:這裏就是我想通了相應的SSL網址

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

您可以通過從storedVars ['host']而不是window.document.location開始簡化您的storeEval ... – 2011-08-03 21:43:14

0
<tr> 
<td>storeLocation</td> 
<td>url</td> 
<td></td> 
</tr> 
<tr> 
<td>echo</td> 
<td>${url}</td> 
<td></td> 
</tr> 
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> 
0

您可以用Selenium司機趕下車當前URL爲字符串,然後將對象轉換爲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; 
} 
1

使用Selenium IDE,無需存儲$(主機)值即可使用。

Command: open 
Target: */login 
Value: 

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

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 
相關問題