zs2020
zs2020

Reputation: 54543

How to set a global Base URL for every test case of one test suite in Selenium IDE?

How to set a global Base URL for every test case of one test suite in Selenium IDE so that I can switch to different environment easily?

Upvotes: 32

Views: 29655

Answers (8)

Rene Wilms
Rene Wilms

Reputation: 1

I created a user-extension command 'doBaseURL' that manipulates the Selenium IDE field 'Base URL'. In every test case of a test suite I added this command | baseURL | | |. Below the surface it sets a stored variable named baseURL. It is possible to give the stored variable 'baseURL' a value from a test case command prior to the first 'baseURL' command. This method overwrites the <link href="http://server-name/" rel="selenium.base"/> setting of each test case file. For me this works fine as now the Selenium IDE reflects the Base URL that the test case(s) will use (or was using when an error occured).

The java script is:

Selenium.prototype.doBaseURL = function(strTarget, strValue) {
/**
  * Enables a more predictive Base URL when running a test suite with multiple test cases. 
  * On first usage it wil fill a stored variable 'baseURL' either with the given strTarget,
  * or when not given with the Base URL as shown on the Selenium IDE UI. The Selenium IDE UI field
  * Base URL is updated to reflect the (new) base URL. 
  * In each subsequent test case with a baseURL command the stored variable 'baseURL' will determine
  * the base.
  *
  * <p> Best to use as first command in each test case and only in the first test case a strTarget
  * argument, in that way the test cases of a test suite will no longer depend on their 'hidden'
  * '<link rel="selenium.base" href="http://www.something.com/" />
  * construction, that makes test cases effectively having an absolute base URL.
  * </p>
  *
  * @param strTarget a Base URL value to be used in the test case (e.g. https://www.google.com/)
  */

// pushes the given strTarget into the selenium.browserbot.baseUrl
// and makes the new Base URL value visible on the Selenium IDE UI.
// A subsequent open command will use this new Base URL value

LOG.debug("begin: doBaseURL | " + strTarget + " | " + strValue + " |");

// note:    window.location.href = strTarget;      causes the browser to replace Selenium-IDE UI with the given url
// used that knowledge to manipulate the Selenium IDE UI as if a manual Base URL change was done.
LOG.debug("window.location.href= [" + window.location.href + "]");
LOG.debug("window.editor.app.getBaseURL() gives [" + window.editor.app.getBaseURL() + "]");

if (strTarget && (strTarget.substring(0,1)!="$")) { // only when strTaget has a value

strValue = "Base URL changed from [" + selenium.browserbot.baseUrl.toString() + "] into [" + strTarget + "]"; 
selenium.browserbot.baseUrl = strTarget.toString();

// set the visible Base URL value on the Selenium IDE UI to the new value
window.editor.app.setBaseURL(strTarget);
LOG.debug("updated into: " + window.editor.app.getBaseURL());

// remember this value for future calls
storedVars["baseURL"] = window.editor.app.getBaseURL();
LOG.debug("storedVars[\"baseURL\"] set to [" + window.editor.app.getBaseURL() + "]");

} else {
// no value =>  take storeVars["baseURL"] when set;
//      otherwise take Selenium IDE Base URL field.
if ((storedVars["baseURL"] != undefined) && (storedVars["baseURL"] != "")) {
    strValue = "Base URL changed from [" + selenium.browserbot.baseUrl.toString() + "] into [" + storedVars["baseURL"] + "]"; 
    selenium.browserbot.baseUrl = storedVars["baseURL"];

    // set the visible Base URL value on the Selenium IDE UI to the new value
    window.editor.app.setBaseURL(storedVars["baseURL"]);
    LOG.debug("updated from storedVars[\"baseURL\"]");
} else {
    strValue = "Base URL changed from [" + selenium.browserbot.baseUrl.toString() + "] into [" + window.editor.app.getBaseURL() + "]"; 
        selenium.browserbot.baseUrl = window.editor.app.getBaseURL();

    // remember this value for future calls
    storedVars["baseURL"] = window.editor.app.getBaseURL();
    LOG.debug("storedVars[\"baseURL\"] set to [" + window.editor.app.getBaseURL() + "]");
}
}
LOG.info(strValue);
LOG.debug("end: doBaseURL | " + strTarget + " | " + strValue + " |");
}

Installing a user-extension script is explained in the "Using User-Extensions With Selenium-IDE" section SeleniumHQ documentation: http://www.seleniumhq.org/docs/08_user_extensions.jsp

Upvotes: 0

Walty Yeung
Walty Yeung

Reputation: 3572

I tried the accepted answer, it does work for selenium IDE, but it does not work when running stand-alone server.

finally, I put the value inside the user-extensions.js , and use storeEval to get the value back. Then the same value could be retrieved inside different cases.

EDIT:

In the user-extensions.js, add the following

var selenium_base_url = "http://example.com"

In the HTLM suite, add the following

<tr>
    <td>storeEval</td>
    <td>selenium_base_url</td>
    <td>case_base_url</td>
</tr>
<tr>
    <td>open</td>
    <td>${case_base_url}/case_path?a=1&b=2</td>
    <td></td>
</tr>

This may look odd at the first sight, but actually user-extension.js could be modified on the fly right before the stand-alone server execution, so you may put a different base url using a script in the real QA scenario.

Upvotes: 1

abn
abn

Reputation: 1373

If you're fine double clicking multiple times,

  1. Double click the top one, set it to the URL you want.
  2. Now double click the top, middle, bottom.
  3. Now double click the bottom, middle, top.

Upvotes: 0

okigan
okigan

Reputation: 1629

Hmm, I think there is a simpler solution:

  • In the first test (of the test suite) adjust <link rel="selenium.base" href="http://google.com" /> as needed

  • In the following tests remove (using text editor or similar): <link rel="selenium.base" ... />

Upvotes: 0

Luke
Luke

Reputation: 438

I found it exceedingly frustrating that I couldn't just have a blank Base URL and have it use whatever page I already had open as the start of the test. What is the point of being able to record relative URLs when it forces you to hardcode a base url that is prefixed to every relative url (thus turning them into absoulte urls)??

Extending on the other answers and from this question: In Selenium IDE, how to get the value of the base url

You can store the current domain in a variable and then use that instead of a hard coded one. This lets you login to the server you want to run the tests on, hit play and it will run the tests without all this baseURL nonsense redirecting you to the wrong server.

Just use these commands at the start of the first script in your suite: store current server

And keep using the http://${host}/ syntax for every truly relative URL you need in your script.

Upvotes: 9

arwenvh
arwenvh

Reputation: 461

If you have a lot of test cases in a single suite, it's a pain to change the Base URL for each. Instead, create a separate case for each Base URL you need to switch between. For example, I have store https://testing.site.com/ as myEnvironment saved as test case SetEnvTesting. I create another case for my production site, SetEnvProduction.

Then, insert the ${myEnvironment} at the beginning of each relative URL in your test cases. For example, open ${myEnvironment}/login.aspx. (This might be a pain if you've got a lot of tests already. I suggest just adding it from now on.) Then, simply put the set-environment test case at the beginning of your test suite. To switch your entire suite to another Base URL, simply put a different set-environment case at the start. Another nice thing is that you can switch environments in the middle of the suite by putting a different set-environment case in the middle.

Edit: Example of using a SetEnvironment test case.

The SetEnvironment case: enter image description here

An example of a following test case. enter image description here

Notice

  1. how the CurrentEnvironment variable is used. You can do this for every case in the suite. Additionally, you can make every separate test suite use this same SetEnvironment case, so they all switch together.

  2. that the Base Url becomes irrelevant. You're overriding it, essentially.

I hope that's helpful!

Upvotes: 33

Bob Dalgleish
Bob Dalgleish

Reputation: 8227

The test case HTML file is responsible for setting the base URL. From the Selenium IDE, you can override the base URL for a test case. A test suite, on the other hand, is only a bag for your test cases.

Remove the base URL setting from each of the test case files referenced from your test suite. Then set the base URL by hand for the test suite and run the suite.

The line in question is in the head of each test case file:

<link href="http://server-name/" rel="selenium.base"/>

If it is missing, then the base URL overrides it automatically.

Upvotes: 12

zs2020
zs2020

Reputation: 54543

I just created a separate test and put it at the top of all tests. Store the base url to an variable and now I am able to access it in other test cases by ${variable_name}.

Upvotes: 6

Related Questions