jQuery in selenium

we all love jQuery. so why not use jQuery in selenium? after all it locates elements. jQuery will make it easy, we just have to add a location strategy that uses jQuery to find elements. These two links helped me get started.

http://stackoverflow.com/questions/2814007/how-do-i-add-a-jquery-locators-to-selenium-remote-control

http://stackoverflow.com/questions/4185640/get-element-with-jquery-and-selenium-ide-1-0-8

Here are the steps i followed. I used selenium-server-standalone-2.2.0.jar downloaded from here.

Step 1:

Extract the jar. I used 7zip to extract.

Step 2:

Edit TestRunner.html inside the core folder.
Add the following as said in SO.

<script language="JavaScript" type="text/javascript" src="jquery.min.js"></script> <script language="JavaScript" type="text/javascript"> function openDomViewer() { var autFrame = document.getElementById('selenium_myiframe');         var autFrameDocument = new SeleniumFrame(autFrame).getDocument();         this.rootDocument = autFrameDocument;         var domViewer = window.open(getDocumentBase(document) + 'domviewer/domviewer.html');         return false; } </script>

And add the jquery-min.js in core folder.

Step 3:

Then add the location strategy to your selenium object as follows.

mySelenium.addLocationStrategy("jquery",
"var loc = locator; " +
"var attr = null; " +
"var isattr = false; " +
"var inx = locator.lastIndexOf('@'); " +


"if (inx != -1){ " +
"   loc = locator.substring(0, inx); " +
"   attr = locator.substring(inx + 1); " +
"   isattr = true; " +
"} " +


"var found = jQuery(inDocument).find(loc); " +
"if (found.length >= 1) { " +
"   if (isattr) { " +
"       return found[0].getAttribute(attr); " +
"   } else { " +
"       return found[0]; " +
"   } " +
"} else { " +
"   return null; " +
"}"
);

Note that this is java code.

Step 4:

Add the following function to user-extensions.js in core/scripts folder.

function jQuery (selector)
{
return selenium.browserbot.getUserWindow().jQuery(selector);
}

Step 5:

Now we need to update the jar file. To do that do something like this.
jar -uf selenium-server-standalone-2.2.0.jar core\TestRunner.html
jar -uf selenium-server-standalone-2.2.0.jar core\jquery.min.js
jar -uf selenium-server-standalone-2.2.0.jar core\scripts\user-extensions.js

Step 6:

That’s it. Just start the server as usual using java -jar selenium-server-standalone-2.2.0

Step 7:

To use jquery to select elements, use something like this.
myselenium.click("jquery=#some_id")

hope it helps.

“No such file to load — application” on Passenger and Rails 3

I got this error when i was deploying a rails 3 app in railsplayground. I was using http://wiki.railsplayground.com/railsplayground/show/How+To+use+Phusion+Passenger as a reference. The error was somewhat familiar, as application controller was renamed to application_controller in 2.2 or 2.3 (not very sure when). Then i came across this post, http://groups.google.com/group/phusion-passenger/browse_thread/thread/676a73280221c93f. Which pointed out, we should use RackBaseURI instead of RailsBaseURI. Yep, the solution is, use

RackBaseURI /

instead of RailsBaseURI /

Ruby on rails – Beginning

I’ve started learning ruby on rails. Will share some useful stuffs on that too. To start with, here’s one.

To specify the root of your rails app, you might specify a default root in routes.rb file. Like,

map.root = :controller => ‘some_controller’, :action => “index”

But still you don’t see what you wanted to see?? Still seeing the Welcome aboard page??

Do not forget to remove the index.html file in public directory. 🙂

Apply style to all the child elements of a parent – CSS

If you want to apply style to all <p> inside <body>, you would write something like this,

body > p { background-color : #00f; }

Let’s say you have a div element and it contains various other elements. And you would like to apply some common style to all the elements. Believe me, there could be reasons for this!

We can’t go and pick all the elements inside <div>. We could just write,

#id_of_div *
{
//some style
}

So this will apply style to all the child elements of <div> with id  ‘id_of_div’. You can overwrite some style for child elements by picking them specifically like this,

#id_of_div #id_of_some_other_element
{
// some style that will override the style given by *
}

insert single quotes – mssql

You enclose strings in single quotes when you are inserting strings in a table on a mssql server. But when the string contains ‘ (single quotes) and you are not using something like a prepared statement(let’s say you wanna make a manual insertion) how would you do it? Simple. Use the single quotes twice. i.e

insert into table_name(string_column) values ('how're you?')

would become

insert into table_name(string_column) values ('how''re you?')

Cheers. 🙂

Fix for – E: Could not get lock /var/cache/apt/archives/lock – open (11: Resource temporarily unavailable)

E: Could not get lock /var/cache/apt/archives/lock – open (11: Resource temporarily unavailable)
E: Unable to lock the download directory

to fix these errors, see if any packet manager is running or any update is going on. if u didnt start any of these and have no idea what to do, just do the following

sudo killall apt-get

hope that solves your prob.

XML to HTML using XSLT

As you all know, XSLT stands for XSL Transformations. Okay, so you are converting XML to HTML using XSL Sometimes XML data might contain some HTML tags and you would want them to reflect in your HTML document. But by default this won’t happen.

For example,

if your XML file had a line this

<data>
<b> bold text </b>
</data>

When you extract the value of <data> tag in XSL, you would use something of this sort

<xsl:value-of select = "data" />

But in the resultant HTML doc, you will see

<b>bold text </b>

This is because, the tags are escaped i.e. <b> is treated as &lt;b&gt;

if you do not want this to happen,

add the following attribute in the xsl tag.

disable-output-escaping="yes"

now xsl line would look like this

<xsl:value-of select = "data" disable-output-escaping="yes"/>

and in the HTML file you will see

bold text
This will come in handy when you want to display units and stuff. Like cm2

There you go 🙂

File operations (read, write, etc..) in JAVA

For Java newbies & not-so-experienced guys,

To read or write files, you do not have to go to BufferedReader or any other stream readers and waste time writing your own code. Use Apache Commons IO. This comes with various pre-written functions and all IO operations are made simple. Dealing with folders are also easy!! Makes our job real easy. 🙂

How to run tomcats under Apache 2.2

We can run multiple tomcats under one apache server.  Refer this to install apache 2.2 and setup virtual hosting.

Let’s say you wanna run two tomcats under apache 2.2. Download and extract latest version of tomcat.
Configure ports for tomcats and test if both the tomcats can run at same time. (To avoid port conflicts.)
Make sure the line similar to this is not commented.

<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″/>
Give different ajp ports for the tomcats. let’s say 8009 for tomcat1 and 7009 for tomcat2.
set jvm route in tomcat1 server.xml. Find the tag and add jvmRoute="tomcat1"

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

set jvm route in tomcat2 server.xml

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

Install mod_jk module. Run,


sudo apt-get install libapache2-mod-jk

Append the following in /etc/apache2/mods-available/jk.load

JkWorkersFile /etc/apache2/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel debug
JkLogStampFormat “[%a %b %d %H:%M:%S %Y]”

Enable mod_jk by,

sudo a2enmod jk

Create a file called workers.properties in apache 2.2 conf directory. Paste the following lines and save it.

worker.list = loadbalancer,tomcat1,tomcat2

worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat1.connection_pool_timeout=600
worker.tomcat1.socket_timeout=600

worker.tomcat2.port=7009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.tomcat2.connection_pool_timeout=600
worker.tomcat2.socket_timeout=600

worker.loadbalancer.type = lb
worker.loadbalancer.balanced_workers = tomcat1,tomcat2
worker.loadbalancer.sticky_session = 1
#worker.loadbalancer.sticky_session_force = False

Please note that we have enabled sticky sessions.
Now we should tell apache when to forward requests to tomcat. Choose a virtualhost file.
In the place of DocumentRoot

add JkMount /* loadbalancer

this will tell apache to forward all request to the loadbalancer.
The virtual host file will look like this.

NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster@localhost1
ServerName localhost1
JkMount /* loadbalancer
</VirtualHost>

so now… all the requests that come to localhost1 will be forwarded to the loadbalancer. tht’s it. 🙂