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

27,570

Solution 1

Try this

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

Solution 2

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|

Solution 3

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>

Solution 4

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].

Share:
27,570
Emilien
Author by

Emilien

Updated on July 09, 2022

Comments

  • Emilien
    Emilien almost 2 years

    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), 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?
  • Emilien
    Emilien over 14 years
    Thanks, it worked just fine! As the production site uses port 80 but the dev sites use custom ports, I used the following: storeEval | window.document.domain | host storeEval | window.location.port | port assertLocation | regexp:http://${host}(|:${port})/some-page$ (sorry for the bad formatting)
  • Peter Centgraf
    Peter Centgraf almost 13 years
    You could simplify your storeEval above by starting from storedVars['host'] instead of window.document.location ...
  • Roy Tinker
    Roy Tinker over 12 years
    @Emilien: you can use backtick (`) to denote code in comments. See this help page.
  • Luke
    Luke about 11 years
    Great tip! I used this to force selenium IDE to use relative URLs instead of the faux-relative-actually-absolute URLs it always tries to use.
  • AsGoodAsItGets
    AsGoodAsItGets over 7 years
    I used window.location.origin to get the protocol, base URL and port all in one :)