问题 GWT 2.0的硒测试


如何使selenium click()与手动鼠标点击相同?

我最近将GWT从1.7.1升级到2.0。一些硒测试(SeleniumRC v1.0.1,IE7)现在失败了。似乎Selenium.click()方法没有选择GWT TreeItem。手动单击将使TreeItem变为蓝色(即,看起来已选中并且在DOM中具有“gwt-TreeItem-selected”类属性),但是selenium测试没有。

我确信硒实际上找到了正确的元素,而不是点击它。如果在click方法中更改字符串参数,则可以检查selenium在找不到元素时是否抛出异常。

下面的示例代码使用GWT Showcase网站。它试图点击“贝多芬”这个词。如果用鼠标单击该单词,您将看到TreeItem变为蓝色。但是,当您运行硒测试时,它不会。

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.fireEvent(),Selenium.mouseOver()/ Down()/ Up()) - 但没有重现手动行为。


3954
2018-01-18 06:22


起源

实际触发click事件的selenium函数是selenium-core / src / main / resources / core / scripts / selenium-browserbot.js中的triggerMouseEvent。大多数代码都是创建一个事件对象,然后调用element.dispatchEvent(event)。 - John


答案:


不幸的是看看这个案例,我无法用Selenium复制点击。我见过很多人抱怨他们不能将Selenium与GWT一起使用,其中一个更有名的团队也有这个问题。 Google Wave开发团队已开始使用WebDriver测试其代码。

现在好消息是,目前有一个合并Selenium和WebDriver的项目,因为它们有自己的优点和缺点,而且其中很多都在不同的领域,因此最终产品将是惊人的。

我相信他们可能有一个WebDriverBackedSelenium的工作版本 谷歌代码 所以你需要做的就是更新Selenium的实例化,它应该开始使用WebDriver代码来运行你的测试。


6
2018-01-18 15:30



谢谢你的建议。我尝试使用WebDriverBackedSelenium但得到了相同的结果 - Selenium没有正确地“选择”我尝试使用click()方法单击的GWT TreeItems。 - Glennn
嗯这似乎很奇怪,因为它应该做webdriver命令,但你只是写硒代码或那就是我如何解释它 - AutomatedTester
在你的评论之后,我重新检查了,结果发现我没有使用WebDriverBackedSelenium。我使用的是selenium-2.0a.1 jar,但没有意识到我必须使用新的WebDriverBackedSelenium()构造函数而不是新的DefaultSelenium()。使用新的构造函数参数并且不调用.start()(抛出错误)它起作用。谢谢堆。 - Glennn
很高兴它有帮助! - AutomatedTester


似乎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做到这一点。可能未来的Selenium版本将更全面地融入WebDriver,一切都会再次变得美妙。但我想现在这个有用了。


2
2018-01-19 06:08





我想根据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");
    }
}

1
2018-01-20 00:27





您实际上不需要“点击”该按钮,而是按“Enter”键。

看到 http://dingyichen.livejournal.com/23628.html


1
2018-05-26 06:20