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.
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.
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.
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");
}
}
You don't actually need to "click" on that button, but press "Enter" on it instead.
See http://dingyichen.livejournal.com/23628.html
精彩评论