Posted By

noah on 09/17/09


javascript js testing frames ruby BDD scope functional ui TDD selenium unit frameset selenium-client

Versions (?)

How to call JavaScript in the Application Under Test, from Selenium

 / Published in: Ruby

When writing functional tests with Selenium, it is often convenient to call JavaScript functions and methods, that are part of the Web application you are testing. After all, Selenium is a functional testing framework for Web UI and most of the exciting bits of Web UI are JS (sorry, all you CSS hackers, but them's the facts).

The problem is that it's not immediately obvious how you can access the DOM of the Application Under Test (AUT). Selenium Core (in which all Selenium tests execute) is a frame set, and if you simply try to call your JavaScript with getEval, you'll get an error because getEval references Selenium Core's DOM, not the DOM of the AUT.

Fortunately the answer is simple. Use this.browserbot.getCurrentWindow() to get a reference to the window object in your DOM. Then you can execute any of the JS that lives in your page, right from Selenium!

This example uses the Ruby driver, but the principle is the same regardless of which language you use.

  1. # Assuming you have a global object named FOO, and you want to execute the method
  3. @selenium.js_eval(this.browserbot.getCurrentWindow()
  5. # This does exactly the same thing as opening the Firebug console and executing

Report this snippet  

You need to login to post a comment.