0

I am a Selenium newbie ...

As a JavaScript programmer, I think I want to handle JavaScript events in my Selenium-2 tests (JUnit). I am joining a team where all of the existing tests have "waitForSomethingToBeRendered" methods. Is there some way my Selenium tests can handle/listen for DOM (or custom) events?

Also, I've read on SO where developers use FireBug to write/debug Selenium-2 tests. How does this work? I don't see FireBug in the browser launched by Selenium. Similarly, why does the following appear to have no effect? Am I trying something Selenium/JUnit does not support?

selenium().getEval("alert('hello');");
selenium().getEval("debugger;");
Upperstage
  • 3,747
  • 8
  • 44
  • 67

3 Answers3

1

Firstly if you are getting started with Selenium I suggest using the 2.0 API which is for WebDriver. To evaluate JavaScript in 2.0 simply cast your WebDriver object to a JavascriptExecutor object and use the methods provided by it. 'waitForSomethingToBeRendered' needs to be done in a few steps. First of all you must ensure the DOM object is available on the page. For this you can do something like this:

WebElement e = null;

try {
  e = driver.findElement( By.id("asdf") );
} catch {
  ...
}

Or:

driver.findElements( By.id("asdf") ).size() != 0

After determining whether the DOM object is available you can do:

e.isDisplayed()

Which will return to you whether the element is currently displayed.

In regards to what you have seen about FireBug and Selenium I am guessing you are confusing Selenium IDE which is a Firefox plugin with Selenium RC/WebDriver which is not a plugin.

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
1

Use Firebug for Firefox while normally browsing your site to explore your DOM to determine the correct element and class ids to select in your Selenium script. If you are using extjs you are going to have some extra fun in determining which elements to select, as extjs randomizes element ids. The best way is to add an extra css class to find the correct element, then select by that class.

Mike Miller
  • 2,149
  • 1
  • 18
  • 23
1

You don't see the firebug extension because selenium opens a stripped firefox. I'm guessing the reason is it's much faster. It's possible (and quite easy in selenium-2) to add the plugin.

The alert doesn't work because selenium-1 (I don't know how selenium-2 handles them) swallows the alerts. There's an api for handling alerts.

Plus, in selenium-1, the JS runs in a different window. So the equivalent of document.getElementById() is sel.getEval("selenium.browserbot.getCurrentWindow().document.getElementById()").

Finally, I don't know about events but you can wait for conditions: sel.wait_for_condition().

Guy
  • 14,178
  • 27
  • 67
  • 88
  • thank you for the comment (+1). I am using Selenium-2; can u point me to the FireBug/Selenium plugin? – Upperstage Jun 29 '11 at 22:10
  • check this question out: http://stackoverflow.com/questions/3421793/how-do-i-run-firebug-within-selenium-2 – Guy Jun 29 '11 at 22:19