Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Tuesday, January 13, 2009

Selenium IDE include command for template

There is a useful extension for Selenium-Core: include, which can add the content of another test to the current test. Then it is possible to reuse test scripts for Selenese. See OpenQA wiki: http://wiki.openqa.org/display/SEL/include

However, it supported Selenium-Core only. Many Selenium-IDE users also want to use this feature. So I've ported the existing include extension to work in Selenium IDE. I added it to wiki: http://wiki.openqa.org/pages/viewpageattachments.action?pageId=283

  • target receives the page address (relative path from base url or use absolute path)
  • text receives vars names and their values for this test as a comma separated list of var_name=value


  • include step run in Selenium-IDE looks like:

    Please set user-extension.js path for SeleniumIDE->options->Selenium Core Extensions

    Friday, January 2, 2009

    Selenium1.0beta for Firefox3 is ready

    We have fixed most of blocking bugs on Firefox3. It's time to upgrade our Selenium lab machines to Firefox3.0.5. Mozilla has announced Firefox2.0.0.20 would be the last version and stopped Firefox2 maintenance & supports. The Firefox3 market share is increasing to 17% at the end of 2008.


    The interesting new issue on Firefox3 is SSL cert exception page blocking Selenium automation. On Firefox2, we invoke RMD.xpi XPCOM in Selenium to intercept all bad cert exception for our self-signed certificates. However, it doesn't work on Firefox3 now that RMD.xpi's author said it only supported Firefox2. Firefox3 introduced a new SSL certificate exception rule. We must find a new workaround.

    Block step:


    Try below selenium ssl test case:
     
    <tr>
     <td>open</td>
     <td>http://kuix.de/mozilla/certwarndiscussion/proposal20061016/</td>
     <td></td>
    </tr>
    <tr>
     <td>open</td>
     <td>https://www.cacert.org</td>
     <td></td>
    </tr>
    <tr>
     <td>open</td>
     <td>https://www.kuix.de</td>
     <td></td>
    </tr>
    <tr>
     <td>open</td>
     <td>https://mur.at</td>
     <td></td>
    </tr>
    <tr>
     <td>open</td>
     <td>https://kuix.de:9443</td>
     <td></td>
    </tr>
    <tr>
     <td>open</td>
     <td>https://www.kuix.de:9443</td>
     <td></td>
    </tr>
    
    I plan to create a new project 'Remember Certificate Exception'(RCE) on AMO today. The RCE.xpi's purpose is auto-complete Firefox3 certificate exception override.

    Sunday, December 28, 2008

    Can Selenium detect if the page has JavaScript errors?

    We usually have common requirement for Selenium: Check JS error in step.

    Use case like:
    1. beginJsErrorChecker
    2. open | http://containJSError.jsp
    3. open | http://noissueJS.jsp
    4. endJsErrorChecker
    It can report url2 has jserror in Selenium:
    [error] JsErrorChecker:[JavaScript Error: "foo is not defined" {file: "http://containJSError.jsp" line: 9}]
    
    It's hard to provide a generic solution for all browsers but my team implements above scenario for Selenium IDE only by Firefox chrome Error Console XPCOM APIs.

    user-extensions.js like:
     
    if (browserVersion.isChrome) {
      var theConsoleListener = {
         stepIdx:0, //beginJsErrorChecker command index
         observe:function( aMessage ){//async!
            dump("Log : " + aMessage.message);
            //Error, Warning, Message too noise. We only report "[JavaScript Error:..."
            if(aMessage.message != null && aMessage.message.indexOf("[JavaScript Error:") == 0){ 
              LOG.error("JsErrorChecker:" + aMessage.message);
                      
              //throw new SeleniumError("Catch JS Error: " + aMessage.message);
              //can't throw exception here: pollution to target JSError
              //so we manually markFailed
              
              //IDE only
              var olddebugIndex = testCase.debugContext.debugIndex;
              testCase.debugContext.debugIndex = this.stepIdx;//set actual index
                      
              testCase.debugContext.failed = true;
              testCase.debugContext.currentCommand().result = 'failed';
              editor.view.rowUpdated(testCase.debugContext.debugIndex);
                      
              testCase.debugContext.debugIndex = olddebugIndex;//recover
            } 
       },
       QueryInterface: function (iid) {
            if (!iid.equals(Components.interfaces.nsIConsoleListener) &&
                  !iid.equals(Components.interfaces.nsISupports)) {
                throw Components.results.NS_ERROR_NO_INTERFACE;
                }
            return this;
       }
      }; 
    }
     
    Selenium.prototype.doBeginJsErrorChecker = function(){
      try {
        if (browserVersion.isChrome) {// firefox 
              theConsoleListener.stepIdx=testCase.debugContext.debugIndex;//set current step idx for async call
              var aConsoleService = Components.classes["@mozilla.org/consoleservice;1"]
                              .getService(Components.interfaces.nsIConsoleService);
              aConsoleService.registerListener(theConsoleListener);
              aConsoleService.reset();
        }else{
          throw new SeleniumError("TODO: Non-FF browser...");
        }
        } catch (e) {
        throw new SeleniumError("Threw an exception: " + e.message);
        }
    };
    
    Selenium.prototype.doEndJsErrorChecker = function(){
      try {
        if (browserVersion.isChrome) {// firefox  
              var aConsoleService = Components.classes["@mozilla.org/consoleservice;1"]
                              .getService(Components.interfaces.nsIConsoleService);
              aConsoleService.unregisterListener(theConsoleListener);
              aConsoleService.reset();
        }else{
          throw new SeleniumError("TODO: Non-FF browser...");
        }
        } catch (e) {
        throw new SeleniumError("Threw an exception: " + e.message);
        }
    };
    
    I just add a Selenim listener to Firefox Error Console to make jserror checker work for Selenium-IDE.
    Because it is async mode to check JSError, we treat beginJsErrorChecker failed if any step contains JSError in begin...end block.
    We also filter all Warning and Message in Error Console, treat Error only as failed here.

    I pasted solution on http://clearspace.openqa.org/message/52135
    http://jira.openqa.org/browse/SEL-613

    Saturday, December 27, 2008

    Selenium1.0b buglist for Firefox 3 Chrome TestRunner

    There are a lot of bugs in Selenium1.0b to run on Firefox 3 with chrome TestRunner.html.

    I list them here:
    1. CSS/Resource issue: http://jira.openqa.org/browse/SIDE-222
    2. Permission Denied issue: http://jira.openqa.org/browse/SEL-612
    3. waitForPopUp issue: http://jira.openqa.org/browse/SIDE-228
    4. click link issue: http://jira.openqa.org/browse/SEL-614
    5. UI-Element not work issue: http://sejq.blogspot.com/2008/12/fix-ui-element-cant-run-bug-in-chrome.html

    Sunday, December 21, 2008

    Two Javascript synchronous sleep functions which won't freeze browser

    Javascript didn't provide Thread.sleep(ms) method like Java which is useful for synchronization.

    Most of time we can solve Javascript synchronization by use setTimeout or setInteval. However, using setTimeout requires you to split your function up into discrete processes, which is hard to do for some Javascript logic. What we really need is a free-standing function that could be added inline to any block of code to share the processor on the fly.

    I encounter a scenario in Selenium today: I want Javascript to sleep to wait for an async call. It's much harder than I thought to implement it. Think about below code:
    function sleep(delay) {
       var startTime = new Date();
       var endTime = null;
       do {
           endTime = new Date();
       } while ((endTime - startTime);
    } 
    
    It looks fine but if you try it, you will find CPU is very busy executing the while loop and freeze browser. This is not what we wanted.

    I got 2 workaround methods. The keypoint is JS NOT freeze CPU/browser during sleep:
    1)javascript sleep 1: use XMLHttpRequest to do sync call for sleep.jsp
    function wbsleep(ms) { 
      var   startDate = new Date();   
      while((new Date()-startDate) < ms){   
          var xmlHttpReq; 
          try {
             // for IE/ActiveX
             if(window.ActiveXObject) {
                 try {
                     xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
                 }
                 catch(e) {
                     xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                 }
             }
             // Native XMLHttp
             else if(window.XMLHttpRequest) {
                 xmlHttpReq = new XMLHttpRequest();
             } 
                    tempurl = "http://localhost/sleeputil.jsp?sleepms="+ms;
             xmlHttpReq.open("GET", tempurl, false); // synchron mode  
             xmlHttpReq.send(null);
          }
          catch(e) {  
          } 
      }
    };
    The sleeputil.jsp is very simple, get request parameter sleepms and then do a Java Thread.sleep(sleepms) in doGet. This method is suitable for all browsers but need you provide a server side sleeputil.jsp.

    2)javascript sleep 2: XPCOM on Firefox chrome only
    /**
     * Netscape compatible WaitForDelay function.
     * You can use it as an alternative to Thread.Sleep() in any major programming language
     * that support it while JavaScript it self doesn't have any built-in function to do such a thing.
     * parameters:
     *  (Number) delay in millisecond
    */
    function nsWaitForDelay(delay) {
      try{
        /**
        * Just uncomment this code if you're building an extention for Firefox.
        * Since FF3, we'll have to ask for user permission to execute XPCOM objects.
        */
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    
        // Get the current thread.
        var thread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).currentThread;
    
        // Create an inner property to be used later as a notifier.
        this.delayed = true;
    
        /* Call JavaScript setTimeout function
         * to execute this.delayed = false
         * after it finish.
         */
        setTimeout("this.delayed = false;", delay);
    
        /**
         * Keep looping until this.delayed = false
        */
        while (this.delayed) {
            /**
             * This code will not freeze your browser as it's documented in here:
             * https://developer.mozilla.org/en/Code_snippets/Threads#Waiting_for_a_background_task_to_complete
            */
            thread.processNextEvent(true);
        }
      }catch(e) {  
      } 
    }
    This is especially suitable for Selenium CORE*ffchrome.

    Saturday, December 20, 2008

    Automate HttpFox(HttpWatch) test case for http traffic manipulation in Selenium

    Selenium can verify and store values by DOM on current page easily. However, it's hard for Selenium to do manipulation on http traffic like POST Data, http headers, Query String and Content sniffer.

    I know many QA did http traffic testing manually by 2 famous software:
    1) HTTPWatch: commercial. support both IE and Foxfox.
    2) HTTPFox: free, opensource. a Firefox extension.

    One day, a guy came to my cube and raised a question: Can I stop my tons of manual functional testing with HttpFox, but use Selenium to automate them? Is it possible that we create a bundle of Selenium APIs to manuplate Http Traffic Sniffer operations?

    Yes, we should add this important feature to Selenium. I finished a beta version to support HttpFox APIs in Selenium:
    1) merge httpfox.xpi into Selenium-IDE.xpi
    2) leverage XPCOM in httpfoxservice.js HTTPFox provided
    3) create Selenium APIs and print http traffic info to Selenium LOG:
    startHttpFox4Sel
    stopHttpFox4Sel
    clearHttpFox4Sel
    verifyTextInHttpFoxURLs(Not)Present | textpattern
    verifyTextInHttpFoxContent(Not)Present | urlpattern | textpattern
    storeContentInHttpFoxByURL | urlpattern | varname
    verifyParamInHttpFoxQueryString(Not)Present | urlpattern | param1=value1,param2=value2 list
    storeParamInHttpFoxQueryString | param@urlpattern | varname
    verifyParamInHttpFoxPostData(Not)Present | urlpattern | param1=value1,param2=value2 list
    storeParamInHttpFoxPostData | param@urlpattern | varname
    ...

    Source draft:
    Selenium.prototype.doStartHttpFox4Sel = function() {
        try {
         if(browserVersion.isChrome){
          HttpFox.cmd_hf_clear();
             HttpFox.cmd_hf_startWatching(); 
            }
        } catch (e) {
         throw new SeleniumError("Threw an exception: " + e.message);
        }
    }; 
    
    Selenium.prototype.doStopHttpFox4Sel = function() {
        try {
            if(browserVersion.isChrome){ 
             HttpFox.cmd_hf_stopWatching();
             HttpFox.cmd_hf_clear();
            }
        } catch (e) {
         throw new SeleniumError("Threw an exception: " + e.message);
        }
    }; 
    
    Selenium.prototype.doClearHttpFox4Sel = function() {
        try {
            if(browserVersion.isChrome){  
             HttpFox.cmd_hf_clear();
            }
        } catch (e) {
         throw new SeleniumError("Threw an exception: " + e.message);
        }
    }; 
           
    Selenium.prototype.isTextInHttpFoxURLsPresent = function(pattern) {
       try {
         if(browserVersion.isChrome){  
       for (var i = 0; i < HttpFox.HttpFoxService.Requests.length; i++){ 
            var request = HttpFox.HttpFoxService.Requests[i];
            //1.Time:
            var time = formatTimeDifference(request.StartTimestamp, request.EndTimestamp) ;
            //2.Sent:
            var sent = "";
            if (request.IsSending){
              sent = humanizeSize(request.getBytesSent(), 6) + "/" + humanizeSize(request.getBytesSentTotal(), 6);
            }
            else {
             sent = humanizeSize(request.getBytesSentTotal(), 6); 
            } 
            //3.Received:     
             var received = "";
             if (request.IsSending){
              received = "*";
           }
           if (!request.IsFinished){
              // show loading body progress
             received = humanizeSize(request.getBytesLoaded(), 6) + "/" + humanizeSize(request.getBytesLoadedTotal(), 6);
           }else{
             received = humanizeSize(request.getBytesLoaded(), 6); 
           }
           if (request.IsFromCache || request.ResponseStatus == 304){
             received = "(" + received + ")";
           } 
            //4.Method:
           var method = request.RequestMethod; 
            //5.Result: 
            var result = "";
           if (request.IsAborted){
             result =  "(Aborted)";
           }else if (request.isError()){
              result = "(Error)";
           }else if (request.IsFromCache && (request.ResponseStatus != 304)) {
              result =  "(Cache)";
           }else if (!request.HasReceivedResponseHeaders && !request.IsFinal){
              result = "*";
           }else{
               result =  request.ResponseStatus;
           } 
            //6.Type:
                  var type = "";
                  if (request.hasErrorCode()){
                   if (request.ContentType){
                   type =  request.ContentType + " (" + request.Status.toString(16) + ")";//we omit nsResultErrors translate
                  }else{
                   type =  request.Status.toString(16);
                  }
                 }else if (!request.HasReceivedResponseHeaders && !request.IsFromCache && !request.IsFinal){
                   type =  "*";
                 }else if (request.isRedirect()){
                  if (request.ResponseHeaders && request.ResponseHeaders["Location"]){
                   type =  "Redirect to: " + request.ResponseHeaders["Location"]; 
                  }else{
                   type =  "Redirect (cached)";
                  }
                 }else{
                  type = request.ContentType;     
                 }  
           //7.URL
           var urlstring = request.Url ;
           //TODO: LOG.info(http sniffer info);
           //TODO: verify as you like by PatternMatcher
       } //end for
        }//end chrome
        } catch (e) {
         throw new SeleniumError("Threw an exception: " + e.message);
        }
    }; 
    

    Now, QA can automate HttpFox steps in Selenium. They don't need to do http traffic testing manually all days.

    Monday, December 15, 2008

    Selenese flowControl extensions - advanced support for include template

    As my previous post, Selenese flowControl extensions work well on IDE and Core now. However, when guys used another powerful extension 'include', they found flowControl not work in include template steps.

    I provide a quick workaround here. Please revise include extension javascript as below:
    Selenium.prototype.doInclude = function(locator, paramString) {
        LOG.debug(IncludeCommand.LOG_PREFIX + " Version " + IncludeCommand.VERSION);
        var includeCommand = new IncludeCommand();
        includeCommand.doInclude(locator, paramString);
      
         //Jerry Qian: add flowControl support for include template steps
         try{
           htmlTestRunner.currentTest.gotoLabels  = {};
           htmlTestRunner.currentTest.whileLabels = { ends: {}, whiles: {} };
           htmlTestRunner.currentTest.initialiseLabels();
         }catch(e){}
         //End flowControl
    };
    Include template steps can use flowControl commands now.

    Saturday, December 13, 2008

    Selenese flowControl extensions

    Usually, we don't need to do conditional logic in Selenese table. Selenium RC programming language is more suitable for complex logic. But sometimes we still want to add some conditional steps in Selenese, like if...else, while.

    Fortunately, there are already flow control extensions on openqa's wiki.
    http://wiki.openqa.org/display/SEL/flowControl

    It can meet most of flow control requirements for Selenese. We still recommend use RC to implement more complicated logic.

    Also some people work on both Core and IDE. They found this extensions not work on IDE. It's a CORE version.

    One guy contributed an IDE version: http://51elliot.blogspot.com/2008/02/selenium-ide-goto.html. IDE user can also use it.

    So I combined them to a unified flowControl extensions: support both IDE and CORE.

    The simplest code:
    var sel_locstr = window.location.href;  
    if(sel_locstr.indexOf("selenium-ide.xul") != -1){ //IDE mode      
       // paste IDE extension here
    }else{//Core mode
       // paste Core extension here
    } 
    Enjoy!

    Friday, December 12, 2008

    Firefox3 new security rule: Selenium no CSS on Chrome TestRunner

    Firefox 3 introduced more strict security rules than older version. Many Selenium weird issues on FF3 are related to it, while most of these issues didn't happen on FF2.

    Today I encountered one SeleniumIDE1.0b2 bug: [SIDE-222]Test execution is not possible in chrome mode in Firefox 3.

    FF3 Error Console reported:
    Security Error: Content at (local test suite html address like file:///c:/seltest/testsuite1.html) may not load or link to "chrome://selenium-ide-testrunner/content/selenium/selenium-test.css" 
    It seems FF3's default security settings not allow external file access chrome:// resource.
    I read Mozilla docs: https://developer.mozilla.org/en/Chrome_Registration#contentaccessible

    It said: (New in FF3) Chrome resources can no longer be referenced from within <img>, <script>, or other elements contained in, or added to, content that was loaded from an untrusted source. This restriction applies to both elements defined by the untrusted source and to elements added by trusted extensions. If such references need to be explicitly allowed, set the contentaccessible flag to yes to obtain the behavior found in older versions of Firefox.

    So, I try to modify Selenium-IDE1.0b2's chrome.manifest file.
    content selenium-ide-testrunner jar:chrome/selenium-ide.jar!/content/ xpcnativewrappers=no 
    content selenium-ide-testrunner jar:chrome/selenium-ide.jar!/content/ xpcnativewrappers=no contentaccessible=yes
    SeleniumIDE1.0b2 displayed CSS correctly this time on FF3 Chrome TestRunner.

    Wednesday, December 3, 2008

    Test Flex/Flash in Selenium

    There are many automation tools for testing Flex & Flash RIA web page, like TestComplete, QTP. However, they are all not free. It's good news if Selenium supports such kind of RIA testing.

    We found there are some interesting projects on Selenium for Flex/Flash testing.

    Here is a topic: http://www.agimatec.de/blog/2008/11/selenium-flex-tests-with-maven/

    Introduction:
    SeleniumFlexApi is in one part a Flex library which needs to be included in your app and on the other part an extension to the Selenium IDE. I like the Flex part, because it makes it easy to dive over Javascript into the details of your Flex app. What I don’t really like is the Javascript extension. Yes, it is not only a Selenium IDE part and you can include it into the start of the Selenium Server, but it is far away from Java and Maven.

    FlashSelenium is a piece of code which let’s you talk from Java over Selenium with your Flex app. The Java part is very handy, but you need to code into Flex, which methods you could call over your bridge. So I need to write special code, which opens the bridge. The SeleniumFlexAPI is much better in this part.

    Monday, December 1, 2008

    Fix ui-element can't run bug in Chrome TestRunner mode

    Selenium-IDE1.0b2 introduced a new feature: UI-Element. It is very useful feature for annoyed XPATH EL.

    We try demo in Selenium IDE panel successfully. However, when we try UI-Element test case in Chrome TestRunner, even if we set &userExtensionsURL=chrome://selenium-ide/content/ui-element.js,file:///c:/ui/my-ui-map.js correctly in chrome URL, it failed to load any UI-Element feature.

    Weird, we have discussed it on openqa forum. Finally, I fix this bug in chrome://selenium-ide/content/ui-element.js
    function is_IDE()
    {      
     //[Begin Fix]
     var locstr = window.location.href;  
     if(locstr.indexOf("selenium-ide.xul") != -1){
         //IDE mode   
         return true;
     }else{
         return false; 
     }
     //[End]
     return (typeof(SeleniumIDE) != 'undefined');
    }
    It seems
    typeof(SeleniumIDE) != 'undefined'
    not work in Chrome TestRunner mode.

    Finally, with above fix, UI-Element works in both IDE and Chrome TestRunner.

    More discussion: http://clearspace.openqa.org/message/53306

    Tuesday, November 18, 2008

    Write user-extensions.js working for both Selenium-IDE and Selenium-Core

    Sometimes we found our user-extensions.js(like include cmd)work on Selenium-Core but error on Selenium-IDE, or vise versa.

    The reason is IDE and Core used 30% different js objects.

    We can write below style user-extension to support both IDE and Core.
    Sample:
    if(IDE_MODE){
       //IDE manner
    }else{//CORE_MODE
       //Core manner
    }
    The key function here is how to determine current mode type by javascript?
    Simple workaround code:
    function isIDE(){
      var locstr = window.location.href;  
      if(locstr.indexOf("selenium-ide.xul") != -1){
          //IDE mode   
          return true;
      }else{
          return false; //Core mode
      }
    }

    Monday, November 17, 2008

    Multiple Profiles running simultaneously synchronization in SeleniumJRunner

    We must assign each SeleniumJRunner a unique profile name at one time.
    Otherwise, if 2 instances use the same profile name at one time, Firefox will report "Firefox is already running..." error.

    Assume we have pre-created Firefox profiles on one machine with name:
    testprofile1
    testprofile2
    testprofile3
    ...
    testprofile9

    We can lock profile name when one SeleniumJRunner is using it and unlock it when SeleniumJRunner quit. If there is no free profile name, let new SeleniumJRunner wait.

    Sample code:
    public static String lockAndGetAvailableProfile(String[] allProfiles) { 
      FileLock lock = null; 
      for (int i = 0; i < allProfiles.length; i++) {
       try {
        RandomAccessFile raf = new RandomAccessFile(PROFILE_LOCK_FOLDER 
                            + "/" +i+".lck" , "rw");
        FileChannel fileChannel = raf.getChannel();
        lock = fileChannel.tryLock();
        if(lock != null && lock.isValid()
           && !lock.isShared()){ 
         return allProfiles[i];
        }else{
         lock = null;
         fileChannel.close();
         raf.close();
        }
       } catch (IOException ioe) {
        ioe.printStackTrace();
       } 
      }
      return "No available profile";
    }
    

    Sunday, November 16, 2008

    Daemon AutoIt3 process to click popup and alerts for Selenium

    There are always all kinds of popup and alerts during selenium running. If they are not clicked out, Selenium may be blocked and timed out.

    We recommend to use a free tool AutoIt3 to write daemon scripts to click out any popup. Make sure selenium run smoothly.

    Sample Autoit3 scripts:
    If WinExists("Alert") Then
        WinActivate("Alert")
        ControlSend ("Alert", "", "", "{SPACE}")
        ContinueLoop
    EndIf
    

    Saturday, November 15, 2008

    Customize Selenium Chrome TestRunner profile in JRunner

    There is a prefs.js configuration file for each Firefox profile.
    Selenium Java Runner can do some necessary customization before launch Firefox TestRunner instance.
    Read prefs.js docs: http://kb.mozillazine.org/Prefs.js_file

    Sample prefs.js customize code:
    PrintStream out = new PrintStream(new FileOutputStream(prefsJS, true));
    // Don't ask if we want to switch default browsers
    out.println("user_pref('browser.shell.checkDefaultBrowser', false);"); 
    // suppress authentication confirmations
    out.println("user_pref('network.http.phishy-userpass-length', 255);");
    // Disable pop-up blocking
    out.println("user_pref('browser.allowpopups', true);");
    out.println("user_pref('dom.disable_open_during_load', false);");
    out.println("user_pref('startup.homepage_welcome_url', '');");
    // Disable Restore your session dialog. Always start a new session
    out.println("user_pref('browser.sessionstore.enabled', false);");
    // Disable security warnings
    out.println("user_pref('security.warn_submit_insecure', false);");
    out.println("user_pref('security.warn_submit_insecure.show_once', false);");
    out.println("user_pref('security.warn_entering_secure', false);");
    out.println("user_pref('security.warn_entering_secure.show_once', false);");
    out.println("user_pref('security.warn_entering_weak', false);");
    out.println("user_pref('security.warn_entering_weak.show_once', false);");
    out.println("user_pref('security.warn_leaving_secure', false);");
    out.println("user_pref('security.warn_leaving_secure.show_once', false);");
    out.println("user_pref('security.warn_viewing_mixed', false);");
    out.println("user_pref('security.warn_viewing_mixed.show_once', false);");
    // Disable cache
    out.println("user_pref('browser.cache.disk.enable', false);");
    out.println("user_pref('browser.cache.memory.enable', true);");
    // Disable "do you want to remember this password?"
    out.println("user_pref('signon.rememberSignons', false);");
    // Disable any of the random self-updating crap
    out.println("user_pref('app.update.auto', false);");
    out.println("user_pref('app.update.enabled', false);");
    out.println("user_pref('extensions.update.enabled', false);");
    out.println("user_pref('browser.search.update', false);");
    out.println("user_pref('browser.safebrowsing.enabled', false);"); 
    //enable javascript 
    out.println("user_pref('javascript.enabled', true);"); 
    //Optional Tips: User-Agent hijack for http traffic debug
    //out.println("user_pref('general.useragent.override', 'Here crack user-agent');");  out.close();

    Friday, November 14, 2008

    Include JS src file in Firefox Chrome Javascript file

    To include a Javascript src file in HTML, it's very simple:
    <script src="scripts/b.js" type="text/javascript"></script>
    However, how to implement above logic in a Chrome js file(XUL missed including b.js).
    There is a XPCOM API @mozilla.org/moz/jssubscript-loader;1
    Sample:
    const subScriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
     //file:/// path for XPCOM. http:// not support
    subScriptLoader.loadSubScript('file:///c:/scripts/b.js', this);
    In normal js file(non-chrome), it's also simple.
    Sample:
    document.write('<script src="scripts/b.js" type="text/javascript"></script>');
    This tip is useful for Selenium-IDE enhancement.

    Thursday, November 13, 2008

    Security Alerts Interceptor for Selenium-IDE

    There are 2 kinds of HTTPs Certification Security Alerts in Firefox.
    1) Domain name mismatch
    2) Certificate expired
    Read http://kb.mozillazine.org/Security_Error:_Domain_Name_Mismatch_or_Server_Certificate_Expired
    Unfortunately, Firefox 2 didn't provide a preferences entry to disable or hidden alerts.
    These annoyed alerts slow down selenium speed and block selenium automation if we don't click out alerts.

    There is a Firefox extension Remember Mismatch Domains(RMD.xpi) to add a "Don't warn me again about this certificate for this domain" checkbox to the Domain Mismatch and Expired Certificate warning windows. When selected the domain name and security certificate domain pair (or certificate and expiration date pair) is stored in a Firefox preference and the security error dialogue will be bypassed on subsequent visits.

    The author provided a XPCOM to destroy Security Alerts Window before it displays in Firefox. We can integrate this XPCOM with Selenium-IDE and provide Security Alerts Interceptor feature.

    Here are steps and code:
    1) copy /platform folder from RMD.xpi to selenium-ide.xpi
    2) copy rmdBadCertHandler.js and rmdIBadCertHandler.xpt to /components folder
    3) edit rmdBadCertHandler.js
    _isRemembered: function (handler_type, target_url, cert) {
       return true;
       //intercept and kill all security alerts
       //or add a preference entry here and return true or false
       ...
    }
    NOTE: it is used for Firefox 2 only. Firefox 3 has a new security exception handler.

    Tuesday, November 11, 2008

    Selenium close all Firefox windows in profile when finish run

    There is a Chrome URL parameter &close=true in Selenium-IDE. When set to true, it will automatically close Selenium Firefox windows when finish run. However, if application window has popuped child windows like ads, selenium can't close these popup windows when quit.

    This has risk to lock Firefox profile when multiple Firefox profiles running simuteniously. Please refer to "Firefox is aleady running issue"
    Enhance Selenium-IDE by XPCOM API: close all Firefox windows under current profile. Add logic when Selenium quit(close=true).
    Sample code:
    var windowManager = Components
      .classes['@mozilla.org/appshell/window-mediator;1'].getService();
    var windowManagerInterface = windowManager
      .QueryInterface( Components.interfaces.nsIWindowMediator);
    var enumerator = windowManagerInterface.getEnumerator( null );
    var appStartup = Components
      .classes['@mozilla.org/toolkit/app-startup;1'].
      getService(Components.interfaces.nsIAppStartup);
    while ( enumerator.hasMoreElements()  )
    {
      var domWindow = enumerator.getNext();
      if (("tryToClose" in domWindow) 
           && !domWindow.tryToClose())
        return false;
      domWindow.close();
    };
    appStartup.quit(Components.interfaces.nsIAppStartup.eAttemptQuit);
    

    Monday, November 10, 2008

    Save to local files with XPCOM for Selenium Firefox Chrome

    We save selenium run snapshot/result/log to local files instead of complex POST to server solution. We require APIs in browser(out of Security Sandbox) for local files accessing.

    In IEHTA, there is ActiveX object FSO to access local files.
    var objFSO = new ActiveXObject("Scripting.FileSystemObject");
    Is there a similar object in Mozilla Firefox Chrome?
    The answer is XPCOM object @mozilla.org/file/local;1

    Save to file Sample:
    function saveFile(fileName, val){
       try {
          netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
       } catch (e) {
          //alert("Permission to save file was denied.");
       }
       var file = Components.classes["@mozilla.org/file/local;1"]
                 .createInstance(Components.interfaces.nsILocalFile);
       file.initWithPath(fileName);
       if (file.exists() == false) {
         //alert("Creating file... " );
         file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
       }
       var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                .createInstance(Components.interfaces.nsIFileOutputStream);
       outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 ); 
       //UTF-8 convert
       var uc = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
         .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
       uc.charset = "UTF-8";
       var data_stream = uc.ConvertFromUnicode(val);
       var result = outputStream.write(data_stream, data_stream.length );
       outputStream.close(); 
    }
    We use UTF-8 encode here.

    Sunday, November 9, 2008

    PseudoTestRunner.html to run Selenium Chrome by cmdline

    Launch Selenium Chrome TestRunner by cmdline: firefox.exe -chrome "chrome://selenium-ide-testrunner/content/selenium/TestRunner.html..." will cause many weird issues on Firefox.
    I'm not sure the root cause which maybe related to Firefox Chrome Security Strategy for -chrome parameter.

    Here is a workaround:
    Hijack selenium-ide.xpi. Under selenium-ide\content\selenium\ add a file PseudoTestRunner.html
    Source:
    <html>
    <head><title>Pseudo Test Runner - Used for launch Chrome by Cmdline</title></head>
    <body>
      <script language="javascript"> 
      var strHref;
      var intParamsStartIndex;
      var strParams;
      strHref = window.location.href;
      intParamsStartIndex = strHref.indexOf("?");
      strParams = strHref.substring(intParamsStartIndex);
      window.open("chrome://selenium-ide-testrunner/content/selenium/TestRunner.html" + strParams);
      window.close();
      </script>
    </body>
    </html>
    
    Then please always use PseudoTestRunner.html instead of TestRunner.html in cmdline, keep the same parameter list in URL.

    Refer to topic: http://clearspace.openqa.org/message/51801#51801