如何使用Selenium WebDriver进行拖放操作

时间:2016-08-09 10:10:18

标签: java selenium-webdriver selenium-chromedriver selenium-firefoxdriver

我在使用Selenium WebDriver的拖放功能方面遇到了长期问题。

根据WebDriver文档,拖放应该是开箱即用的命令:

WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();

然而,这似乎与Firefox或Chrome驱动程序无关。

下面是一个示例测试,尝试在2个公共可拖动网站上使用拖放功能。使用FirefoxDriver和ChromeDriver参数化并执行测试。

package test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

@RunWith(Parameterized.class)
public class DragAndDropTest {

    enum Browser {FIREFOX, CHROME};

    private Browser browser;
    private WebDriver driver;

    @Parameters
    public static Collection<Object[]> data() throws Exception {
        List<Object[]> params = new ArrayList<Object[]>();
        params.add(new Object[] { Browser.FIREFOX });
        params.add(new Object[] { Browser.CHROME });
        return params;
    }

    public DragAndDropTest(Browser browser) {
        this.browser = browser;
    }

    @Before
    public void before() {
        switch (browser) {
        case FIREFOX:
            this.driver = new FirefoxDriver();
            break;
        case CHROME:
            this.driver = new ChromeDriver();
        }
    }

    @After
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void test1() {
        By drag = By.id("div1");
        By drop = By.id("div2");
        By expected = By.cssSelector("#div2 #drag1");
        // load page
        driver.get("http://www.w3schools.com/html/html5_draganddrop.asp");
        // wait for draggable element visible
        new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(drag));
        // drag and drop
        new Actions(driver).dragAndDrop(driver.findElement(drag), driver.findElement(drop)).perform();
        // verify results
        Assert.assertEquals("Drag&Drop failed", 1,driver.findElements(expected).size());
    }

    @Test
    public void test2() {
        By drag = By.id("Item1");
        By drop = By.id("Item5");
        By expected = By.cssSelector("#DragContainer5 #Item1");
        // load page
        driver.get("http://www.webreference.com/programming/javascript/mk/column2/index.html");
        // wait for draggable element visible
        new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(drag));
        // drag and drop
        new Actions(driver).dragAndDrop(driver.findElement(drag), driver.findElement(drop)).perform();
        // verify results
        Assert.assertEquals("Drag&Drop failed", 1, driver.findElements(expected).size());
    }
}

有关上述测试无效的原因的任何建议/指示?

1 个答案:

答案 0 :(得分:2)

W3C网站具有HTML5拖放功能,目前Webdriver不支持此功能。请参阅此问题 - https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/3604

但是在你的第一个测试用例中,你应该将它用于drag元素。 通过drag = By.id(“drag1”);

这是您要拖动的图像的定位器。