如何使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()) - 但没有重现手动行为。
不幸的是看看这个案例,我无法用Selenium复制点击。我见过很多人抱怨他们不能将Selenium与GWT一起使用,其中一个更有名的团队也有这个问题。 Google Wave开发团队已开始使用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做到这一点。可能未来的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");
}
}