Archive for the ‘Uncategorized’ Category

How to get VNC working with a hidden desktop on Ubuntu 9.04 jaunty

Friday, November 6th, 2009

The problem: you want to VNC into your work machine, an Ubuntu 9.04 jaunty, over a VPN but not have everything you do show up on your screen at work. Obviously, the client machine doesn’t matter — you just need a VNC viewer.
The background: there is an old post from 2006 with 60 pages of information on this. The url is this link.
It has been summarised with some personal feedback in 2007 at this link.
Unfortunately it does not work on Ubuntu 9.04 jaunty.

Here are the steps:

  • Do what this link suggests.
  • Change
    service Xvnc
    {
    type = UNLISTED
    disable = no
    socket_type = stream
    protocol = tcp
    wait = yes
    user = root
    server = /usr/bin/Xvnc
    server_args = -inetd :1 -query localhost -geometry 1024x768 -depth 16 -once -fp /usr/share/fonts/X11/misc -DisconnectClients=0 -NeverShared passwordFile=/root/.vncpasswd -extension XFIXES
    port = 5901
    }

    to

    service Xvnc
    {
    type = UNLISTED
    disable = no
    socket_type = stream
    protocol = tcp
    wait = yes
    user = root
    server = /usr/bin/Xvnc
    server_args = -inetd :1 -query 127.0.0.1 -geometry 1024x768 -depth 16 -once -fp /usr/share/fonts/X11/misc -DisconnectClients=0 -NeverShared passwordFile=/root/.vncpasswd -extension XFIXES
    port = 5901
    }
  • Do

    sudo mkdir -p /etc/X11/xserver
  • Create a file SecurityPolicy in /etc/X11/xserver. You can find one on the Web. I started with this link.
  • Create a file rgb.txt in /etc/X11. I used the one from the vim installation.
  • Reboot and you’re done.

How to use Selenium in real life with a rapidly-changing UI

Tuesday, June 9th, 2009

Selenium is a web testing framework. You can break it up into 3 parts:

1. The web site being tested
2. A client that you write (in Java or C#, for example), that exercises the web site and makes assertions in a framework like JUnit or NUnit
3. A Selenium server, written in Java, that acts as a proxy for your client, forks browsers (e.g. FireFox and IE) and then mediates between your client and your web site. Part of the power of Selenium is that it uses your actual browser, so you get close to an accurate testing environment.

The problem with web testing in general is that it is very sensitive to changes in the site being tested. Change the xpath of a control and your test will have to be rewritten.

This solution took only 2 days to implement and negates the need for someone to rewrite the tests every day in a language like C#. Just re-record, translate and debug and you’re done. Developers can do it themselves for features that they come up with, and QA can add tests of their own.

The process:

1. You use FireFox and the Selenium IDE plugin for FireFox to record a test in “HtmlSelenese” (the default save format for the plugin). Note that it is recorded in FireFox, but tested in any browser Selenium supports.
2. You automatically translate that test into Java or C# or whatever, making sure that your translator accommodates idiosyncracies in your UI or Selenium, or both. Most of the problems stem from JavaScript/Ajax, and result in the IDE giving you a wrong xpath for an element or a bad semantics for your application, e.g. when a div exists but is not visible.
3. You subclass DefaultSelenium in Java/C# to accommodate your translator. For example, you might want to override “IsVisible” to include testing for the presence of the element in a loop — Selenium will throw an error if you test before the page is fully loaded, which can happen with Ajax applications.

So what you are doing here is using one-off development time to replace everyday QA time. You will need to update the translator much less often than the tests that it translates.

The methodology:

Selenium is written in Java. You can modify the XlateHtmlSeleneseToJava class to produce a translator that translates to anything. Just remember to add the ideoc.xml that this class needs.

The payoff:

Your QA people can come up with a methodology for recording tests, and then rerun the Selenium IDE to record this test, and translate the test automatically to your testing framework’s language, in minutes. No fiddling around with rewriting Java or C# tests every time the web site changes.

Agile, that is.

Recursive table expressions in SQLServer 2005

Friday, May 29th, 2009

Given a table Edge(Source, Sink) representing a directed graph, the transitive closure TC(Source, Sink) is the set of all paths in the graph. Relational algebra (on which SQL is based) can’t express it. However, you can express it using first-order logic rules:

TC(X,Y) :- Edge(X,Y)
TC(X,Y) :- Edge(X,Z), TC(Z,Y)

or

“there is a path from X to Y if there is an edge from X to Y, or an edge from X to some Z such that there is a path from Z to Y”.

SQLServer 2005 now offers you a way to do this using recursive tables:

with TC(Source,Sink,Depth) as
(
select Source, Sink, 0 as Depth
from Edge
union all
select e.Source, t.Sink , t.Depth+1 as Depth
from Edge e, TC t
where e.Sink = t.Source
and t.Depth < 10
)
select …

Note that we have limited the recursion depth to 10 to force termination. This has been sufficient for a recent project. The default recursion depth in SqlServer2005 is 100, but you can set it to be higher (up to 32767). So if your graph has paths with lengths over 32767, you won’t get a complete result. However, this seems to work in practice.

Cron and time

Saturday, May 2nd, 2009

UNIX cron has been around for over 30 years. It’s a way to schedule events. Here’s what it looks like:

# .—————- minute (0 – 59)
# | .————- hour (0 – 23)
# | | .———- day of month (1 – 31)
# | | | .——- month (1 – 12) OR jan,feb,mar,apr …
# | | | | .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed

Here is a question:

Can you add time to a schedule, i.e. say “5 hours after the cron fires do something”, in cron syntax?

Hints:
a) Can you express the first day of the month?
b) Can you express the last day of the month?

Using ASP.NET routing with MonoRail to default to index.castle

Wednesday, March 25th, 2009

We have a web site based on MonoRail with some aspx pages. This article is about using ASP.NET routing to redirect all urls of the form /controller to /controller/index.castle.

1. IIS configuration.

IIS will not let you route urls that aren’t mapped to a handler, so we need to use a wildcard handler.
However, we don’t want to use a handler for static content, e.g. images.

  • Use adsutil.vbs to add the wildcard “*,…/aspnet_isapi.dll,0,All” to the ScriptMaps for your site.
    Presumably you are setting the ScriptMaps anyway to map the .castle extension to aspnet_isapi.dll.
    However, it is important that the flags be 0 for the wildcard.
  • Then use adsutil.vbs to set /…/dir/ScriptMaps to the empty string, where dir is an IIsWebDirectory that exists in your site and contains static content.

2. For the routing, you will need the assemblies System.Web.Routing and System.Web.Abstractions from the directory Program Files/Reference Assemblies/Microsoft/Framework/v3.5

a. In your web.config, add the following at the top of the httpHandlers section:

add name=”urlRoutingModule” type=”System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″

b. Add the following to the top of your Global.asax

<%@ Import Namespace="System.Web.Routing" %>

c. Add the routes to your Global.asax.cs file as follows:

protected void Application_Start(object sender, EventArgs e)
{

RegisterRoutes(RouteTable.Routes);
}

private static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route(”{prefix}/{*rest}”, new MyRouteHandler()));
}

d. Create the class MyRouteHandler as follows:

public class MyRouteHandler : MonoRailHttpHandlerFactory, IRouteHandler
{
private const string RailsContextKey = “rails.context”;
private const string IsMonorailRequestKey = “is.mr.request”;

public IHttpHandler GetHttpHandler(RequestContext context)
{
string path = RouteTable.Routes.GetVirtualPath(context, new RouteValueDictionary {}).VirtualPath;
string last = path.Split(’/').Last();
HttpContext httpContext = HttpContext.Current;
if(!last.Contains(”.”))
{
path = string.Format(”{0}/index.castle”, path);
httpContext.RewritePath(path);
Initialize(httpContext.ApplicationInstance);
}
if(path.EndsWith(”.castle”))
{
return GetHandler(httpContext, httpContext.Request.RequestType, httpContext.Request.RawUrl,
httpContext.Request.PhysicalApplicationPath);

}
return (IHttpHandler) BuildManager.CreateInstanceFromVirtualPath(path, typeof (Page));
}

private static void Initialize(HttpApplication app)
{
// Create the IRailsEngineContext
HttpContext context = app.Context;
IServiceContainer container = ServiceContainerAccessor.ServiceContainer;
IUrlTokenizer urlTokenizer = (IUrlTokenizer)container.GetService(typeof(IUrlTokenizer));
HttpRequest request = context.Request;
UrlInfo urlInfo = urlTokenizer.TokenizeUrl(request.FilePath, request.Url, request.IsLocal, request.ApplicationPath);
IServiceProvider userServiceProvider = ServiceProviderLocator.Instance.LocateProvider();
DefaultRailsEngineContext newContext = new DefaultRailsEngineContext(ServiceContainerAccessor.ServiceContainer, urlInfo, context, userServiceProvider);
context.Items[RailsContextKey] = newContext;
context.Items[IsMonorailRequestKey] = true;
newContext.AddService(typeof (IRailsEngineContext), newContext);
// Create the controller
UrlInfo info = newContext.UrlInfo;
IControllerFactory controllerFactory = (IControllerFactory)newContext.GetService(typeof(IControllerFactory));
Controller controller = controllerFactory.CreateController(info);
// Create the executor
IControllerLifecycleExecutorFactory factory =
(IControllerLifecycleExecutorFactory)newContext.GetService(typeof(IControllerLifecycleExecutorFactory));
IControllerLifecycleExecutor executor = factory.CreateExecutor(controller, newContext);
context.Items[ControllerLifecycleExecutor.ExecutorEntry] = executor;
// Initialize
executor.InitializeController(info.Area, info.Controller, info.Action);
// Validate
if (!executor.SelectAction(info.Action, info.Controller))
{
return;
}
if (!executor.RunStartRequestFilters())
{
newContext.UnderlyingContext.Response.End();
executor.Dispose();
}
}
}

At this point, you’re done.

Running regression tests with Selenium and Internet Explorer on Windows

Tuesday, March 17th, 2009

There is a lot of misinformation on the Web about running Selenium as part of regression testing in a Windows environment. Here is what worked for us.

Design goals:

1 Run the selenium tests on the command line.
2 Run selenium tests as part of regression testing on a build server with IE6. The regression tests are run by a service.

In both cases, the main options are:

A Run the Selenium server as part of test setup and close it on test teardown
B Run the Selenium server as a service

Synopsis:

1A Works out of the box
1B If you only have IE7, use srvany for the selenium server and you are done. Otherwise see the case below.
1B, 2A, 2B Use srvany for the selenium server, *iexploreproxy on the client and -singleWindow on the server