Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Testing of GWT 2.0

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.

like image 318
Glennn Avatar asked Jan 18 '10 06:01

Glennn


3 Answers

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.

like image 134
AutomatedTester Avatar answered Nov 11 '22 10:11

AutomatedTester


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");
    }
}
like image 28
Glennn Avatar answered Nov 11 '22 10:11

Glennn


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

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

like image 45
Ding-Yi Chen Avatar answered Nov 11 '22 11:11

Ding-Yi Chen