Paul Hammant's Blog: Contrasting Selenium 2 locator styles
Just a quickie - with the Etsy.com tutorial in the JBehave we have three styles of using Selenium 2 (WebDriver) to locate and click on things in the page. The Page objects (which still need some work) illustrate the difference.
- Plain Java, canonical WebDriver style
- Plain Java, WebDriver in fluent style
- Groovy + Geb
Plain Java style:
findElement(xpath("//a[@title = '" + section + "']")).click(); findElement(id("search-query")).sendKeys(thing); WebElement select = findElement(xpath("//select[@class ='handmade']")); new Select(select).selectByValue(subCategory);
link(xpath("@title = '" + section + "'")).click(); input(id("search_query")).sendKeys(thing).submit(); select(className("handmade")).selectByValue(subCategory);
$("a", title : section).click() $("#search-query").sendKeys(thing) // a small issue in geb presently makes this less elegant new Select($("select.handmade").getElement(0)).selectByValue(subCategory) // if it were not for that issues, the code should be: $("form").category = subCategory
I am not sure if the fluent style is better than the canonical Java way, as the latter’s locators may survive some page refactorings. I wish the WebDriver and WebElement APIs were more fluent though. Not least - I wish things didn’t return void.
Also, Geb’s last release is against Selenium 2.0rc3. We’re at 2.5.0 now, and that’s too far behind for my liking. If you attempt to force an upgrade of dependencies for Geb, you will encounter subtle incompatibilities. The work around for one of those is shown in the new Select(…) logic. Also, I’d wish that Geb did the fluent style too - matching what the WATIR team champion more closely that the fluent selenium thing shown in #2 above.
blog comments powered by Disqus