1

As part of the company product, User has to add a bookmarklet to the browser (a bookmark button that executes a JavaScript code instead of opening a URL).The source code of the bookmarklet is as follows:

javascript: (function () {
            var d = document;
            var script = d.createElement('scr'+'ipt');
            script.src = 'https://<Company_URL>/Bookmarklet/ScriptContent?scriptFile=bm-script.js&rnd=' + Math.floor(Math.random() * 99999);
            if (typeof script.onload === 'object') {
                script.onload = function () {
                     doGeneralScript();
                 };
            }
            else {
                script.onreadystatechange = function() {
                    if (this.readyState == 'complete' || this.readyState == 'loaded') {
                        doGeneralScript();
                    }
                };
            }
            d.getElementsByTagName('head')[0].appendChild(script);
            }());

The code basically opens a frame popup with some controls on it and the next step for the User is to click on one of the controls. Meantime, the Selenium IDE is unable to make a click on the button located outside the common area, so there are two ways of executing the script associated with the bookmarklet with the Selenium IDE:

  1. Create a one-liner script and make "open" statement. It works perfectly for the Firefox browser version 11.0 but causes non-fatal failure on Firefox 23.0.When on Firefox 23.0, the Selenium script makes pause and waits for the window to appear ignoring the frame popup. Eventually scripts resumes after throwing error.
  2. Write a custom user extension that will execute the script upon request

I tried to add the following three extensions to the Selenium Core extensions (user-extensions.js)

Selenium.prototype.doMbProduction = function()
    {
      var d = document;
      var script = d.createElement('scr' + 'ipt');
      script.src = 'https://<Company_URL>/Bookmarklet/ScriptContent?scriptFile=bm-script.js&rnd=' + Math.floor(Math.random() * 99999);
      if (typeof script.onload === 'object') {
          script.onload = this.doRunGeneralScript();
      } else {
          script.onreadystatechange = this.doPerformLoad();
      }
      d.getElementByTagName('head')[0].appendChild(script);
    }

    Selenium.prototype.doRunGeneralScript = function(){
      script.doGeneralScript();
    }

    Selenium.prototype.doPerformLoad = function()
    {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
    this.doRunGeneralScript();
      }
    }

But that won't work. Upon executing command mbProduction, the script throws fatal error

[error] Unexpected Exception: ReferenceError: script is not defined. fileName -> chrome://selenium-ide/content/tools.js -> file:///C:/Users/3pf/Downloads/sideflow1.js?1376849717650, lineNumber -> 124

the line 124 in my cutom user-extensions is script.doGeneralScript();

Can anybody help me find out what I am doing wrong, please? Any helpful suggestions are greatly appreciated.

user2644066
  • 11
  • 1
  • 3

2 Answers2

1

I ran into the same problem. Below what did it for me to fix it:

  1. Create a file with file name 'user-extensions.js'.
  2. Store this file within a public directory of your site.
  3. Load this file in 'Options' > 'Options' > 'Selenium Core extensions (user-extensions.js)'
  4. Restart Selenium

You might still get a popup with an error that the script can not be found, but this is not the case - it should work.

When you update your user-extensions.js file, you have to restart Selenium. Check the checkbox 'Activate developer tools' to be able to press the 'Reload' button. This will make it possible to reload your script without restarting Selenium.

1

I hope my response is not too late.

First, I see two problems off the bat.

1) The error message looks very precise. If you look at your code

Selenium.prototype.doRunGeneralScript = function(){
  script.doGeneralScript();
}
  • the variable 'script' exists inside the doRunGenerateScript method without being defined. That will not work

2) You will not have access directly to document inside the Selenuim object. To get a hold of the 'document' object, use:

var document = this.browserbot.getDocument();

I hope that helps :-)

mainas
  • 754
  • 1
  • 6
  • 15