开发者

In Selenium IDE, how to get the value of the base url

Is it possible to retrieve the value of the base url from inside a Selenium script (a plain HTML-saved script from Selenium IDE)?

What I'm trying to do is verify the current url using assertLocation. But assertLocation returns the absolute url. I would like to compare the current url to a relative url without having to use an * at the start of the url.

I'd like to have access to the base string because I want to be able to run the tests on different sites (various dev sites + production site), 开发者_StackOverflow社区but if I use the * I can not check for the root page (*/ would be true for each page that ends with a /...)

This is what I currently do:

|assertLocation | */some-page | |

This is what I'd like to do:

|assertLocation | baseURL + "/some-page" | |

Note: is it even possible to:

  1. use a variable in the target;
  2. concatenate a variable and a string?


Try this

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


You can also open your base page and use storeLocation to put the current location into a variable:

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

Bonus: here's how I figured out the corresponding SSL url

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


The above solutions cause issues with my development environment being on a non-standard port. This solution may also help with https. If you truly want the base url:

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


Using Selenium IDE, this is do able without storing the $(host) value.

Command: open
Target: */login
Value:

This snippet string-match patterns available in the Selenium Core [Source].


<tr>
<td>storeLocation</td>
<td>url</td>
<td></td>
</tr>
<tr>
<td>echo</td>
<td>${url}</td>
<td></td>
</tr>


You can use Selenium driver to get the current Url as a String, then convert the object into a URL object. Once you have done that, the Java URL class has some methods that you can use to get parts of the 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;
}


After struggling with using the verifyLocation selenium IDE command (which only gives you the host name to verify, and will include parts of the query string which may be widely random at runtime) the way to verify the current url and path which is the most flexible (so far) is to use the "verifyEval" command and use javascript to parse out the portion of the url I want to test:

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

You can put any of the location properties that javascript supports and test those (see https://gist.github.com/jlong/2428561)...

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

OR for example:

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

Or feel free to make combinations

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜