How to read the attribute of an xml node in unix?

January 10th, 2012By WaikhomComputers/Internet,Linux,Programming

In unix/linux, you can accomplish so many things which would normally require programming languages in other operating systems. The unix shell is such a powerful tool that almost every bit of automation is possible in unix. So, when it comes to automation, unix is my #1 choice. Today, I’ll share with you how to read xml attributes in unix.

PROBLEM

Although, there isn’t an in built library in unix for reading an xml attribute, there are several ways to read an xml attribute in unix. Some of them are complicated and some of them are too easy. Today, I’ll discuss one of the easiest ways to read the attribute of an xml node in unix.

COMMON APPROACHES

Before I come to the easiest one, I’ll point out some of the other ways to read/parse an xml but without much explanation. Here are some of the suggestions made by fellow unix users :-

  1. Using xsltproc: xsltproc is a unix tool for transforming xml into html/ other xml format by using xslt (stylesheet). This method needs the knowledge of xslt and might be a bit tricky and cumbersome. However, this method will have more accurate and less error prone results because of the use of an xml oriented tool
  2. Using awk / sed: awk or sed can be used in a somewhat tricky manner to read the xml attributes. This one is also sometimes tricky and a beginner might wanna avoid

THE SOLUTION – MY APPROACH

The way i read the attributes of an xml node in unix is quite easy and can be used effectively for many attributes but with one drawback. This method cannot be used to read attribute values if there are multiple attributes with the same name. Otherwise this method is very easy and very efficient.

Let’s consider an xml from which we want to read the attribute values.
e.g. item.xml

<root>
<item name="foo" value="bar"></item>
</root>

In the above example, we can read the attribute values of “name” and “value” by using the following unix command.

eval $(tr '[< >]' '\n' < item.xml | egrep 'name|value')

EXPLANATION

In order to understand the above command, let's break it down into small pieces.
First, let's consider the command "tr '[< >]' '\n'". This command is basically splitting the xml into lines. It replaces >< and spaces with new lines. In other words, the above xml will become:

root
item
name="foo"
value="bar"
/item
/root

Now, the piped command egrep 'name|value' does nothing but filter out all lines except the ones containing the name and value attributes. This way, the xml is now reduced to

name="foo"
value="bar"

Then we evaluate this string as unix commands by using the eval command so that their values are assigned to their respective variable names. And we can easily access the values of these attributes by using their variable names in the shell as $name and $value respectively.

How to automate file upload in Internet Explorer using Selenium?

January 8th, 2012By WaikhomComputers/Internet,Programming,Web Development

Selenium is the most popular framework for web browser automation. We use selenium for automating our UI test cases. Selenium works perfectly fine in most web browsers with the exception of the obviously most problematic “Internet Explorer”. Although most of the features of selenium work fine with internet explorer, there are a few hiccups particularly associated with Internet Explorer e.g. capturing Screenshot, file upload, etc. Here I’ll be talking about automating the file upload feature in Internet Explorer using Selenium.

In Firefox, you can automate file upload by simple using the “selenium.type” method over the file selector.

Selenium.type("/input[@name=image_file]");

However, the type method doesn’t work in Internet explorer because of security restrictions. Internet Explorer doesn’t allow you to change the value of file inputs through JavaScript. So, I did a little research on how I can automate the process in Internet Explorer. There seems to be a few solutions to this problem but none of them really solved mine because all of the solutions are for those who are not using selenium RC or are not using RC server in a remote machine. In my case, selenium RC is located in a remote machine and all my java classes in a separate controller machine. This makes it difficult to invoke autoit scripts as it is mentioned in most of the available solutions. Some of the methods I tried are:

  1. Using selenium.attachFIle: Selenium has an api for automating file uploads and is provided as the attachFile method. However, this method is supported in Firefox only and doesn’t work with Internet Explorer.
  2. Using native keys to type in the characters: Selenium RC has apis for generating native keyboard events. This method can be used to automate keyboard events in the OS level and outside the web browser. I also tried this in Internet Explorer, but once I click the browse button and the file selector popups, selenium gets blocked and the keys could not be typed without manual intervention
  3. Using AutoIT with Selenium: One most popular method of automating the file upload is to use an autoit script in conjuction with selenium. However this also had it’s own problem. I couldn’t invoke the autoit script from my test script as I was running selenium RC in a remote machine. I found another interesting post about this method here – http://automationtricks.blogspot.com/2010/09/how-to-upload-file-in-selenium.html

THE FIX

After a series of trials, I could finally automate the file upload scenario in Internet explorer. The fix was to upgrade my selenium from 1.x to 2.x (or webdriver). Unlike selenium 1.x, webdriver is not javascript driven and is not as restricted as javascript driven selenium 1.x. However, upgrading from 1.x to webdriver was a mess because all my test cases were then not working as expected in firefox. So, i did a little hack here to make sure web driver is used only in case of Internet explorer. Given below are the changes I made.

Before:

DefaultSelenium selenium = new DefaultSelenium("localhost",4444,"*iexplore","http://www.example.com");
selenium.start();

After:

WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),DesiredCapabilities.internetExplorer());
DefaultSelenium selenium = new WebDriverBackedSelenium(driver,"http://www.example.com"));

With this code change, we are now using web driver backed selenium, and because of this, the selenium.type method can be used without any security restrictions. As a result, my file upload automation was run successfully. Well, thanks to those guys working on web driver.

In fact, many of the limitations of selenium 1.x have been addressed by web driver and there’s a properly documented section on migrating selenium 1.x to webdriver. If you are facing any issues with selenium 1.x, you might try upgrading to web driver. More details on the upgrade process are available here – migrating from selenium 1.x to web driver. Well, if you find anything interesting, feel free to share it here.

How to prevent/disable javascript error popups in internet explorer while running Selenium tests?

January 6th, 2012By WaikhomComputers/Internet,Windows

Today, I was trying to run some selenium tests on Internet Explorer 8 and got another “This is why I hate Internet explorer” problem. All the selenium tests work fine in Firefox but as soon as I started running them in Internet Explorer, lots of javascript error popups started coming up. The popups were blocking the selenium tests and there was no way I could prevent those popups. In fact, this trivial problem blocked me for a day and was driving me crazy. And when I got through it at last, I thought what the fuck? I won’t let this stupid thing waste someone else’s time again. That’s why I’m writing this post so that you don’t get stuck like me again.

Well, it started with me migrating my selenium tests to webdriver. I had to migrate to webdriver (selenium 2.x) from selenium 1.x because file upload doesn’t seem to work properly (again in Internet explorer) with selenium 1. That leads me to another post – How to automate file upload in Internet explorer using selenium? But that’s another day. For now, let’s focus on preventing those javascript error popups.

How to prevent/disable javascript error popups in internet explorer?

It seems there are lots of information out there in Google on how to prevent / disable javascript error popups but none of them really worked for me. Some of them are simple settings change in IE but for some reason, all those instructions didn’t help me in any way. But finally, I got this worked when I reset the IE Settings. So, here we go all the steps I followed in order to disable those freaky javascript popups.

  1. Disable script debugging – I tried disabling the script debugging option with no use. Well, if you want to, you can disable script debugging by opening Internet Explorer -> Tools->Internet Options -> Advanced and checking the “disable script debugging” option there.
  2. Disable JavaScript error notification (both manually & using Microsoft fixit) – I tried this too but in vain. You can disable the error notifications by going to Tools->Internet Options -> Advanced and unchecking the “Display a notification about every script error” option. Or if your don’t want to do it manually, you can use the Microsoft fixit wizard available at http://support.microsoft.com/kb/822521 or follow the manual steps there
  3. Reset IE Settings – This was the last resort and it did solve the problem. The downside is that all IE settings are lost and you might not want it. But as I said, if the above steps didn’t work and you are ready to lose your IE settings, you can reset the IE settings to disable/prevent those crazy javascript errors. Well, you can find more info on the resetting procedure at http://support.microsoft.com/kb/923737/ln.

Well, with these steps, I was able to get rid of those annoying popups. If you are facing this issue, try these out and let me know the results.

Macbook Pro Unibody

October 27th, 2011By WaikhomComputers/Internet,Electronics/Gadgets,PC Hardware,Video Posts

One of the unique features of the macbook pro is the unibody. The enclosure of the macbook pro is made from a single block of aluminium and is called the unibody. This makes the body light, strong and smooth.

One thing that I like the most about apple products is their finishing. And in this case also, the macbook pro displays a symbol of delicate and super smooth finishing touch. Although I haven’t got one of these macbooks, I’m really in the mood to get one. And I’m sure I’ll buy one soon enough. Also, below is a video on the unibody and is worth watching.
YouTube Preview Image

Publish your blog posts in audio with text to speech converter

October 25th, 2011By WaikhomGeneral

After sitting in front of a PC for more than 9 hours, I don’t feel like staring into my Computer screen just to read a news article or a blog post. And for the last few days, I’ve not been quite well as far as my eyes are concerned. So I thought, what if I can listen to a blog post or an article? This reminded me of technologyreview.com which had indeed incorporated the audio podcasting feature some time back. And I really liked listening to their podcasts because the voice was natural and easy to understand.

So, I’ve been looking around for some audio synthesizer and did found some cool tools. One of them was the MARY Text to speech which is an open source project on text to speech coversion. I tried this out and the voice was clear and natural. It is the perfect choice if you own a commercial site and you don’t want dependencies on third parties. However, MARY is a generic TTS tool and apart from being a self hosted service, it doesn’t really have ready to use plugins for wordpress.

As the search kept going, I finally stumbled into odiogo which is a hosted service and of course free for bloggers. You need to register in their site but hey, that takes 10 secs only. And yes, odiogo has a wordpress plugin and to use it, you don’t have to do anything else apart from installing the plugin. Once you install the plugin and enter the feed id, it’s all good to go. There will be “listen now” button below the title of your blog post. Just click that button to listen to your blog post. Well, you can try this out by listening to this blog post by clicking the same button at the top (I’ve removed this plugin as it doesn’t work properly. I hope they fix the issues soon). I hope you enjoy reading this and try out this awesome plugin. Happy blogging :D .

Kontera and slow wordpress

October 24th, 2011By WaikhomComputers/Internet,Web Development

For quite some time, my blog has been slow and I finally got the time to sort this out. Using Firebug, I analyzed the page response times of all requests made while opening my homepage. And surprisingly, the kontera ad code that I added seems to be taking a lot of time. So, I just removed the ad code and tried refreshing my page and then …oorah.. My blog is loading faster than ever before.

But then, what about the bucks I get from kontera? I went to their site and just checked my last quarter earnings and it was pathetic. Who’s gonna sacrifice the speed of their blog for a few bucks a quarter. Maybe i’ll try something like infolinks but not kontera. Not anymore. Well, if you have kontera and your blog is taking too much time, removing the kontera ad code is the first thing I’ll suggest you to do. And after that, you can start looking for “i like taking time to load” plugins in firebug.

First post from my Android

October 16th, 2011By WaikhomCell Phones,Technology,Wordpress Plugins

So, I was playing around with my new HTC desire HD. And I found this cool android app with which you can virtually do anything with your WordPress blog. It’s called WordPress and its from Automattic inc. With this app, you can view and moderate comments, posts, pages and even view your wp stats.

Well, this is my first post from my android phone using this cool app. And even though you cannot manage your whole blog with this, there’s almost everything that you need everyday. Well, I think you should also try it out and share what you think about it.

Recent Photos

DSC04900DSC00584DSC00583DSC00580DSC04898DSC00492DSC00339DSC04828