Thursday, September 23, 2010

Automatically click Yes on Internet Explorer Security Alert in Python for automated testing (with Selenium)

Trying to run Selenium RC tests for a secure test website, and kept getting an "alert" from Internet Explorer 6 that there's a problem with the certificate. After searching for answers, I couldn't find a solution to force Internet Explorer 6 to ignore/accept the cert, despite adding it to "trusted sites", and the certificate to "Trusted" stores. In my case, the alert mentions the company name does not match (so it seems it will never accept it).

So, the only workaround I could think of was to automatically click the Yes button (or maybe send alt+y). Below is the python code that seems to do the trick for me:

import win32gui
import win32api


def ie_secalert_yes(maxtry=10):     
    """Left click YES button if the IE Security Alert dialog is found.
    Put this is in a thread, lest it be blocked from ever finding the alert.
   
    Keyword arguments:
    maxtry -- Give up looking for dialog after this many attempts (default 10)
   
    Note:
    (hdlg) FindWindow did not seem to work with class = "Dialog", but 32770
    does.
   
    """
   
    hdlg = 0
    hwnd = 0
    curtry = 0
   
    while curtry < maxtry: 
        hdlg = win32gui.FindWindow(32770, "Security Alert")
        if hdlg == 0:
            curtry += 1
            time.sleep(0.2)
        else:
            curtry=maxtry

     if hdlg > 0:
        hwnd = win32gui.FindWindowEx(hdlg,0,"Button", "&Yes")
        if hwnd > 0:
            win32api.PostMessage(hwnd, 0x0201, 0, 0) #WM_LBUTTONDOWN
            win32api.PostMessage(hwnd, 0x0202, 0, 0) #WM_LBUTTONUP
 
 

You could return hdlg or hwnd if you wanted, they'll be > 0 if the dialog was found. The above also works fine when there is no alert dialog too, in case there's any concern. This could easily be adapted/extended to handle any other window outside of selenium's control.

You should put the above in a thread (I used a timer), then init the timer before selenium, but run it after selenium.start, an example

t = threading.Timer(3.0, ie_secalert_yes)
    self.selenium = selenium("localhost", 4444, "*iexplore", URL) 
    self.selenium.start()
    t.start()
 
 


Caveats: Only tested in Windows XP, Selenium RC 2, and Python 2.6.5, Internet Explorer 6.

Tuesday, September 21, 2010

Auto-complete for R in EmEditor

This will work for any language you have a syntax file for, but I'll use R as the example.

1. Open an R file, and on the plugins menu, click "Word Complete"
2. Type a couple of letters e.grn and then press ctrl+space (default word suggest keyboard combination) and hopefully an auto-suggest popup menu will appear.


3. If nothing seems to happen, be sure you have configured R with the R syntax file (see previous post), otherwise you may need to do some further configuration. 
3.1 Go to Tools > Plugins > Customize Plugins > Word Complete (be sure it has a check) > R and double check the various settings, also verify the keyboard short cut assigned under the keyboard tab.




This post was inspired by Tal Galili's discussion of wordpress themes, and Yihui Xie's Notepad++ autocomplete efforts.

Sunday, September 12, 2010

EmEditor Professional as an R script editor

R is not supported "out of the box" by EmEditor, so here's a few tips I've found for using it as a great editor for R.
  • Code Syntax highlighting
  • Executing R scripts and capturing output
  • Use ctags symbols to navigate files.
  • If you create packages for CRAN or like neat code, you might like to use a macro to tidy it up. 

    Getting the R syntax file
    This is on the EmEditor website, in the Library > Syntax Files section. Thank you to whomever created and uploaded it. While you are in the properties, it's also a good opportunity to change tabs to spaces.

    Creating an "External Tool" to run R scripts
    The following settings (see also screenshot) in the create/properties screen for external tools should get things running:
    Command: C:\Program Files\R\R-2.11.0\bin\Rterm.exe
    Arguments: --vanilla --quiet --slave --file="$(Path)"
    Initial Directory: $(Dir)
    Icon Path: C:\Program Files\R\R-2.11.0\bin\R.exe
    Save file: Checked
    Use Output bar: Checked
    Close one exit: Checked
    Input: None
    Output: Create new document
    Standard Error: Create new document

    Using a macro to run R scripts
    Edit 18th Jan 2011: The below text refers to the old version of the macro and is no longer relevant, a new post will  describe the new macro, and it is also documented on the github site. Get the new macro now hosted on github

    Edit 10th Jan 2011 - This macro has been updated and there is now a specific post regarding it.
    See emeditor-r-code-macro-almost.html

    This offers more flexibility than the external tool choice, particularly if you want to run only a portion of code (more than passing an "expression"), or if the file has not been saved yet. The Rrun.jsee macro is one I constructed to allow passing a saved file, unsaved file or snippet of code to rterm - feel free to use it and improve it.

    Generating and using ctags symbols for R
    The simplest way of using ctags for an unsupported language is to use the regular expression support, and include it in a file .ctags in your home directory (c:\users\xxxx\ for vista). This will then automatically get picked up by ctags and you won't need to do any further config regarding it in EmEditor (or other editor for that matter). It's also O/S transportable.
    My .ctags has the following, you may want to modify it to suit your particular needs though (you probably don't want the apply or plot, or even variable).
    Note: ctags does not seem to support multiline, so if you have a function assigned on a different line (like when it is tidied by R (see below)) it won't find it.

    --langdef=R
    --langmap=R:.r
    --regex-R=/([[:alnum:]\._]+)([[:space:]]*)<-([[:space:][:cntrl:]]*function)/\1/f,function/
    --regex-R=/([[:alnum:]]+)[[:space:]]*<-/\1/v,variable/
    --regex-R=/[^[:alnum:]]plot[[:space:]]*\(([[:alnum:],=" ]+)\)/\1/p,plot/
    --regex-R=/([[:alpha:]]apply[[:space:]]*\([[:alnum:],=" ]+\))/\1/a,apply/i
     
     
    

    To double check the file is ok, you can run "C:\Program Files\EmEditor\PlugIns\ctags.exe" --list-kinds  it will show R listed. When you update the symbols in EmEditor, you should see something similar to this:



    Tidying up R code with a macro in EmEditor
    Based on the "Writing R Extensions" manual, I created an EmEditor macro (jsee) to do basic R code tidying. It is based off the run R macro, so you can tidy sections of code as well as the whole file. It should be easily modified to run through cscript/wscript rather than EmEditor, just beware some objects are EmEditor only.
    Note: This method will remove any comments in the code, it also seems to force new lines for function assigns, eg, turns this:

    shorten.to.string <- function(line, token)

    to

    shorten.to.string <-
    function (line, token) 



    I'm not sure if that breaks suggested style rules or not, is a pain for picking up with ctags though (more a ctags failing).