views:

2355

answers:

5

How can I make a selenium click() work the same as a manual mouse click?

I have recently upgraded GWT from 1.7.1 to 2.0. Some selenium tests (SeleniumRC v1.0.1, IE7) are now failing. It seems that the Selenium.click() method is not selecting a GWT TreeItem. A manual click will make the TreeItem go blue (ie. look selected and have "gwt-TreeItem-selected" class attribute in the DOM), but the selenium test doesn't.

I'm convinced that selenium is actually finding the right element, just not clicking on it. If you change the string parameter in the click method you can check that selenium throws an exception when the element isn't found.

The sample code below uses the GWT Showcase website. It tries to click on the word "Beethoven". If you click on that word with your mouse, you'll see the TreeItem go blue. However when you run the selenium test, it won't.

package test;

import org.junit.Before;
import org.junit.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class TestTreeClick {
    static Selenium selenium = null;

    @Before
    public void setUp() throws Exception {
        if (selenium == null) {
            selenium = new DefaultSelenium("localhost", 4444, "*iexplore",
                    "http://gwt.google.com/samples/Showcase/Showcase.html#CwTree");
            selenium.start();
        }
    }

    @Test
    public void testingClicking() {
        selenium.open("http://gwt.google.com/samples/Showcase/Showcase.html#CwTree");
        selenium.click("gwt-debug-cwTree-staticTree-root-child0-content");
    }
}

I have tried some other methods (Selenium.clickAt(), Selenium.fireEvent(), Selenium.mouseOver()/Down()/Up() ) - but none reproduce the manual behaviour.

+4  A: 

Unfortunately having a look at this case I have not been able to replicate clicking with Selenium. I have seen a number of people complaining that they can't use Selenium with GWT and one of the more famous teams have that issue. The Google Wave development team have started using WebDriver to test their code.

Now the good thing is that there currently a project to merge Selenium and WebDriver as they have their strengths and weaknesses and a number of them are in different areas so the final product will be amazing.

I believe that they may have a working version of the WebDriverBackedSelenium at Google Code so all you would need to do is update the instantiation of Selenium and it should start using the WebDriver code to run your test.

AutomatedTester
Thanks for that suggestion. I tried using the WebDriverBackedSelenium but got the same outcome - Selenium didn't properly "select" GWT TreeItems that I tried to click using its click() method.
Glennn
hmm that seems wierd since its supposed to do webdriver commands but you just write selenium code or thats how I have interpreted it
AutomatedTester
Following your comment, I re-checked and it turns out that I wasn't using WebDriverBackedSelenium. I was using the selenium-2.0a.1 jar, but hadn't realised I had to use new WebDriverBackedSelenium() constructor instead of new DefaultSelenium(). With new constructor parameters and not-calling .start() (which throws an error) it worked. Thanks heaps.
Glennn
Glad it helped!
AutomatedTester
+2  A: 

It seems that WebDriver can do it like this.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Example {
 public static void main(String[] args) throws InterruptedException { 
  WebDriver driver = new InternetExplorerDriver();
  driver.get("http://gwt.google.com/samples/Showcase/Showcase.html#CwTree");
  WebElement element = driver.findElement(By.id("gwt-debug-cwTree-staticTree-root-child0-content"));
  element.click();
 }
}

I'd still like to be able to do it with Selenium. It may be that a future Selenium release will more fully incorporate WebDriver, and everything will be wonderful again. But I guess this works for now.

Glennn
A: 

I wanted to post the code that finally worked for me following useful comments from AutomatedTester.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.ie.InternetExplorerDriver;

import com.thoughtworks.selenium.Selenium;

public class TestTreeClick {

    public static void main(String[] args) {
        WebDriver driver = new InternetExplorerDriver();
        Selenium selenium = new WebDriverBackedSelenium(driver, "http://gwt.google.com/samples/Showcase/Showcase.html#CwTree");
        selenium.open("http://gwt.google.com/samples/Showcase/Showcase.html#CwTree");
        selenium.click("gwt-debug-cwTree-staticTree-root-child0-content");
    }
}
Glennn
A: 

is there any version of Selenium IDE (or a different tool similar to it) that can handle this problem? cause with the version 1.0.6 requests are not being send to the server after clicking a button.

hunter1986
A: 

You don't actually need to "click" on that button, but press "Enter" on it instead.

See http://dingyichen.livejournal.com/23628.html

Ding-Yi Chen