2

What is the correct way to run a jQuery .click() in a selenium test? The element being clicked is a hyperlink.

HTML:

<div id="buttons">
    <a class="button" id="btnRun" href="javascript:void(0)">Run</a>
</div>
<div id="output" />

jQuery

$(document).ready(function () {
    $('#btnRun').click(function () {
       $("#output").html("hello world");
    });
})

Selenium:

[Test]
public void TestOutput()
{
    selenium_local = new DefaultSelenium("localhost", 4444, 
                                         "*firefox", "https://localhost");
    selenium_local.Start(); 

    selenium_local.Open("/TestPage");
    selenium_local.WaitForPageToLoad("30000");
    Assert.That(selenium_local.IsElementPresent("id=btnRun"), Is.True);

    selenium_local.Click("id=btnRun");
    Thread.Sleep(5000);

    // also tried without success:
    // selenium_local.FireEvent("id=btnRun","click");
    // selenium_local.Click("xpath=//a[@id='btnRun']");
    // selenium_local.Click("dom=document.getElementById('btnRun')");

    Assert.That(selenium.GetValue("id=output"), Is.EqualTo("hello world"));
}

When I run the test the button click does not occur, and after the 2nd WaitForPageToLoad, the test finishes and reports failure (because the text hello world was not found, because the button click never occurred)

JK.
  • 21,477
  • 35
  • 135
  • 214
  • In your jQuery Code, you need to put $("#output").html("hello world"); – Xaisoft Apr 06 '11 at 03:33
  • Sorry that was just an example, the button click does actually do something (load a jqgrid) when it is clicked manually. But in the test the click is not even executing – JK. Apr 06 '11 at 03:36

2 Answers2

1

(Note: I use selenium with java, so there might be a difference, but I strongly doubt it)

I have similar problems sometimes, but we're using plain-ol'-javascript in some places, gwt in others, and mootools in some other places (don't ask why so many) for our different ajax calls so it's possible our problems are completely unrelated. But, maybe I can help regardless.

I have to sometimes use mouseOver then click. Sometimes I even have to do mouseDown and then mouseUp. Or a combination of the two. And even in some cases we have to retry the click.

(Also, unless clicking the link/button causes a full page load, the WaitForPageToLoad is going to cause issues because it waits for a page loaded event. Here's how I ended up waiting for Ajax calls to finish, however you should be able to come up with a cleaner solution if you're only using JQuery)

Community
  • 1
  • 1
Joel
  • 16,474
  • 17
  • 72
  • 93
  • Thanks, I will try mouseOver + click first. Which of my locators is best/or how can I tell that the locator has found the hyperlink? I already have custom ajaxStart/ajaxStop, (http://stackoverflow.com/questions/2878533/jquery-automatic-loading-gif-and-button-disable-on-submit-click) so I should be able to modify that to try your counter solution. – JK. Apr 06 '11 at 04:48
  • mouseOver + click didn't help, but I dont know if it is locating the element or not: I used id=btnRun. – JK. Apr 06 '11 at 04:57
  • strange... does IsElementPresent("btnRun") return true? Also, it might be too obvious to say but, make sure you don't have any duplicate IDs on the page, even if they're hidden. You might even try a different syntax. Like "css=div#buttons a#btnRun" or something. – Joel Apr 06 '11 at 20:49
  • I've added `Assert.That(selenium_local.IsElementPresent("id=btnRun"), Is.True);` and it did not fail the assert, so it must be there? There's only one item with that id. – JK. Apr 06 '11 at 22:42
  • That's very strange. And it's not throwing an exception at the click? I would guess that it's a problem with your jquery, I don't think I'll be much help with that. I would try messing around with the jquery code and see if I could get it working doing other things. – Joel Apr 07 '11 at 02:52
0

Some JavaScript libraries respond to the mouse-up event as indicating a "click". If so, MouseDown() followed by MouseUp() ought to do the job.

Ross Patterson
  • 9,527
  • 33
  • 48