Thursday, April 02, 2009

Syntax Highlighting for Sahi Code

I have added syntax highlighting to my blogs now. Used Alex Gorbatchev's SyntaxHighlighter 2.0 which is awesome. I added a Sahi brush to SyntaxHighlighter so that keywords in Sahi would also be highlighted.

This is what shBrushSahi.js looks like:

SyntaxHighlighter.brushes.SahiScript = function()
{
var keywords = 'abstract boolean break byte case catch char class const continue debugger ' +
'default delete do double else enum export extends false final finally float ' +
'for function goto if implements import in instanceof int interface long native ' +
'new null package private protected public return short static super switch ' +
'synchronized this throw throws transient true try typeof var void volatile while with';

var schedulerFns = '_alert _assertEqual _assertNotEqual _assertNotNull _assertNull _assertTrue _assert _assertNotTrue _assertFalse _assertExists _assertNotExists _callServer _click _clickLinkByAccessor _dragDrop _resetSavedRandom _setSelected _setValue _simulateEvent _call _eval _setGlobal _wait _popup _highlight _log _navigateTo _callServer _doubleClick _rightClick _addMock _removeMock _expectConfirm _setFile _expectPrompt _debug _debugToErr _debugToFile _mouseOver _keyPress _focus _keyDown _keyUp _mockImage _execute _assertContainsText _enableKeepAlive _disableKeepAlive _dragDropXY _deleteCookie _createCookie _clearPrintCalled _saveDownloadedAs _clearLastDownloadedFileName _rteWrite';

var browserFns = '_accessor _button _check _checkbox _image _imageSubmitButton _link _password _radio _select _submit _textarea _textbox _event _getGlobal _random _savedRandom _cell _table _containsText _containsHTML _byId _row _getText _getCellText _div _span _spandiv _option _lastConfirm _reset _file _lastPrompt _lastAlert _get _style _byText _cookie _position _print _printCalled _label _lastDownloadedFileName _rteHTML _rteText _re _prompt _getCellText _getSelectedText _scriptName _isVisible _listItem _parentNode _parentCell _parentRow _parentTable _in';

var otherFns = '_getDB _readFile _logException _logExceptionAsFailure _stopOnError _continueOnError _include';

schedulerFns += (' ' + otherFns);


this.regexList = [
{ regex: SyntaxHighlighter.regexLib.singleLineCComments, css: 'comments' }, // one line comments
{ regex: SyntaxHighlighter.regexLib.multiLineCComments, css: 'comments' }, // multiline comments
{ regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' }, // double quoted strings
{ regex: SyntaxHighlighter.regexLib.singleQuotedString, css: 'string' }, // single quoted strings
{ regex: /\s*#.*/gm, css: 'preprocessor' }, // preprocessor tags like #region and #endregion
{ regex: new RegExp(this.getKeywords(schedulerFns), 'gm'), css: 'color4' }, // operators and such
{ regex: new RegExp(this.getKeywords(browserFns), 'gm'), css: 'color5' }, // operators and such
{ regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword' } // keywords
];

this.forHtmlScript(SyntaxHighlighter.regexLib.scriptScriptTags);
};

SyntaxHighlighter.brushes.SahiScript.prototype = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.SahiScript.aliases = ['sahi', 'sahiscript'];


And this is what I added to my blogger template:


<script src='http://sahi.co.in/static/syntaxhighlighter/scripts/shCore.js' type='text/javascript'/>
<script src='http://sahi.co.in/static/syntaxhighlighter/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://sahi.co.in/static/syntaxhighlighter/scripts/shBrushSahi.js' type='text/javascript'/>
<link href='http://sahi.co.in/static/syntaxhighlighter/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://sahi.co.in/static/syntaxhighlighter/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<style>
.syntaxhighlighter .color4, .syntaxhighlighter .color4 a{color: #000A7F !important;}
.syntaxhighlighter .color5,.syntaxhighlighter .color5 a{color: brown !important;}
</style>
<script type='text/javascript'>
SyntaxHighlighter.config.clipboardSwf = 'http://sahi.co.in/static/syntaxhighlighter/scripts/clipboard.swf';
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>


Note that, in blogger, <br> tags will be visible in the code in place of new lines if you do not add
SyntaxHighlighter.config.bloggerMode = true;


So now Sahi scripts on the blog will look like this:


function search(){
_setValue(_textbox("q"), "sahi");
_click(_submit("Google Search"));
_assertExists(_link("Sahi"));
}
function goToForums(){
_click(_link("Sahi"));
_click(_link("Forums"));
}
function logout(){
if (_condition(_link("Logout"))){
_click(_link("Logout"));
}
}
function login($username, $password){
_click(_link("Login"));
_setValue(_textbox("req_username"), $username);
_setValue(_password("req_password"), $password);
_click(_submit("Login"));
_assertExists(_listItem("Logged in as "+$username));
}

search();
goToForums();
logout();
login("sahitest" "khuljasimsim");

Wednesday, October 01, 2008

Chrome rocks!

Chrome rocks absolutely!

It is blazingly fast, and feels ultra light weight. After using it for a few weeks now, I am totally addicted.

The landing page which shows snapshots of all pages recently viewed, is very convenient to start off and I feel great to just be able to click on a thumbnail and get to the site with out examining the url on my url bar or clicking on a bookmark. In fact the landing page is something that has really made a lot of difference in my browsing experience. I don't get lost as often as I used to and it saves me a lot of time and effort.

I love the incognito mode which allows me to login simultaneously as different users, even into web applications which use permanent cookies. This is great for developers testing web applications.

I like the ability to drag a tab and create its own window and vice versa.

Space usage on chrome is very efficient and the concept of getting rid of the windows title bar is a very good one. The concept of showing page popups as part of the same tab but hidden down below is also quite useful.

Honestly speaking I don't care much about being able to type the search terms or url into a single box. Surprising that Google talks about that more than the other features.

My default browser was Firefox, but now I am converted.

Sunday, August 31, 2008

Sahi V2 20080831 Released

After close to a month of development, Sahi V2 20080831 has been released today. (http://sahi.co.in/w/)

This release uses Rhino as the scripting engine, thus moving most of the script execution to the proxy. This should go a long way in simplifying Sahi scripts. Scripts now execute on the proxy, and only stuff that needs to execute on the browser is sent to the browser. Thus scheduler functions are sent to the browser for execution. One change which has come in is that custom functions which may have been added for identification of browser elements, now need to be wrapped in a <browser></browser> tag so that they are also sent to the browser.

This build also has some important changes to the SocketPool which will fix issues related to too many sockets being used and errors due to BindExceptions. Suite execution has been changed such that even if the browser crashes, the suite will continue with the next script and thus not hold up a build. DB methods now close connections properly.

There will still be a few rough edges and I hope users will report bugs so that they are easily fixed. Meanwhile, help spread the message through your blogs or email forums.

Sahi V2 20080831 Released

After close to a month of development, Sahi V2 20080831 has been released today. (http://sahi.co.in/w/)

This release uses Rhino as the scripting engine, thus moving most of the script execution to the proxy. This should go a long way in simplifying Sahi scripts. Scripts now execute on the proxy, and only stuff that needs to execute on the browser are sent to the browser. Thus scheduler functions are sent to the browser for execution. One change which has come in is that custom functions which may have been added for identification of browser elements, now need to be wrapped in a tag so that they are also sent to the browser.

This build also has some important changes to the SocketPool which will fix issues related to too many sockets being used and errors due to BindExceptions. Suite execution has been changed such that even if the browser crashes, the suite will continue with the next script and thus not hold up a build. DB methods now close connections properly.

There will still be a few rough edges and I hope users will report bugs so that they are easily fixed. Meanwhile, help spread the message through your blogs or email forums.

Thursday, August 21, 2008

Sahi - Latest developments

Copy of post on forum: http://sahi.co.in/forums/viewtopic.php?id=261

I had not been active on the Sahi forums (http://sahi.co.in/forums) for sometime. Thanks a ton to StringyLow, tinchie8, lepierrot, pankaj.nith and others for keeping this forum active, and replying to posts.

Meanwhile I have been working on a version of Sahi which attacks one of the basic problems with Sahi.
Scopes of variables, scheduler and normal functions and the the way steps are queued and executed and the way their integrity needs to be maintained across page loads.

The problem had been that the scripts (after parsing) were executed on the browser itself, and when a page unloads, the state of the script's execution needed to be persisted on the proxy and then resurrected when the next page loaded. While this allowed the ease of using javascript for scripting, when scripts became bigger, the browsers and the proxy had to do a lot more persisting and resurrecting.

As the logical next step, I wanted to move this script execution to the proxy. I now use Rhino, an excellent javascript engine, to execute the scripts on the proxy. Only steps that need to be executed on the browser are sent to the browser. Javascript can still be used for Sahi scripting. Even though the script still is parsed, it is much simpler to understand script execution than it was before. Rhino also comes with a debugger which can prove useful to Sahi script debugging.

The version is slated to be released soon. I am looking for volunteers to test and give me feedback about the new version. If you have existing scripts, the scripts may need to be modified a little to make it work with the new version.
If you are interested, please post back or email me at narayan at sahi.co.in.

Tuesday, August 12, 2008

Viewing cookies in IE

There are times during development when you wish to see the cookies of a particular site, but do not know how to view them. You can use this simple solution to view cookies.

Drag this link to the Bookmarks toolbar on firefox. (Enable the toolbar by: View -> Toolbars -> Bookmarks Toolbar)
Show Cookies

On IE, right click on link, "Add to Favorites", Create In -> "Links". (Enable toolbar via Tools -> Toolbars -> Links)

Once you have this link on the toolbar, you can navigate to any site and click it to see the cookies.

Of course there are plenty of cookie viewing techniques and tools around, but this is a very easy and handy way of checking cookies quickly.

The html of the link above looks like this:
<a href="javascript:alert(document.cookie)">Show Cookies</a>

Bookmarlets like these can be really useful at times. For example in my newly launched http://www.househunt.in website, I have a bookmarklet which helps me gather relevant links from a builder's site. All I need to do is navigate to the builder's website, click a bookmarklet on my browser and it injects javascript into the page to collect all relevant links and display a popup page. I then just add some more information and submit. This ease has helped more than 400 projects in Pune to be added within a short span of time.

Monday, August 11, 2008

Launching HouseHunt.in

While looking for houses I needed an easy way of searching and shortlisting properties in Pune. So wrote this web application over the past few weeks. It is ready for use now.

The url is http://www.househunt.in

For the geekily oriented, this app is built and deployed on the Google App Engine platform (http://code.google.com/appengine/).

It is written in python using some features of the django framework and uses Google's BigTable instead of a regular database.

Tuesday, April 15, 2008

Unicode encode decode error in python email

Been bothered with these on and off errors in our application while sending email via python:
File "/usr/lib/python2.4/email/Generator.py", line 182, in handletext self._fp.write(payload)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2014' in position 4986: ordinal not in range(128)

The reason is that Python's email module needs some information about charset used when a MIMEText object is created.

Here is a link which explians how to solve this:
http://mg.pov.lt/blog/unicode-emails-in-python


Links to understand unicode in Python:

http://effbot.org/zone/unicode-objects.htm
http://www.amk.ca/python/howto/unicode