GWT2.0的硒测试
如何使selenium click()与手动鼠标单击一样工作 我最近将GWT从1.7.1升级到了2.0。一些硒测试(SeleniumRC v1.0.1,IE7)现在失败了。似乎Selenium.click()方法没有选择GWT树项。手动单击将使TreeItem变为蓝色(即,在DOM中看起来选中并具有“gwt TreeItem selected”类属性),但selenium测试没有 我确信硒实际上是在寻找正确的元素,只是没有点击它。如果更改click方法中的string参数,可以检查selenium在找不到元素时是否引发异常 下面的示例代码使用GWT Showcase网站。它试图点击“贝多芬”这个词。如果你用鼠标点击这个词,你会看到树变成蓝色。但是,当您运行selenium测试时,它不会GWT2.0的硒测试,gwt,selenium,automated-tests,Gwt,Selenium,Automated Tests,如何使selenium click()与手动鼠标单击一样工作 我最近将GWT从1.7.1升级到了2.0。一些硒测试(SeleniumRC v1.0.1,IE7)现在失败了。似乎Selenium.click()方法没有选择GWT树项。手动单击将使TreeItem变为蓝色(即,在DOM中看起来选中并具有“gwt TreeItem selected”类属性),但selenium测试没有 我确信硒实际上是在寻找正确的元素,只是没有点击它。如果更改click方法中的string参数,可以检查seleniu
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");
}
}
我尝试过其他一些方法(Selenium.clickAt()、Selenium.firevent()、Selenium.mouseOver()/Down()/Up())-但没有一种方法可以重现手动行为。不幸的是,在查看此案例后,我无法用Selenium复制单击。我看到很多人抱怨说他们不能在GWT中使用硒元素,而一个更著名的团队也有这个问题。GoogleWave开发团队已经开始使用WebDriver来测试他们的代码 现在好的事情是,目前有一个合并Selenium和WebDriver的项目,因为它们有各自的优点和缺点,而且其中许多都在不同的领域,所以最终的产品将是惊人的
我相信他们可能有一个WebDriverBackedSelenium的工作版本,所以您需要做的就是更新Selenium的实例化,它应该开始使用WebDriver代码来运行您的测试。看起来WebDriver可以这样做
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();
}
}
我仍然希望能够用硒来做。未来的Selenium版本可能会更全面地结合WebDriver,一切都会再次变得美好。但是我想这现在可以用了。我想在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");
}
}
你实际上不需要“点击”那个按钮,而是按“回车”键
请参见在selenium core/src/main/resources/core/scripts/selenium-browserbot.js中,实际触发点击事件的selenium函数是triggerMouseEvent。大部分代码都是创建一个事件对象,然后调用element.dispatchEvent(event)。谢谢你的建议。我尝试使用WebDriverBackedSelenium,但得到了相同的结果-Selenium没有正确地“选择”我尝试使用其click()方法单击的GWT树。嗯,这看起来很奇怪,因为它应该执行webdriver命令,但您只编写Selenium代码,或者这就是我在您的评论后对其的解释,我重新检查后发现我没有使用WebDriverBackedSelenium。我使用的是selenium-2.0a.1jar,但没有意识到我必须使用新的WebDriverBackedSelenium()构造函数而不是新的DefaultSelenium()。使用新的构造函数参数,并且不调用.start()(会引发错误),它工作正常。谢谢你。