Monday, October 7, 2013

How to handle commandLink in Selenium 2?

My initial assumption on the link is constructed using <a href...="">. Thus when I initiate my test using following code, it was failed unexpectedly.
WebElement clickMe = webDriver.findElement(By.partialLinkText("Click Me"));;
Why this happened? I have double verify on my code, it just looking fine. But when I view the page’s source, I got the following code in my mind.

    Click Me

Doesn’t it sound funny? Consider the page was build using JSF and the link is construct using commandLink in JSF like this:

I wasn’t sure the background on how JSF is render on the browser. But one thing I am sure is the link has JavaScript. Without further ado, my temporary solution is to workaround the JavaScript using third party library called SeleniumJQuery. It is quite straight forward to use and pretty simple, to click on the link containing JavaScript, do this code:
jQueryFactory jq = new jQueryFactory();

WebElement clickMe = webDriver.findElement(By.partialLinkText("Click Me"));
It will work like charm.

No comments: