Using WebDriver and the power of jQuery to interact with html elements.

I was creating an automation test to verify that TeamMentor’s About page was displayed correctly. After opening the About page and perform the test, I wanted to close it (by looking at the small X  that closes the window).

But after some attempts to look at the element by class, I received an error. The Display attribute of the element was set to false (weird because it was displayed anyway). WebDriver throws an error when you are trying to perform any operation (like the click event) in an element that is not visible or the Display attribute is false.

ScreenHunter_167 Feb. 04 06.02

Using the magic of jQuery to interact with those scenarios

Fortunately, I was able to find an approach to fire events (like click), using jQuery. This approach is quite interesting because I could think in a wrapper to execute a JavaScript code that contains some test. So this is the way to execute the jQuery:

JavaScript Executor

This method receives the jQuery script and executes it, which is really powerful. So at the time of invoking this method, you should pass your script, here is an example of how to use it:

jQuery script

The previous code uses jQuery to perform the raise the click event and close the window. Doing this with WebDriver (and trying to look at the element by class throws an error).

It seems like this is a good approach to be considered in the near future.

Advertisements

About Michael Hidalgo

Michael is a Software Developer Engineer based on San José, Costa Rica. He leads the OWASP Chapter from Costa Rica. You can take a look at my blog at http://blog.michaelhidalgo.info/
This entry was posted in Browser Automation, JQuery, Selenium WebDriver and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s