This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Learning Web UI Automation

Week 5 - JavaScript Execution

David Burns's picture
Tue, 2011-03-01 19:11

This week we are going to have a look at interacting with the page using Selenium and JavaScript. This can be useful for those times that we need to call JavaScript APIs. This can be useful if Selenium can't access that item at the time.

We had a brief look doing the wait_for_condition() during week 3, but let's break down all the bits that we need to use.

Get_eval()

The thing that we need to understand is that Selenium is developed in JavaScript. We use python to speak to a proxy that then translates that to JavaScript commands that are then used that in the browser. The JavaScript object that Selenium uses is called BrowserBot.

When we are working with Selenium all commands are held by the Selenium JavaScript object. This gives us access to another object called BrowserBot that has access to the browser. So if we wanted to we could call get_eval(), which executes JavaScript snippets that we give it, call the equivalent Selenium commands.

For example, we can do self.selenium.get_eval("this.isElementPresent('locator');") and it will return true.

I suggest at this point just trying it on one of the tests that you have already sent in.

The next thing that we can do is to understand BrowserBot. BrowserBot is the way that we control the DOM in the browser. So when we did our wait_for_condition(...) we needed to wait for something in the DOM to cause our snippet to return true.

We can also gain access to the Window and that way we can then call JavaScript libraries that are available. So if your test needs to work against a JavaScript API we would then get the window from BrowserBot and then call it.

For example.

selenium.get_eval("this.browserbot.getUserWindow().javascriptApiCall();")

The getUserWindow() returns an object that represents the window object that JavaScript has access to. Once you have that you can then call anything on the page that you want. One thing to note is that you will not be able to do something that violates the Same Origin Policy.

Firing Events

There are times that we need to fire off a JavaScript event before we can use that element. This could be the onBlur or onFocus events when we use an input box. To do this we would use the fire_event() method. This takes 2 arguments, the first is the locator and the second is the event name.

So to hover over something you would use the mouseover event. If you wanted to focus on the element you would fire the onFocus event and if you wanted to go away from an element, you would fire the onBlur event. To do this we need to just mention which event to fire. So for mouseover we would do selenium.fire_event("locator","mouseover"). To do an onBlur do selenim.fire_event("locator", "blur")

Task

This week's task is going to be a bit of a free for all. I would like you to pick a site, preferably visible to the
public so I can view it, and write a test that interacts with the page using something we have learnt in this lecture.

Email me with your answers and if you get stuck let me know so I can help!