Selenium, Firebug and Xpath (updated)

Posted: February 2nd, 2011 | Author: | Filed under: general | 2 Comments »

Today I was working on writing a selenium test to ensure a feature was working properly on our site.  The button I was trying to click had a duplicate button on the page and each did something different.  The problem was that they were both in different forms, but had the same name and no ID.  In  selenium the only way to target the specific button is via XPath.  The XPath for this is a little crazy:

//div/div[2]/div/div/div[2]/div/div/div[2]/div[2]/div[2]/div[2]/div/form/input[13]

Now I spent a bit of time trying to figure it out manually and quickly gave up.  I figured firebug might have something and sure enough if you “Inspect Element” on the button firebug pops up with the full html path to the element shown across the top of the firebug window.  Hovering over each element gives you the full XPath and right clicking lets you do a few different operations such as Copy Html and Copy XPath.  Now the XPath that it gives you will look like

/html/body/div/div[2]/div/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div/form/input[13]

I removed the /html/body and replaced with // to match the rest of the XPath that selenium generates, replaced the name=fieldname in my test and it worked.

*Please NOTE*

This is horrible don’t ever use it 🙂 – All joking aside if you have the option to add a class or an id(as rasenplanscher points out in the comments below) to an element on the page that you need to target you should do that instead.  If you can’t like this situation, this will work for you.


I heart groovy

Posted: January 19th, 2011 | Author: | Filed under: code | Tags: | Comments Off on I heart groovy

Reason number 2343242354 why i love groovy – I had a bunch of urls and I needed to run NSLOOKUP on them. I had a couple of options, write a shell script, do each by hand, write it in java (eeek) or write it in groovy. I chose groovy.

["https://www.googleadservices.com",
"https://roia.biz",
"https://www.emjcd.com",
"https://nmargin.com",
"https://ssl.google-analytics.com",
"https://www.ifactz.com",
"https://secure.interclick.com",
"https://r.turn.com",
"https://secure.trafficmp.com",
"https://secure.fastclick.net",
"https://ads.revsci.net",
"https://conv.opt.fimserve.com",
"https://ds.contextweb.com",
"https://ad.yieldmanager.com",
"https://beacon.dedicatednetworks.com",
"https://flex.atdmt.com",
"https://seal.verisign.com",
"https://sealserver.trustkeeper.net"].each{ url ->
println "nslookup ${url[8..-1]}".execute().text
}

Ok let’s look whats going on here. First thing you’ll notice if you’re coming from the java world is there is no class definition, there’s an implicit class created for you so for simple scripts especially run once scripts like this, it’s not necessary.

The first part is a list which is created with []

["https://www.googleadservices.com",
"https://roia.biz"]

Next we are calling a method on the list called .each which accepts a closure which is in this case

{ url ->
println "nslookup ${url[8..-1]}".execute().text
}

Contained in this closure is what I really needed to do, which was do an NSLOOKUP on each url, but only the domain, I had to strip out the protocol (I know this could have easily been done with a search and replace in my editor but then you don’t have any example code)

We start by creating a groovy string – denoted by the"" which allows us to substitute the url in with the ${url} syntax. Another niceity is to omit the {}’s if you do not need to run any additional methods on the variable you are substituting – had the https:// not been in the url i could have written it like so:

println "nslookup $url".execute().text

What you’ll notice is by using the array index notation with a range I am able to get all chars starting at position 8 and go all the way to the end (in groovy -1 means the end of the string, -2 would be 2 from the right -3, 3 from the right etc. – no more subString(pos, string.len - pos)) This same syntax also works with lists

Now that I have the portion of the domain I need, we call .execute() on it. In groovy a .execute method is added to the String class and when doing so the entire string will be passed to the command line and executed. The following would print out the directory listing where the script was running.

"ls -la".execute().text

Finally we call .text (equivalent of .getText()) since .execute only returns a java.lang.UNIXProcess and the text property gives you back the output

Also note that println is the equivalent of System.out.println("text") – it’s just another groovy shortcut.